sinlog 0.0.6 → 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.
@@ -3,20 +3,30 @@
3
3
  module Sinlog
4
4
  module_function
5
5
 
6
- # => Integer
6
+ # @param level [Integer, String, Symbol, nil] Log Level.
7
7
  #
8
- # == Example
8
+ # @return [Integer] the log level as an integer.
9
+ #
10
+ # @note If `level` is `nil`, returns `Sinlog.logger.level`.
11
+ #
12
+ # @example
9
13
  #
10
14
  # require 'sinlog'
11
15
  #
12
- # # values: 'debug', 'info', 'warn', 'error', 'fatal', 'unknown'
13
- # Sinlog.to_log_level('dbg') #=> LV[:debug] => 0
14
- # Sinlog.to_log_level('info') #=> LV[:info] => 1
16
+ # # optional values:
17
+ # # 'debug', 'info', 'warn', 'error', 'fatal', 'unknown',
18
+ # # 'dbg', 'information', 'warning', 'err', 'unk',
19
+ # # '0', '1', '2', '3', '4', '5'
15
20
  #
16
- def to_log_level(level) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
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?
17
25
  case level
18
26
  when 0..5
19
27
  level
28
+ when '0', '1', '2', '3', '4', '5'
29
+ level.to_i
20
30
  when 'debug', 'dbg', :dbg
21
31
  LV[:debug]
22
32
  when 'info', 'information'
@@ -36,21 +46,23 @@ module Sinlog
36
46
  end
37
47
  end
38
48
 
39
- # => Sinlog::Logger.instance
49
+ # @return [<Sinlog::Logger>] `=> Sinlog::Logger.instance`
40
50
  def instance
41
51
  Sinlog::Logger.instance
42
52
  end
43
53
 
44
- # => ::Logger
54
+ # Configures and returns the `Sinlog.instance.logger`.
45
55
  #
46
- # == English
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
47
61
  #
48
- # Creates a new ::Logger instance.
49
62
  #
50
63
  # You can configure the log level through parameters.
51
64
  #
52
65
  # - env_name
53
- # - Type: String
54
66
  # - Specifies the name of an environment variable.
55
67
  # - If set to nil, the program will attempt to read `RUBY_LOG` and set the log level accordingly.
56
68
  # - Example: logger(env_name: nil)
@@ -61,36 +73,35 @@ module Sinlog
61
73
  # - The log level will be set to `warn`.
62
74
  #
63
75
  # - level
64
- # - Type: Integer OR String OR Symbol
65
76
  # - The level parameter takes precedence over env_name.
66
77
  # - If both level and env_name are provided, the program will parse level first and return early.
67
78
  #
68
- # == 中文
79
+ # ## 中文
69
80
  #
70
- # 创建一个新的 ::Logger 实例。
81
+ # 配置并获取 `Sinlog.instance.logger`。
71
82
  #
72
83
  # 我们可以通过参数来配置日志级别。
73
84
  #
74
85
  # - env_name
75
- # - 类型: String
76
86
  # - 指定特定的环境变量名称
77
87
  # - 当其为 nil 时,程序默认会尝试获取 RUBY_LOG 的值,并设置日志级别。
78
88
  # - 假设 logger(env_name: nil)
79
89
  # - 若用户调用 `RUBY_LOG=debug ./[your-script].rb`
80
- # - 则日志级别为 debug
90
+ # - 则日志级别为 debug
81
91
  # - 假设 logger(env_name: "XX_CLI_LOG")
82
92
  # - 若用户调用 `XX_CLI_LOG=warn ./[your-script].rb`
83
- # - 则日志级别为 warn
93
+ # - 则日志级别为 warn
84
94
  # - level
85
- # - 类型: Integer OR String OR Symbol
86
95
  # - level 的优先级要高于 env_name
87
96
  # - 若 level 和 env_name 都不为 nil, 则程序会优先解析 level,并提前 return。
88
97
  #
89
- # == Example
98
+ # @see Sinlog::Logger.logger
99
+ #
100
+ # @example
90
101
  #
91
102
  # require 'sinlog'
92
103
  #
93
- # # level values:
104
+ # # optional level values:
94
105
  # # - "debug"
95
106
  # # - "dbg"
96
107
  # # - "info"
@@ -98,29 +109,34 @@ module Sinlog
98
109
  # # - "error"
99
110
  # # - "err"
100
111
  # # - "fatal"
112
+ # # - "unknown"
113
+ # # - "unk"
101
114
  # # - Integer (e.g., Sinlog::LV[:debug])
102
115
  #
103
116
  # log = Sinlog.logger(level: "dbg")
104
117
  # log.level == Sinlog::LV[:debug] #=> true
