sinlog 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/Changelog.md +40 -0
- data/docs/Readme-zh.md +163 -83
- data/docs/Readme.md +192 -98
- data/lib/sinlog/01_consts.rb +27 -0
- data/lib/sinlog/02_logger.rb +109 -0
- data/lib/sinlog/03_module_fn.rb +146 -0
- data/lib/sinlog/04_log_ext.rb +41 -0
- data/lib/sinlog/05_short_ext.rb +52 -0
- data/lib/sinlog/06_loggable.rb +160 -0
- data/lib/sinlog/07_proc.rb +93 -0
- data/lib/sinlog/08_module_short_ext.rb +89 -0
- data/lib/sinlog/version.rb +2 -2
- data/lib/sinlog.rb +31 -17
- metadata +10 -10
- data/lib/sinlog/init.rb +0 -106
- data/lib/sinlog/log_ext.rb +0 -62
- data/lib/sinlog/log_short_ext.rb +0 -69
- data/lib/sinlog/loggable.rb +0 -57
- data/rbi/sinlog/init.rbi +0 -34
- data/rbi/sinlog/log_ext.rbi +0 -27
- data/rbi/sinlog/log_short_ext.rbi +0 -27
- data/rbi/sinlog/loggable.rbi +0 -25
- data/rbi/sinlog/version.rbi +0 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sinlog
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
# @param level [Integer, String, Symbol, nil] Log Level.
|
|
7
|
+
#
|
|
8
|
+
# @return [Integer] the log level as an integer.
|
|
9
|
+
#
|
|
10
|
+
# @note If `level` is `nil`, returns `Sinlog.logger.level`.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
#
|
|
14
|
+
# require 'sinlog'
|
|
15
|
+
#
|
|
16
|
+
# # optional values:
|
|
17
|
+
# # 'debug', 'info', 'warn', 'error', 'fatal', 'unknown',
|
|
18
|
+
# # 'dbg', 'information', 'warning', 'err', 'unk',
|
|
19
|
+
# # '0', '1', '2', '3', '4', '5'
|
|
20
|
+
#
|
|
21
|
+
# Sinlog.as_log_level(:dbg) #=> LV[:debug] => 0
|
|
22
|
+
# Sinlog.as_log_level('info') #=> LV[:info] => 1
|
|
23
|
+
def as_log_level(level = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
|
|
24
|
+
level = Sinlog.logger.level if level.nil?
|
|
25
|
+
case level
|
|
26
|
+
when 0..5
|
|
27
|
+
level
|
|
28
|
+
when '0', '1', '2', '3', '4', '5'
|
|
29
|
+
level.to_i
|
|
30
|
+
when 'debug', 'dbg', :dbg
|
|
31
|
+
LV[:debug]
|
|
32
|
+
when 'info', 'information'
|
|
33
|
+
LV[:info]
|
|
34
|
+
when 'warn', 'warning'
|
|
35
|
+
LV[:warn]
|
|
36
|
+
when 'err', 'error', :err
|
|
37
|
+
LV[:error]
|
|
38
|
+
when 'fatal'
|
|
39
|
+
LV[:fatal]
|
|
40
|
+
when 'unk', 'unknown', :unk
|
|
41
|
+
LV[:unknown]
|
|
42
|
+
when Symbol
|
|
43
|
+
LV.fetch(level, LV[:error])
|
|
44
|
+
else
|
|
45
|
+
LV[:error]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @return [<Sinlog::Logger>] `=> Sinlog::Logger.instance`
|
|
50
|
+
def instance
|
|
51
|
+
Sinlog::Logger.instance
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Configures and returns the `Sinlog.instance.logger`.
|
|
55
|
+
#
|
|
56
|
+
# @param level [Integer, String, Symbol, nil] Log Level.
|
|
57
|
+
# @param env_name [#to_s] Name of the environment variable.
|
|
58
|
+
# @return [StdLogger]
|
|
59
|
+
#
|
|
60
|
+
# ## English
|
|
61
|
+
#
|
|
62
|
+
#
|
|
63
|
+
# You can configure the log level through parameters.
|
|
64
|
+
#
|
|
65
|
+
# - env_name
|
|
66
|
+
# - Specifies the name of an environment variable.
|
|
67
|
+
# - If set to nil, the program will attempt to read `RUBY_LOG` and set the log level accordingly.
|
|
68
|
+
# - Example: logger(env_name: nil)
|
|
69
|
+
# - If the user runs `RUBY_LOG=debug ./[your-script].rb`
|
|
70
|
+
# - The log level will be set to `debug`.
|
|
71
|
+
# - Example: logger(env_name: "XX_CLI_LOG")
|
|
72
|
+
# - If the user runs `XX_CLI_LOG=warn ./[your-script].rb`
|
|
73
|
+
# - The log level will be set to `warn`.
|
|
74
|
+
#
|
|
75
|
+
# - level
|
|
76
|
+
# - The level parameter takes precedence over env_name.
|
|
77
|
+
# - If both level and env_name are provided, the program will parse level first and return early.
|
|
78
|
+
#
|
|
79
|
+
# ## 中文
|
|
80
|
+
#
|
|
81
|
+
# 配置并获取 `Sinlog.instance.logger`。
|
|
82
|
+
#
|
|
83
|
+
# 我们可以通过参数来配置日志级别。
|
|
84
|
+
#
|
|
85
|
+
# - env_name
|
|
86
|
+
# - 指定特定的环境变量名称
|
|
87
|
+
# - 当其为 nil 时,程序默认会尝试获取 RUBY_LOG 的值,并设置日志级别。
|
|
88
|
+
# - 假设 logger(env_name: nil)
|
|
89
|
+
# - 若用户调用 `RUBY_LOG=debug ./[your-script].rb`
|
|
90
|
+
# - 则日志级别为 debug
|
|
91
|
+
# - 假设 logger(env_name: "XX_CLI_LOG")
|
|
92
|
+
# - 若用户调用 `XX_CLI_LOG=warn ./[your-script].rb`
|
|
93
|
+
# - 则日志级别为 warn
|
|
94
|
+
# - level
|
|
95
|
+
# - level 的优先级要高于 env_name
|
|
96
|
+
# - 若 level 和 env_name 都不为 nil, 则程序会优先解析 level,并提前 return。
|
|
97
|
+
#
|
|
98
|
+
# @see Sinlog::Logger.logger
|
|
99
|
+
#
|
|
100
|
+
# @example
|
|
101
|
+
#
|
|
102
|
+
# require 'sinlog'
|
|
103
|
+
#
|
|
104
|
+
# # optional level values:
|
|
105
|
+
# # - "debug"
|
|
106
|
+
# # - "dbg"
|
|
107
|
+
# # - "info"
|
|
108
|
+
# # - "warn"
|
|
109
|
+
# # - "error"
|
|
110
|
+
# # - "err"
|
|
111
|
+
# # - "fatal"
|
|
112
|
+
# # - "unknown"
|
|
113
|
+
# # - "unk"
|
|
114
|
+
# # - Integer (e.g., Sinlog::LV[:debug])
|
|
115
|
+
#
|
|
116
|
+
# log = Sinlog.logger(level: "dbg")
|
|
117
|
+
# log.level == Sinlog::LV[:debug] #=> true
|
|
118
|
+
# a = "Foo"
|
|
119
|
+
# log.debug "a=#{a}"
|
|
120
|
+
#
|
|
121
|
+
# using Sinlog::Refin
|
|
122
|
+
# Sinlog.logger(level: 'warn')
|
|
123
|
+
# log.level == Sinlog::LV[:warn] #=> true
|
|
124
|
+
# "Bar".log_warn
|
|
125
|
+
#
|
|
126
|
+
# ENV["CUSTOM_LOG"] = 'info'
|
|
127
|
+
# log = Sinlog.logger(env_name: "CUSTOM_LOG")
|
|
128
|
+
# log.level == Sinlog::LV[:info] #=> true
|
|
129
|
+
# "Baz".log_info
|
|
130
|
+
#
|
|
131
|
+
# log = Sinlog.logger(level: "error", env_name: "CUSTOM_LOG")
|
|
132
|
+
# log.level == Sinlog::LV[:error] #=> true
|
|
133
|
+
# "foobar".log_err
|
|
134
|
+
#
|
|
135
|
+
def logger(level: nil, env_name: nil)
|
|
136
|
+
std_logger = instance.logger
|
|
137
|
+
|
|
138
|
+
# if level != nil
|
|
139
|
+
return std_logger.tap { _1.level = as_log_level(level) } unless level.nil?
|
|
140
|
+
|
|
141
|
+
# if env_name != nil
|
|
142
|
+
instance.set_level_from_env!(env_name) unless env_name.nil?
|
|
143
|
+
|
|
144
|
+
std_logger
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sinlog
|
|
4
|
+
# @private
|
|
5
|
+
#
|
|
6
|
+
# == Methods
|
|
7
|
+
#
|
|
8
|
+
# - `#log_dbg` – DEBUG
|
|
9
|
+
# - `#log_info` – INFO
|
|
10
|
+
# - `#log_warn` – WARN
|
|
11
|
+
# - `#log_err` – ERROR
|
|
12
|
+
# - `#log_fatal` – FATAL
|
|
13
|
+
# - `#log_unk` – UNKNOWN
|
|
14
|
+
module LogExt
|
|
15
|
+
def log_dbg
|
|
16
|
+
Sinlog.logger.debug(self)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def log_info
|
|
20
|
+
Sinlog.logger.info(self)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def log_warn
|
|
24
|
+
Sinlog.logger.warn(self)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def log_err
|
|
28
|
+
Sinlog.logger.error(self)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def log_fatal
|
|
32
|
+
Sinlog.logger.fatal(self)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def log_unk
|
|
36
|
+
Sinlog.logger.unknown(self)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
# -----
|
|
40
|
+
private_constant :LogExt
|
|
41
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sinlog
|
|
4
|
+
# @private
|
|
5
|
+
#
|
|
6
|
+
# LogShortExt is a module that adds convenient logging methods.
|
|
7
|
+
#
|
|
8
|
+
# Similar to LogExt, but methods omit the `log_` prefix.
|
|
9
|
+
#
|
|
10
|
+
# For example:
|
|
11
|
+
# - `"msg".err` instead of `"msg".log_err`
|
|
12
|
+
# - `"msg".warn` instead of `"msg".log_warn`
|
|
13
|
+
#
|
|
14
|
+
# We can activate it with `using Sinlog::ShortRefin`
|
|
15
|
+
#
|
|
16
|
+
#
|
|
17
|
+
# == Methods
|
|
18
|
+
#
|
|
19
|
+
# - `#dbg` – DEBUG
|
|
20
|
+
# - `#info` – INFO
|
|
21
|
+
# - `#warn` – WARN
|
|
22
|
+
# - `#err` – ERROR
|
|
23
|
+
# - `#fatal` – FATAL
|
|
24
|
+
# - `#unk` – UNKNOWN
|
|
25
|
+
module LogShortExt
|
|
26
|
+
def dbg
|
|
27
|
+
Sinlog.logger.debug(self)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def info
|
|
31
|
+
Sinlog.logger.info(self)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def warn
|
|
35
|
+
Sinlog.logger.warn(self)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def err
|
|
39
|
+
Sinlog.logger.error(self)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def fatal
|
|
43
|
+
Sinlog.logger.fatal(self)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def unk
|
|
47
|
+
Sinlog.logger.unknown(self)
|
|
48
|
+
end
|
|
49
|
+
# -----
|
|
50
|
+
end
|
|
51
|
+
private_constant :LogShortExt
|
|
52
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sinlog
|
|
4
|
+
# Provides logging helpers for any object.
|
|
5
|
+
#
|
|
6
|
+
# @see Sinlog::Refin
|
|
7
|
+
# @see Sinlog::ShortMixin
|
|
8
|
+
#
|
|
9
|
+
# ## Methods
|
|
10
|
+
#
|
|
11
|
+
# - `Object#log_dbg` – DEBUG
|
|
12
|
+
# - `Object#log_info` – INFO
|
|
13
|
+
# - `Object#log_warn` – WARN
|
|
14
|
+
# - `Object#log_err` – ERROR
|
|
15
|
+
# - `Object#log_fatal` – FATAL
|
|
16
|
+
# - `Object#log_unk` – UNKNOWN
|
|
17
|
+
#
|
|
18
|
+
# @example
|
|
19
|
+
#
|
|
20
|
+
# require 'sinlog'
|
|
21
|
+
#
|
|
22
|
+
# include Sinlog::Mixin
|
|
23
|
+
#
|
|
24
|
+
# "A debug message".log_dbg
|
|
25
|
+
# "Hello, world".log_info
|
|
26
|
+
#
|
|
27
|
+
# Object.method_defined?(:log_warn) #=> true
|
|
28
|
+
#
|
|
29
|
+
module Mixin
|
|
30
|
+
def self.included(_host) = ::Object.include(LogExt)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @see Sinlog::Mixin
|
|
34
|
+
# @see Sinlog::ShortRefin
|
|
35
|
+
#
|
|
36
|
+
# @note The main difference from {Sinlog::Mixin} is that `Refin` uses Refinements instead of Mixins.
|
|
37
|
+
#
|
|
38
|
+
# - You need to activate it with `using Sinlog::Refin`
|
|
39
|
+
# - rather than `include Sinlog::Mixin`
|
|
40
|
+
#
|
|
41
|
+
# ## Source
|
|
42
|
+
#
|
|
43
|
+
# refine ::Object { import_methods LogExt }
|
|
44
|
+
#
|
|
45
|
+
# ## Methods
|
|
46
|
+
#
|
|
47
|
+
# - `#log_dbg` – DEBUG
|
|
48
|
+
# - `#log_info` – INFO
|
|
49
|
+
# - `#log_warn` – WARN
|
|
50
|
+
# - `#log_err` – ERROR
|
|
51
|
+
# - `#log_fatal` – FATAL
|
|
52
|
+
# - `#log_unk` – UNKNOWN
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
55
|
+
#
|
|
56
|
+
# require 'sinlog'
|
|
57
|
+
#
|
|
58
|
+
# module A
|
|
59
|
+
# module_function
|
|
60
|
+
# using Sinlog::Refin
|
|
61
|
+
#
|
|
62
|
+
# def demo = "Something happened".log_warn
|
|
63
|
+
# def respon_to_log_dbg? = [].respond_to? :log_dbg
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# A.demo
|
|
67
|
+
# # => [WARN] 11:17:38.024 Something happened
|
|
68
|
+
# # // (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
|
|
69
|
+
#
|
|
70
|
+
# A.respon_to_log_dbg? # => true
|
|
71
|
+
# [].respond_to? :log_dbg #=> false
|
|
72
|
+
#
|
|
73
|
+
module Refin
|
|
74
|
+
refine ::Object do
|
|
75
|
+
import_methods LogExt
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Provids convenient logging methods.
|
|
80
|
+
#
|
|
81
|
+
# Similar to {Sinlog::Refin}, but methods omit the `log_` prefix.
|
|
82
|
+
#
|
|
83
|
+
# For example:
|
|
84
|
+
#
|
|
85
|
+
# - `"msg".err` instead of `"msg".log_err`;
|
|
86
|
+
# - `"msg".warn` instead of `"msg".log_warn`
|
|
87
|
+
#
|
|
88
|
+
# ## Source
|
|
89
|
+
#
|
|
90
|
+
# refine ::Object { import_methods LogShortExt }
|
|
91
|
+
#
|
|
92
|
+
# ## Methods
|
|
93
|
+
#
|
|
94
|
+
# - `#dbg` – DEBUG
|
|
95
|
+
# - `#info` – INFO
|
|
96
|
+
# - `#warn` – WARN
|
|
97
|
+
# - `#err` – ERROR
|
|
98
|
+
# - `#fatal` – FATAL
|
|
99
|
+
# - `#unk` – UNKNOWN
|
|
100
|
+
#
|
|
101
|
+
# @see Sinlog::Refin
|
|
102
|
+
# @see Sinlog::ShortMixin
|
|
103
|
+
#
|
|
104
|
+
# @example
|
|
105
|
+
#
|
|
106
|
+
# require 'sinlog'
|
|
107
|
+
#
|
|
108
|
+
# module A
|
|
109
|
+
# module_function
|
|
110
|
+
# using Sinlog::ShortRefin
|
|
111
|
+
#
|
|
112
|
+
# def demo = "Something happened".warn
|
|
113
|
+
# def respon_to_dbg? = [].respond_to? :dbg
|
|
114
|
+
# end
|
|
115
|
+
#
|
|
116
|
+
# A.demo
|
|
117
|
+
# # => [WARN] 11:17:38.024 Something happened
|
|
118
|
+
# # // (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
|
|
119
|
+
#
|
|
120
|
+
# A.respon_to_dbg? # => true
|
|
121
|
+
# [].respond_to? :dbg #=> false
|
|
122
|
+
module ShortRefin
|
|
123
|
+
refine ::Object do
|
|
124
|
+
import_methods LogShortExt
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Provids convenient logging methods.
|
|
129
|
+
#
|
|
130
|
+
# Similar to {Sinlog::Mixin}, but methods omit the `log_` prefix.
|
|
131
|
+
#
|
|
132
|
+
# @note Since mixin monkey patching pollutes the global scope, use `include Sinlog::ShortMixin` with caution.
|
|
133
|
+
#
|
|
134
|
+
# @see Sinlog::Mixin
|
|
135
|
+
# @see Sinlog::ShortRefin
|
|
136
|
+
#
|
|
137
|
+
# ## Methods
|
|
138
|
+
#
|
|
139
|
+
# - `Object#dbg` – DEBUG
|
|
140
|
+
# - `Object#info` – INFO
|
|
141
|
+
# - `Object#warn` – WARN
|
|
142
|
+
# - `Object#err` – ERROR
|
|
143
|
+
# - `Object#fatal` – FATAL
|
|
144
|
+
# - `Object#unk` – UNKNOWN
|
|
145
|
+
#
|
|
146
|
+
# @example
|
|
147
|
+
#
|
|
148
|
+
# require 'sinlog'
|
|
149
|
+
#
|
|
150
|
+
# include Sinlog::ShortMixin
|
|
151
|
+
#
|
|
152
|
+
# "A debug message".dbg
|
|
153
|
+
# "Hello, world".info
|
|
154
|
+
#
|
|
155
|
+
# Object.method_defined?(:err) #=> true
|
|
156
|
+
#
|
|
157
|
+
module ShortMixin
|
|
158
|
+
def self.included(_host) = ::Object.include(LogShortExt)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Provides a set of reusable **unary lambdas**.
|
|
4
|
+
#
|
|
5
|
+
# @note Each function is implemented using `Kernel.lambda(&:log_xxx)`
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
#
|
|
9
|
+
# Log = Sinlog::Proc
|
|
10
|
+
# 'debug'.tap &Log.dbg
|
|
11
|
+
# 'information'.tap &Log.info
|
|
12
|
+
# 'warning'.tap &Log.warn
|
|
13
|
+
# 'error'.tap &Log.err
|
|
14
|
+
# 'fatal'.tap &Log.fatal
|
|
15
|
+
# 'unknown'.tap &Log.unk
|
|
16
|
+
#
|
|
17
|
+
module Sinlog::Proc
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
# + Object#log_*
|
|
21
|
+
using Sinlog::Refin
|
|
22
|
+
|
|
23
|
+
# Returns a lambda that calls `log_dbg` on the given object.
|
|
24
|
+
#
|
|
25
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
26
|
+
#
|
|
27
|
+
# @example Basic usage
|
|
28
|
+
#
|
|
29
|
+
# Log = Sinlog::Proc
|
|
30
|
+
#
|
|
31
|
+
# "debug message".tap &Log.dbg
|
|
32
|
+
# # OR: Log.dbg["debug message"]
|
|
33
|
+
# # OR: Log.dbg.call("debug message")
|
|
34
|
+
#
|
|
35
|
+
def dbg
|
|
36
|
+
Kernel.lambda(&:log_dbg)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Returns a lambda that calls `log_info` on the given object.
|
|
40
|
+
#
|
|
41
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
42
|
+
#
|
|
43
|
+
# @example
|
|
44
|
+
#
|
|
45
|
+
# "info message".tap &Sinlog::Proc.info
|
|
46
|
+
def info
|
|
47
|
+
Kernel.lambda(&:log_info)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Returns a lambda that calls `log_warn` on the given object.
|
|
51
|
+
#
|
|
52
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
55
|
+
#
|
|
56
|
+
# "warning message".tap &Sinlog::Proc.warn
|
|
57
|
+
def warn
|
|
58
|
+
Kernel.lambda(&:log_warn)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Returns a lambda that calls `log_err` on the given object.
|
|
62
|
+
#
|
|
63
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
64
|
+
#
|
|
65
|
+
# @example
|
|
66
|
+
#
|
|
67
|
+
# "Error message".tap &Sinlog::Proc.err
|
|
68
|
+
def err
|
|
69
|
+
Kernel.lambda(&:log_err)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Returns a lambda that calls `log_fatal` on the given object.
|
|
73
|
+
#
|
|
74
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
75
|
+
#
|
|
76
|
+
# @example
|
|
77
|
+
#
|
|
78
|
+
# "Fatal Error message".tap &Sinlog::Proc.fatal
|
|
79
|
+
def fatal
|
|
80
|
+
Kernel.lambda(&:log_fatal)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Returns a lambda that calls `log_unk` on the given object.
|
|
84
|
+
#
|
|
85
|
+
# @return [::Proc] `.call(Object)` => Boolean
|
|
86
|
+
#
|
|
87
|
+
# @example
|
|
88
|
+
#
|
|
89
|
+
# "Unknown Level".tap &Sinlog::Proc.unk
|
|
90
|
+
def unk
|
|
91
|
+
Kernel.lambda(&:log_unk)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Provides a set of reusable **unary functions** that call logging methods.
|
|
4
|
+
#
|
|
5
|
+
# - `dbg` – DEBUG
|
|
6
|
+
# - `info` – INFO
|
|
7
|
+
# - `warn` – WARN
|
|
8
|
+
# - `err` – ERROR
|
|
9
|
+
# - `fatal` – FATAL
|
|
10
|
+
# - `unk` – UNKNOWN
|
|
11
|
+
#
|
|
12
|
+
# -------------
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
#
|
|
16
|
+
# Sinlog.dbg 'debug'
|
|
17
|
+
# Sinlog.info 'information'
|
|
18
|
+
# Sinlog.warn 'warning'
|
|
19
|
+
# Sinlog.err 'error'
|
|
20
|
+
# Sinlog.fatal 'fatal'
|
|
21
|
+
# Sinlog.unk 'unknown'
|
|
22
|
+
#
|
|
23
|
+
module Sinlog
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# + Object#log_*
|
|
27
|
+
using Sinlog::Refin
|
|
28
|
+
|
|
29
|
+
# @param obj [Object]
|
|
30
|
+
# @return [Boolean]
|
|
31
|
+
#
|
|
32
|
+
# @example Basic usage
|
|
33
|
+
#
|
|
34
|
+
# Sinlog.dbg "This is a debug message"
|
|
35
|
+
#
|
|
36
|
+
def dbg(obj)
|
|
37
|
+
obj.log_dbg
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @param obj [Object]
|
|
41
|
+
# @return [Boolean]
|
|
42
|
+
#
|
|
43
|
+
# @example
|
|
44
|
+
#
|
|
45
|
+
# Sinlog.info "info message"
|
|
46
|
+
def info(obj)
|
|
47
|
+
obj.log_info
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @param obj [Object]
|
|
51
|
+
# @return [Boolean]
|
|
52
|
+
#
|
|
53
|
+
# @example
|
|
54
|
+
#
|
|
55
|
+
# Sinlog.warn "warning message"
|
|
56
|
+
def warn(obj)
|
|
57
|
+
obj.log_warn
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @param obj [Object]
|
|
61
|
+
# @return [Boolean]
|
|
62
|
+
#
|
|
63
|
+
# @example
|
|
64
|
+
#
|
|
65
|
+
# Sinlog.err "Error message"
|
|
66
|
+
def err(obj)
|
|
67
|
+
obj.log_err
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param obj [Object]
|
|
71
|
+
# @return [Boolean]
|
|
72
|
+
#
|
|
73
|
+
# @example
|
|
74
|
+
#
|
|
75
|
+
# Sinlog.fatal "Fatal Error message"
|
|
76
|
+
def fatal(obj)
|
|
77
|
+
obj.log_fatal
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @param obj [Object]
|
|
81
|
+
# @return [Boolean]
|
|
82
|
+
#
|
|
83
|
+
# @example
|
|
84
|
+
#
|
|
85
|
+
# Sinlog.unk "Unknown Level"
|
|
86
|
+
def unk(obj)
|
|
87
|
+
obj.log_unk
|
|
88
|
+
end
|
|
89
|
+
end
|
data/lib/sinlog/version.rb
CHANGED
data/lib/sinlog.rb
CHANGED
|
@@ -4,17 +4,19 @@
|
|
|
4
4
|
#
|
|
5
5
|
# The following modules provide different ways to add logging capabilities:
|
|
6
6
|
#
|
|
7
|
-
# *
|
|
8
|
-
# *
|
|
9
|
-
# *
|
|
7
|
+
# * Refin : A refinement. Activate it with `using` to add logging methods with the `log_` prefix.
|
|
8
|
+
# * ShortRefin : A refinement. Similar to `Sinlog::Refin`, but methods omit the `log_` prefix.
|
|
9
|
+
# * Mixin : A mixin module. It's a global object monkey patch, use `include Sinlog::Mixin` with caution.
|
|
10
|
+
# * ShortMixin : A mixin module. Similar to `Sinlog::Mixin`, but methods omit the `log_` prefix.
|
|
10
11
|
#
|
|
11
|
-
# === Comparison Table
|
|
12
|
+
# === Comparison Table (Monkey Patching)
|
|
12
13
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
14
|
+
# | Module | Type | Activation | Method Naming |
|
|
15
|
+
# | ---------- | ---------- | ---------- | -------------------------------------------------------- |
|
|
16
|
+
# | Mixin | Mixin | include | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
17
|
+
# | Refin | Refinement | using | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
18
|
+
# | ShortMixin | Mixin | include | dbg, info, warn, err, fatal, unk |
|
|
19
|
+
# | ShortRefin | Refinement | using | dbg, info, warn, err, fatal, unk |
|
|
18
20
|
#
|
|
19
21
|
# == Examples
|
|
20
22
|
#
|
|
@@ -29,21 +31,33 @@
|
|
|
29
31
|
# === Mixin
|
|
30
32
|
#
|
|
31
33
|
# require 'sinlog'
|
|
32
|
-
# include Sinlog::
|
|
34
|
+
# include Sinlog::Mixin
|
|
35
|
+
#
|
|
33
36
|
# "Hello".log_info
|
|
37
|
+
# "World".log_dbg
|
|
34
38
|
#
|
|
35
39
|
# === Refinement
|
|
36
40
|
#
|
|
37
41
|
# require 'sinlog'
|
|
38
|
-
#
|
|
39
|
-
#
|
|
42
|
+
#
|
|
43
|
+
# using Sinlog::Refin
|
|
44
|
+
# "Foo".log_info
|
|
45
|
+
#
|
|
46
|
+
# using Sinlog::ShortRefin
|
|
47
|
+
# "Bar".warn
|
|
40
48
|
#
|
|
41
49
|
# Read more: https://github.com/2moe/sinlog-gem
|
|
42
|
-
|
|
50
|
+
module Sinlog; end
|
|
43
51
|
|
|
44
52
|
require_relative 'sinlog/version'
|
|
45
53
|
|
|
46
|
-
require_relative 'sinlog/
|
|
47
|
-
require_relative 'sinlog/
|
|
48
|
-
require_relative 'sinlog/
|
|
49
|
-
|
|
54
|
+
require_relative 'sinlog/01_consts'
|
|
55
|
+
require_relative 'sinlog/02_logger'
|
|
56
|
+
require_relative 'sinlog/03_module_fn'
|
|
57
|
+
|
|
58
|
+
require_relative 'sinlog/04_log_ext'
|
|
59
|
+
require_relative 'sinlog/05_short_ext'
|
|
60
|
+
|
|
61
|
+
require_relative 'sinlog/06_loggable'
|
|
62
|
+
require_relative 'sinlog/07_proc'
|
|
63
|
+
require_relative 'sinlog/08_module_short_ext'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sinlog
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- 2moe
|
|
@@ -15,19 +15,19 @@ extensions: []
|
|
|
15
15
|
extra_rdoc_files: []
|
|
16
16
|
files:
|
|
17
17
|
- License
|
|
18
|
+
- docs/Changelog.md
|
|
18
19
|
- docs/Readme-zh.md
|
|
19
20
|
- docs/Readme.md
|
|
20
21
|
- lib/sinlog.rb
|
|
21
|
-
- lib/sinlog/
|
|
22
|
-
- lib/sinlog/
|
|
23
|
-
- lib/sinlog/
|
|
24
|
-
- lib/sinlog/
|
|
22
|
+
- lib/sinlog/01_consts.rb
|
|
23
|
+
- lib/sinlog/02_logger.rb
|
|
24
|
+
- lib/sinlog/03_module_fn.rb
|
|
25
|
+
- lib/sinlog/04_log_ext.rb
|
|
26
|
+
- lib/sinlog/05_short_ext.rb
|
|
27
|
+
- lib/sinlog/06_loggable.rb
|
|
28
|
+
- lib/sinlog/07_proc.rb
|
|
29
|
+
- lib/sinlog/08_module_short_ext.rb
|
|
25
30
|
- lib/sinlog/version.rb
|
|
26
|
-
- rbi/sinlog/init.rbi
|
|
27
|
-
- rbi/sinlog/log_ext.rbi
|
|
28
|
-
- rbi/sinlog/log_short_ext.rbi
|
|
29
|
-
- rbi/sinlog/loggable.rbi
|
|
30
|
-
- rbi/sinlog/version.rbi
|
|
31
31
|
homepage: https://github.com/2moe/sinlog-gem
|
|
32
32
|
licenses:
|
|
33
33
|
- MIT
|