105
- # a = 3
118
+ # a = "Foo"
106
119
  # log.debug "a=#{a}"
107
120
  #
108
- # log = Sinlog.logger(level: 'warn')
121
+ # using Sinlog::Refin
122
+ # Sinlog.logger(level: 'warn')
109
123
  # log.level == Sinlog::LV[:warn] #=> true
110
- # log.error "Failed to open file."
124
+ # "Bar".log_warn
111
125
  #
112
126
  # ENV["CUSTOM_LOG"] = 'info'
113
127
  # log = Sinlog.logger(env_name: "CUSTOM_LOG")
114
128
  # log.level == Sinlog::LV[:info] #=> true
129
+ # "Baz".log_info
115
130
  #
116
- # log = Sinlog.logger(env_name: "CUSTOM_LOG", level: "error")
131
+ # log = Sinlog.logger(level: "error", env_name: "CUSTOM_LOG")
117
132
  # log.level == Sinlog::LV[:error] #=> true
133
+ # "foobar".log_err
118
134
  #
119
- def logger(env_name: nil, level: nil)
135
+ def logger(level: nil, env_name: nil)
120
136
  std_logger = instance.logger
121
137
 
122
138
  # if level != nil
123
- return std_logger.tap { _1.level = to_log_level(level) } unless level.nil?
139
+ return std_logger.tap { _1.level = as_log_level(level) } unless level.nil?
124
140
 
125
141
  # if env_name != nil
126
142
  instance.set_level_from_env!(env_name) unless env_name.nil?
@@ -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
@@ -3,16 +3,19 @@
3
3
  module Sinlog
4
4
  # Provides logging helpers for any object.
5
5
  #
6
- # == Overview
6
+ # @see Sinlog::Refin
7
+ # @see Sinlog::ShortMixin
7
8
  #
8
- # * `log_dbg` – DEBUG
9
- # * `log_info` – INFO
10
- # * `log_warn` – WARN
11
- # * `log_err` – ERROR
12
- # * `log_fatal` – FATAL
13
- # * `log_unk` – UNKNOWN
9
+ # ## Methods
14
10
  #
15
- # == Example
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
16
19
  #
17
20
  # require 'sinlog'
18
21
  #
@@ -24,31 +27,31 @@ module Sinlog
24
27
  # Object.method_defined?(:log_warn) #=> true
25
28
  #
26
29
  module Mixin
27
- def self.included(_host)
28
- ::Object.include(LogExt)
29
- end
30
+ def self.included(_host) = ::Object.include(LogExt)
30
31
  end
31
32
 
32
- # Provides logging helpers for any object.
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.
33
37
  #
34
- # - The main difference from `Sinlog::Mixin` is that `Refin` uses Refinements instead of Mixins.
35
38
  # - You need to activate it with `using Sinlog::Refin`
36
- # - rather than `include Sinlog::Mixin`.
39
+ # - rather than `include Sinlog::Mixin`
37
40
  #
38
- # == Source
41
+ # ## Source
39
42
  #
40
43
  # refine ::Object { import_methods LogExt }
41
44
  #
42
- # == Overview
45
+ # ## Methods
43
46
  #
44
- # * `log_dbg` – DEBUG
45
- # * `log_info` – INFO
46
- # * `log_warn` – WARN
47
- # * `log_err` – ERROR
48
- # * `log_fatal` – FATAL
49
- # * `log_unk` – UNKNOWN
47
+ # - `#log_dbg` – DEBUG
48
+ # - `#log_info` – INFO
49
+ # - `#log_warn` – WARN
50
+ # - `#log_err` – ERROR
51
+ # - `#log_fatal` – FATAL
52
+ # - `#log_unk` – UNKNOWN
50
53
  #
51
- # == Example
54
+ # @example
52
55
  #
53
56
  # require 'sinlog'
54
57
  #
@@ -62,7 +65,7 @@ module Sinlog
62
65
  #
63
66
  # A.demo
64
67
  # # => [WARN] 11:17:38.024 Something happened
65
- # # => (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
68
+ # # // (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
66
69
  #
67
70
  # A.respon_to_log_dbg? # => true
68
71
  # [].respond_to? :log_dbg #=> false
@@ -75,26 +78,30 @@ module Sinlog
75
78
 
76
79
  # Provids convenient logging methods.
77
80
  #
78
- # Similar to `Sinlog::Refin`, but methods omit the `log_` prefix.
81
+ # Similar to {Sinlog::Refin}, but methods omit the `log_` prefix.
79
82
  #
80
83
  # For example:
81
- # * `"msg".err` instead of `"msg".log_err`;
82
- # * `"msg".warn` instead of `"msg".log_warn`
83
84
  #
84
- # == Source
85
+ # - `"msg".err` instead of `"msg".log_err`;
86
+ # - `"msg".warn` instead of `"msg".log_warn`
87
+ #
88
+ # ## Source
85
89
  #
86
90
  # refine ::Object { import_methods LogShortExt }
87
91
  #
88
- # == Overview
92
+ # ## Methods
89
93
  #
90
- # * `dbg` – DEBUG
91
- # * `info` – INFO
92
- # * `warn` – WARN
93
- # * `err` – ERROR
94
- # * `fatal` – FATAL
95
- # * `unk` – UNKNOWN
94
+ # - `#dbg` – DEBUG
95
+ # - `#info` – INFO
96
+ # - `#warn` – WARN
97
+ # - `#err` – ERROR
98
+ # - `#fatal` – FATAL
99
+ # - `#unk` – UNKNOWN
96
100
  #
97
- # == Example
101
+ # @see Sinlog::Refin
102
+ # @see Sinlog::ShortMixin
103
+ #
104
+ # @example
98
105
  #
99
106
  # require 'sinlog'
100
107
  #
@@ -108,11 +115,10 @@ module Sinlog
108
115
  #
109
116
  # A.demo
110
117
  # # => [WARN] 11:17:38.024 Something happened
111
- # # => (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
118
+ # # // (WARN is displayed in yellow highlight; 11:17:38.024 is the current time and may vary)
112
119
  #
113
120
  # A.respon_to_dbg? # => true
114
121
  # [].respond_to? :dbg #=> false
115
- #
116
122
  module ShortRefin
117
123
  refine ::Object do
118
124
  import_methods LogShortExt
@@ -121,20 +127,23 @@ module Sinlog
121
127
 
122
128
  # Provids convenient logging methods.
123
129
  #
124
- # Similar to `Sinlog::Mixin`, but methods omit the `log_` prefix.
130
+ # Similar to {Sinlog::Mixin}, but methods omit the `log_` prefix.
125
131
  #
126
- # Note: Since mixin monkey patching pollutes the global scope, use `include Sinlog::ShortMixin` with caution.
132
+ # @note Since mixin monkey patching pollutes the global scope, use `include Sinlog::ShortMixin` with caution.
127
133
  #
128
- # == Overview
134
+ # @see Sinlog::Mixin
135
+ # @see Sinlog::ShortRefin
129
136
  #
130
- # * `dbg` – DEBUG
131
- # * `info` – INFO
132
- # * `warn` – WARN
133
- # * `err` – ERROR
134
- # * `fatal` – FATAL
135
- # * `unk` – UNKNOWN
137
+ # ## Methods
136
138
  #
137
- # == Example
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
138
147
  #
139
148
  # require 'sinlog'
140
149
  #
@@ -146,8 +155,6 @@ module Sinlog
146
155
  # Object.method_defined?(:err) #=> true
147
156
  #
148
157
  module ShortMixin
149
- def self.included(_host)
150
- ::Object.include(LogShortExt)
151
- end
158
+ def self.included(_host) = ::Object.include(LogShortExt)
152
159
  end
153
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sinlog
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.7'
5
5
  end
data/lib/sinlog.rb CHANGED
@@ -51,11 +51,13 @@ module Sinlog; end
51
51
 
52
52
  require_relative 'sinlog/version'
53
53
 
54
- require_relative 'sinlog/consts'
55
- require_relative 'sinlog/logger'
56
- require_relative 'sinlog/module_fn'
54
+ require_relative 'sinlog/01_consts'
55
+ require_relative 'sinlog/02_logger'
56
+ require_relative 'sinlog/03_module_fn'
57
57
 
58
- require_relative 'sinlog/log_ext'
59
- require_relative 'sinlog/short_ext'
58
+ require_relative 'sinlog/04_log_ext'
59
+ require_relative 'sinlog/05_short_ext'
60
60
 
61
- require_relative 'sinlog/loggable'
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - 2moe
@@ -14,19 +14,20 @@ executables: []
14
14
  extensions: []
15
15
  extra_rdoc_files: []
16
16
  files:
17
- - ".rubocop.yml"
18
17
  - License
18
+ - docs/Changelog.md
19
19
  - docs/Readme-zh.md
20
20
  - docs/Readme.md
21
21
  - lib/sinlog.rb
22
- - lib/sinlog/consts.rb
23
- - lib/sinlog/log_ext.rb
24
- - lib/sinlog/loggable.rb
25
- - lib/sinlog/logger.rb
26
- - lib/sinlog/module_fn.rb
27
- - lib/sinlog/short_ext.rb
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
28
30
  - lib/sinlog/version.rb
29
- - rbi/sinlog.rbi
30
31
  homepage: https://github.com/2moe/sinlog-gem
31
32
  licenses:
32
33
  - MIT