sinlog 0.0.4 → 0.0.6
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/.rubocop.yml +33 -0
- data/docs/Readme-zh.md +71 -50
- data/docs/Readme.md +95 -52
- data/lib/sinlog/consts.rb +27 -0
- data/lib/sinlog/log_ext.rb +32 -48
- data/lib/sinlog/loggable.rb +129 -33
- data/lib/sinlog/logger.rb +78 -0
- data/lib/sinlog/module_fn.rb +130 -0
- data/lib/sinlog/short_ext.rb +55 -0
- data/lib/sinlog/version.rb +2 -2
- data/lib/sinlog.rb +28 -16
- data/rbi/sinlog.rbi +95 -0
- metadata +7 -8
- data/lib/sinlog/init.rb +0 -106
- data/lib/sinlog/log_short_ext.rb +0 -69
- 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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e0d59d9821dd32f8ecb824c2b1cb3440c67c740f7787c657ca3afeba24f0d985
|
|
4
|
+
data.tar.gz: 4068166e9411404ddeb6bbade61ae85044a729f328ac82ebf8f9089cdb3be988
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 30d849c4dcabd26f801f3e12c779eb36ced2d07e5459770f956d7950a44acddfc5465a795df607235376685f24ef25c7b5cb5c3277bd49088da4abe6424862ac
|
|
7
|
+
data.tar.gz: 0cd703e33418be0d051a9ad896cf62dc94759c6edba81784836951ad99e21dadd1c40be11f91d9dc027592dd4b40c7d13cde001d28556af36e2e4c42880d3456
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# ln ~/.config/rubocop/config.yml .rubocop.yml
|
|
2
|
+
AllCops:
|
|
3
|
+
TargetRubyVersion: 3.1
|
|
4
|
+
|
|
5
|
+
Lint/MissingCopEnableDirective:
|
|
6
|
+
Enabled: false
|
|
7
|
+
Lint/RedundantCopDisableDirective:
|
|
8
|
+
Enabled: false
|
|
9
|
+
Lint/EmptyExpression:
|
|
10
|
+
# disabled => allow ()
|
|
11
|
+
Enabled: false
|
|
12
|
+
|
|
13
|
+
# https://docs.rubocop.org/rubocop/cops_style.html
|
|
14
|
+
Style/TrailingCommaInHashLiteral:
|
|
15
|
+
EnforcedStyle: diff_comma
|
|
16
|
+
Style/TrailingCommaInArrayLiteral:
|
|
17
|
+
EnforcedStyle: diff_comma
|
|
18
|
+
Style/Lambda:
|
|
19
|
+
EnforcedStyle: literal
|
|
20
|
+
Style/ModuleFunction:
|
|
21
|
+
# EnforcedStyle: extend_self
|
|
22
|
+
EnforcedStyle: module_function
|
|
23
|
+
Style/BlockDelimiters:
|
|
24
|
+
Enabled: false
|
|
25
|
+
Style/Documentation:
|
|
26
|
+
Enabled: false
|
|
27
|
+
|
|
28
|
+
# https://docs.rubocop.org/rubocop/cops_layout.html
|
|
29
|
+
Layout/CaseIndentation:
|
|
30
|
+
EnforcedStyle: end
|
|
31
|
+
IndentOneStep: true
|
|
32
|
+
Layout/MultilineMethodCallIndentation:
|
|
33
|
+
EnforcedStyle: indented_relative_to_receiver
|
data/docs/Readme-zh.md
CHANGED
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
|
|
25
25
|
- [快速上手](#快速上手)
|
|
26
26
|
- [安装](#安装)
|
|
27
|
-
- [
|
|
27
|
+
- [Monkey Patching 模块对照表](#monkey-patching-模块对照表)
|
|
28
28
|
- [方法列表](#方法列表)
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
29
|
+
- [Mixin \& Refin](#mixin--refin)
|
|
30
|
+
- [ShortMixin \& ShortRefin](#shortmixin--shortrefin)
|
|
31
31
|
- [例子](#例子)
|
|
32
32
|
- [经典方法调用 (非 mixin,亦非 refinement)](#经典方法调用-非-mixin亦非-refinement)
|
|
33
|
-
- [Mixin](#mixin)
|
|
34
33
|
- [Refinement](#refinement)
|
|
34
|
+
- [Mixin](#mixin)
|
|
35
35
|
- [Learn Sinlog API By Example](#learn-sinlog-api-by-example)
|
|
36
36
|
- [Classic Method Call](#classic-method-call)
|
|
37
37
|
- [进阶](#进阶)
|
|
@@ -56,18 +56,18 @@
|
|
|
56
56
|
gem install sinlog
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
###
|
|
60
|
-
|
|
61
|
-
| 模块 | 类型 | 方法 |
|
|
62
|
-
| ----------- | ---------- | -------------------------------------------- |
|
|
63
|
-
| Loggable | Mixin | `log_dbg`, `log_info`, etc. |
|
|
64
|
-
| LogExt | Refinement | `log_dbg`, `log_info`, etc. |
|
|
65
|
-
| LogShortExt | Refinement | `dbg`, `info`, `warn`, `err`, `fatal`, `unk` |
|
|
59
|
+
### Monkey Patching 模块对照表
|
|
66
60
|
|
|
61
|
+
| 模块 | Type | Activation | 方法 |
|
|
62
|
+
| ---------- | ---------- | ---------- | -------------------------------------------------------- |
|
|
63
|
+
| Mixin | Mixin | include | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
64
|
+
| Refin | Refinement | using | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
65
|
+
| ShortMixin | Mixin | include | dbg, info, warn, err, fatal, unk |
|
|
66
|
+
| ShortRefin | Refinement | using | dbg, info, warn, err, fatal, unk |
|
|
67
67
|
|
|
68
68
|
### 方法列表
|
|
69
69
|
|
|
70
|
-
####
|
|
70
|
+
#### Mixin & Refin
|
|
71
71
|
|
|
72
72
|
- `log_dbg` – DEBUG
|
|
73
73
|
- `log_info` – INFO
|
|
@@ -76,14 +76,17 @@ gem install sinlog
|
|
|
76
76
|
- `log_fatal` – FATAL
|
|
77
77
|
- `log_unk` – UNKNOWN
|
|
78
78
|
|
|
79
|
-
####
|
|
80
|
-
|
|
81
|
-
LogShortExt 类似于 LogExt。
|
|
79
|
+
#### ShortMixin & ShortRefin
|
|
82
80
|
|
|
83
|
-
|
|
81
|
+
- ShortRefin 类似于 Refin
|
|
82
|
+
- 除了方法的命名不同外,二者的内部实现没有任何区别。
|
|
83
|
+
- Refin 的方法带有 `log_` 前缀
|
|
84
|
+
- ShortRefin 没有
|
|
84
85
|
|
|
85
|
-
-
|
|
86
|
-
-
|
|
86
|
+
- ShortMixin 类似于 Mixin
|
|
87
|
+
- 二者的区别只是命名不同
|
|
88
|
+
- Mixin 的方法带有 `log_` 前缀
|
|
89
|
+
- ShortMixin 没有
|
|
87
90
|
|
|
88
91
|
---
|
|
89
92
|
|
|
@@ -94,10 +97,10 @@ LogShortExt 类似于 LogExt。
|
|
|
94
97
|
- `fatal` – FATAL
|
|
95
98
|
- `unk` – UNKNOWN
|
|
96
99
|
|
|
97
|
-
> ⚠️ 由于
|
|
98
|
-
> 对于 ruby 代码中的 `warn "msg"
|
|
100
|
+
> ⚠️ 由于 ShortMixin & ShortRefin 带有 warn 方法,因此会覆盖 warn。
|
|
101
|
+
> 对于 ruby 代码中的 `warn "msg"` (输出到 stderr 而非输出为日志格式),您可能需要手动将其修改为 `Kernel.warn "msg"`。
|
|
99
102
|
>
|
|
100
|
-
> 如果介意的话,那就使用 `using Sinlog::
|
|
103
|
+
> 如果介意的话,那就使用 `using Sinlog::Refin` 而不是 `using Sinlog::ShortRefin`。
|
|
101
104
|
|
|
102
105
|
### 例子
|
|
103
106
|
|
|
@@ -111,35 +114,49 @@ log.info "Information"
|
|
|
111
114
|
log.debug "This is a debug message"
|
|
112
115
|
```
|
|
113
116
|
|
|
117
|
+
#### Refinement
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
require 'sinlog'
|
|
121
|
+
using Sinlog::Refin
|
|
122
|
+
{ dir: "/path/to/xx" }.log_info
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
require 'sinlog'
|
|
127
|
+
using Sinlog::ShortRefin
|
|
128
|
+
{ dir: "/path/to/xx" }.info
|
|
129
|
+
```
|
|
130
|
+
|
|
114
131
|
#### Mixin
|
|
115
132
|
|
|
116
133
|
```ruby
|
|
117
134
|
require 'sinlog'
|
|
118
|
-
include Sinlog::
|
|
135
|
+
include Sinlog::Mixin
|
|
119
136
|
"Hello".log_info
|
|
120
137
|
```
|
|
121
|
-
#### Refinement
|
|
122
138
|
|
|
123
139
|
```ruby
|
|
124
140
|
require 'sinlog'
|
|
125
|
-
|
|
126
|
-
|
|
141
|
+
include Sinlog::ShortMixin
|
|
142
|
+
"Hello".info
|
|
127
143
|
```
|
|
128
144
|
|
|
129
145
|
## Learn Sinlog API By Example
|
|
130
146
|
|
|
131
|
-
<img src="../assets/img/预览.
|
|
147
|
+
<img src="../assets/img/预览.webp" alt="预览">
|
|
132
148
|
|
|
133
149
|
```ruby
|
|
134
150
|
require 'sinlog'
|
|
135
151
|
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
module A
|
|
153
|
+
module_function
|
|
154
|
+
using Sinlog::ShortRefin
|
|
138
155
|
|
|
139
|
-
def
|
|
140
|
-
'
|
|
141
|
-
您可能会觉得我有点啰嗦,哈哈哈!'.dbg
|
|
142
|
-
'
|
|
156
|
+
def log
|
|
157
|
+
['喂喂喂123,能看到这条调试消息吗?',
|
|
158
|
+
'您可能会觉得我有点啰嗦,哈哈哈!'].dbg
|
|
159
|
+
'神经,害我笑了一下。'.info
|
|
143
160
|
|
|
144
161
|
'开门!查水表。'.warn
|
|
145
162
|
{ error: "IO", type: "输入数据无效" }.err
|
|
@@ -148,10 +165,12 @@ class A
|
|
|
148
165
|
end
|
|
149
166
|
end
|
|
150
167
|
|
|
151
|
-
Sinlog::LV[:info].then do
|
|
152
|
-
Sinlog.logger_with_level it
|
|
153
|
-
end
|
|
154
168
|
|
|
169
|
+
A.log
|
|
170
|
+
|
|
171
|
+
# 修改日志级别为 error
|
|
172
|
+
Sinlog.logger(level: 'err')
|
|
173
|
+
Kernel.warn 'Logger.level => error'
|
|
155
174
|
A.log
|
|
156
175
|
```
|
|
157
176
|
|
|
@@ -162,7 +181,9 @@ A.log
|
|
|
162
181
|
```ruby
|
|
163
182
|
require 'sinlog'
|
|
164
183
|
|
|
165
|
-
|
|
184
|
+
ENV["CUSTOM_LOG"] = 'info'
|
|
185
|
+
|
|
186
|
+
log = Sinlog.logger(env_name: "CUSTOM_LOG")
|
|
166
187
|
|
|
167
188
|
log.debug 'debug'
|
|
168
189
|
log.info 'information'
|
|
@@ -200,7 +221,8 @@ require 'sinlog'
|
|
|
200
221
|
class EpubProcessor
|
|
201
222
|
def initialize(epub_file, logger = nil)
|
|
202
223
|
@epub = epub_file
|
|
203
|
-
|
|
224
|
+
logger ||= Sinlog::logger(env_name: "XX_LOG")
|
|
225
|
+
@logger = logger
|
|
204
226
|
@logger.debug "EpubProcessor class 初始化完成。"
|
|
205
227
|
end
|
|
206
228
|
end
|
|
@@ -223,17 +245,18 @@ end
|
|
|
223
245
|
- fatal = 4
|
|
224
246
|
- unknown = 5
|
|
225
247
|
|
|
248
|
+
> 有意思的一点是: ruby 的标准库的 logger 的日志级别与 rust 的 [log::Level](https://docs.rs/log/latest/log/enum.Level.html) 是相反的。
|
|
249
|
+
|
|
226
250
|
```ruby
|
|
227
251
|
p Sinlog::LV
|
|
228
252
|
# => {debug: 0, info: 1, warn: 2, error: 3, fatal: 4, unknown: 5}
|
|
229
253
|
|
|
230
254
|
# 将日志级别修改为 warn
|
|
231
|
-
log = Sinlog.
|
|
255
|
+
log = Sinlog.logger(level: 'warn')
|
|
232
256
|
# OR:
|
|
233
|
-
# log = Sinlog.logger
|
|
257
|
+
# log = Sinlog.logger(level: Sinlog::LV[:warn])
|
|
234
258
|
# OR:
|
|
235
|
-
# log = Sinlog.
|
|
236
|
-
|
|
259
|
+
# log = Sinlog.logger.tap { it.level = 2 }
|
|
237
260
|
|
|
238
261
|
log.error "这条消息会显示出来!低级别 WARN(2)会显示高级别 ERROR(3) 的日志。"
|
|
239
262
|
log.info "这条消息不会显示出来!高级别 WARN(2) 不会显示低级别 INFO(1) 的日志。"
|
|
@@ -250,13 +273,11 @@ log.info "这条消息不会显示出来!高级别 WARN(2) 不会显示低级
|
|
|
250
273
|
|
|
251
274
|
> 使用环境变量足够简单也足够高效。
|
|
252
275
|
|
|
253
|
-
Sinlog
|
|
254
|
-
|
|
255
|
-
本质上调用了 `set_level_from_env!(env_name = 'RUBY_LOG')` 函数。
|
|
276
|
+
`Sinlog::Logger` 在初始化时,会尝试读取环境变量 `RUBY_LOG` 的值。
|
|
256
277
|
|
|
257
278
|
- 若该环境变量不存在,则使用 debug(0)。
|
|
258
|
-
- 若该环境变量存在,且其值为空,则使用
|
|
259
|
-
- 若该环境变量的值无效,则使用
|
|
279
|
+
- 若该环境变量存在,且其值为空,则使用 error(3)。
|
|
280
|
+
- 若该环境变量的值无效,则使用 error(3)。
|
|
260
281
|
|
|
261
282
|
我们可以用 POSIX-sh 设置环境变量,然后 logger 在初始化的时候,就会自动将日志级别设置为(RUBY_LOG的值)warn。
|
|
262
283
|
|
|
@@ -276,7 +297,7 @@ export XX_CLI_LOG=info
|
|
|
276
297
|
ruby:
|
|
277
298
|
|
|
278
299
|
```ruby
|
|
279
|
-
logger = Sinlog.
|
|
300
|
+
logger = Sinlog.logger(env_name:"XX_CLI_LOG")
|
|
280
301
|
|
|
281
302
|
logger.debug "由于当前日志级别为 INFO(1),因此不会显示此消息 DEBUG(0)。"
|
|
282
303
|
logger.info "Hello!"
|
|
@@ -284,7 +305,7 @@ logger.info "Hello!"
|
|
|
284
305
|
|
|
285
306
|
### 日志输出设备/路径
|
|
286
307
|
|
|
287
|
-
默认情况下,Sinlog 会输出到 STDERR
|
|
308
|
+
默认情况下,Sinlog 会输出到 `STDERR`。
|
|
288
309
|
|
|
289
310
|
您如果需要自定义日志输出路径的话,那可以调用 logger 的 reopen 方法。
|
|
290
311
|
|
|
@@ -310,9 +331,9 @@ log.error "发生甚么事了!QuQ"
|
|
|
310
331
|
|
|
311
332
|
### 注意事项
|
|
312
333
|
|
|
313
|
-
Sinlog 用的是 Singleton 单例模式,整个程序会共享同一个实例(日志记录器)。
|
|
334
|
+
`Sinlog::Logger` 用的是 Singleton 单例模式,整个程序会共享同一个实例(日志记录器)。
|
|
314
335
|
|
|
315
|
-
在同一个程序的 class A 中修改 Sinlog 后,会影响到 class B 的 Sinlog
|
|
336
|
+
在同一个程序的 class A 中修改 `Sinlog.logger` (a.k.a. `Sinlog::Logger.instance.logger`) 后,会影响到 class B 的 `Sinlog::Logger`。
|
|
316
337
|
|
|
317
338
|
## 题外话
|
|
318
339
|
|
data/docs/Readme.md
CHANGED
|
@@ -24,14 +24,14 @@ Table of Contents (click to expand)
|
|
|
24
24
|
|
|
25
25
|
- [Quick Start](#quick-start)
|
|
26
26
|
- [Installation](#installation)
|
|
27
|
-
- [Comparison](#comparison)
|
|
27
|
+
- [Comparison Table (Monkey Patching)](#comparison-table-monkey-patching)
|
|
28
28
|
- [Method List](#method-list)
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
29
|
+
- [Mixin \& Refin](#mixin--refin)
|
|
30
|
+
- [ShortMixin \& ShortRefin](#shortmixin--shortrefin)
|
|
31
31
|
- [Examples](#examples)
|
|
32
32
|
- [Classic Method Call (Neither Mixin nor Refinement)](#classic-method-call-neither-mixin-nor-refinement)
|
|
33
|
-
- [Mixin](#mixin)
|
|
34
33
|
- [Refinement](#refinement)
|
|
34
|
+
- [Mixin](#mixin)
|
|
35
35
|
- [Learn Sinlog API by Example](#learn-sinlog-api-by-example)
|
|
36
36
|
- [Classic Method Call](#classic-method-call)
|
|
37
37
|
- [Advanced](#advanced)
|
|
@@ -44,6 +44,7 @@ Table of Contents (click to expand)
|
|
|
44
44
|
- [Side Note](#side-note)
|
|
45
45
|
- [Changelog](#changelog)
|
|
46
46
|
- [0.0.3](#003)
|
|
47
|
+
- [0.0.6](#006)
|
|
47
48
|
- [License](#license)
|
|
48
49
|
|
|
49
50
|
</details>
|
|
@@ -58,31 +59,38 @@ Table of Contents (click to expand)
|
|
|
58
59
|
gem install sinlog
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
### Comparison
|
|
62
|
+
### Comparison Table (Monkey Patching)
|
|
62
63
|
|
|
63
|
-
| Module
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
|
|
|
67
|
-
|
|
|
64
|
+
| Module | Type | Activation | Method Naming |
|
|
65
|
+
| ---------- | ---------- | ---------- | -------------------------------------------------------- |
|
|
66
|
+
| Mixin | Mixin | include | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
67
|
+
| Refin | Refinement | using | log_dbg, log_info, log_warn, log_err, log_fatal, log_unk |
|
|
68
|
+
| ShortMixin | Mixin | include | dbg, info, warn, err, fatal, unk |
|
|
69
|
+
| ShortRefin | Refinement | using | dbg, info, warn, err, fatal, unk |
|
|
68
70
|
|
|
69
71
|
### Method List
|
|
70
72
|
|
|
71
|
-
####
|
|
73
|
+
#### Mixin & Refin
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
- `log_dbg` – DEBUG
|
|
76
|
+
- `log_info` – INFO
|
|
77
|
+
- `log_warn` – WARN
|
|
78
|
+
- `log_err` – ERROR
|
|
79
|
+
- `log_fatal` – FATAL
|
|
80
|
+
- `log_unk` – UNKNOWN
|
|
79
81
|
|
|
80
|
-
####
|
|
82
|
+
#### ShortMixin & ShortRefin
|
|
81
83
|
|
|
82
|
-
`LogShortExt` works the same way as `LogExt`, except for method naming:
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
-
|
|
85
|
+
- **ShortRefin** is similar to **Refin**
|
|
86
|
+
- Apart from the difference in method naming, their internal implementations are identical.
|
|
87
|
+
- Methods in **Refin** have the `log_` prefix
|
|
88
|
+
- **ShortRefin** does not
|
|
89
|
+
|
|
90
|
+
- **ShortMixin** is similar to **Mixin**
|
|
91
|
+
- The only difference is in naming
|
|
92
|
+
- Methods in **Mixin** have the `log_` prefix
|
|
93
|
+
- **ShortMixin** does not
|
|
86
94
|
|
|
87
95
|
---
|
|
88
96
|
|
|
@@ -93,10 +101,10 @@ gem install sinlog
|
|
|
93
101
|
- `fatal` – FATAL
|
|
94
102
|
- `unk` – UNKNOWN
|
|
95
103
|
|
|
96
|
-
> ⚠️
|
|
97
|
-
>
|
|
104
|
+
> ⚠️ Since **ShortMixin** and **ShortRefin** define a `warn` method, they will override the default `warn`.
|
|
105
|
+
> For Ruby code that uses `warn "msg"` (which outputs to **stderr** rather than using a log format), you may need to manually change it to `Kernel.warn "msg"`.
|
|
98
106
|
>
|
|
99
|
-
> If this
|
|
107
|
+
> If this bothers you, then use `using Sinlog::Refin` instead of `using Sinlog::ShortRefin`.
|
|
100
108
|
|
|
101
109
|
### Examples
|
|
102
110
|
|
|
@@ -110,34 +118,48 @@ log.info "Information"
|
|
|
110
118
|
log.debug "This is a debug message"
|
|
111
119
|
```
|
|
112
120
|
|
|
121
|
+
#### Refinement
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
require 'sinlog'
|
|
125
|
+
using Sinlog::Refin
|
|
126
|
+
{ dir: "/path/to/xx" }.log_info
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
require 'sinlog'
|
|
131
|
+
using Sinlog::ShortRefin
|
|
132
|
+
{ dir: "/path/to/xx" }.info
|
|
133
|
+
```
|
|
134
|
+
|
|
113
135
|
#### Mixin
|
|
114
136
|
|
|
115
137
|
```ruby
|
|
116
138
|
require 'sinlog'
|
|
117
|
-
include Sinlog::
|
|
139
|
+
include Sinlog::Mixin
|
|
118
140
|
"Hello".log_info
|
|
119
141
|
```
|
|
120
142
|
|
|
121
|
-
#### Refinement
|
|
122
|
-
|
|
123
143
|
```ruby
|
|
124
144
|
require 'sinlog'
|
|
125
|
-
|
|
126
|
-
|
|
145
|
+
include Sinlog::ShortMixin
|
|
146
|
+
"Hello".info
|
|
127
147
|
```
|
|
128
148
|
|
|
129
149
|
## Learn Sinlog API by Example
|
|
130
150
|
|
|
131
|
-
<img src="../assets/img/preview.
|
|
151
|
+
<img src="../assets/img/preview.webp" alt="preview">
|
|
132
152
|
|
|
133
153
|
```ruby
|
|
134
154
|
require 'sinlog'
|
|
135
155
|
|
|
136
|
-
|
|
137
|
-
|
|
156
|
+
module A
|
|
157
|
+
module_function
|
|
158
|
+
using Sinlog::ShortRefin
|
|
138
159
|
|
|
139
|
-
def
|
|
140
|
-
'
|
|
160
|
+
def log
|
|
161
|
+
['Hey hey hey, could you see this debug message?',
|
|
162
|
+
'You might find it a bit verbose, hahaha!'].dbg
|
|
141
163
|
'Just some info.'.info
|
|
142
164
|
|
|
143
165
|
'FBI, open the door!'.warn
|
|
@@ -147,10 +169,11 @@ class A
|
|
|
147
169
|
end
|
|
148
170
|
end
|
|
149
171
|
|
|
150
|
-
|
|
151
|
-
Sinlog.logger_with_level it
|
|
152
|
-
end
|
|
172
|
+
A.log
|
|
153
173
|
|
|
174
|
+
# update the log level to error
|
|
175
|
+
Sinlog.logger(level: 'err')
|
|
176
|
+
Kernel.warn 'Logger.level => error'
|
|
154
177
|
A.log
|
|
155
178
|
```
|
|
156
179
|
|
|
@@ -161,7 +184,9 @@ If you prefer the traditional style (`log.info(msg)` instead of `msg.info`):
|
|
|
161
184
|
```ruby
|
|
162
185
|
require 'sinlog'
|
|
163
186
|
|
|
164
|
-
|
|
187
|
+
ENV["CUSTOM_LOG"] = 'info'
|
|
188
|
+
|
|
189
|
+
log = Sinlog.logger(env_name: "CUSTOM_LOG")
|
|
165
190
|
|
|
166
191
|
log.debug 'debug'
|
|
167
192
|
log.info 'information'
|
|
@@ -200,7 +225,8 @@ require 'sinlog'
|
|
|
200
225
|
class EpubProcessor
|
|
201
226
|
def initialize(epub_file, logger = nil)
|
|
202
227
|
@epub = epub_file
|
|
203
|
-
|
|
228
|
+
logger ||= Sinlog::logger(env_name: "XX_LOG")
|
|
229
|
+
@logger = logger
|
|
204
230
|
@logger.debug "EpubProcessor class initialization completed."
|
|
205
231
|
end
|
|
206
232
|
end
|
|
@@ -223,16 +249,18 @@ Log levels from low to high are:
|
|
|
223
249
|
- fatal = 4
|
|
224
250
|
- unknown = 5
|
|
225
251
|
|
|
252
|
+
> Interestingly, the log levels in Ruby’s standard library `Logger` are the opposite of Rust’s [log::Level](https://docs.rs/log/latest/log/enum.Level.html).
|
|
253
|
+
|
|
226
254
|
```ruby
|
|
227
255
|
p Sinlog::LV
|
|
228
256
|
# => {debug: 0, info: 1, warn: 2, error: 3, fatal: 4, unknown: 5}
|
|
229
257
|
|
|
230
258
|
# Change the log level to warn
|
|
231
|
-
log = Sinlog.
|
|
259
|
+
log = Sinlog.logger(level: 'warn')
|
|
232
260
|
# OR:
|
|
233
|
-
# log = Sinlog.logger
|
|
261
|
+
# log = Sinlog.logger(level: Sinlog::LV[:warn])
|
|
234
262
|
# OR:
|
|
235
|
-
# log = Sinlog.
|
|
263
|
+
# log = Sinlog.logger.tap { it.level = 2 }
|
|
236
264
|
|
|
237
265
|
log.error "This message will be displayed! Lower level WARN (2) will display higher level ERROR (3) logs."
|
|
238
266
|
log.info "This message will not be displayed! Higher level WARN (2) will not display lower level INFO (1) logs."
|
|
@@ -249,13 +277,11 @@ To allow them to configure `log.level` directly, we can use environment variable
|
|
|
249
277
|
|
|
250
278
|
> Using environment variables is simple and efficient.
|
|
251
279
|
|
|
252
|
-
By default, Sinlog will attempt to read the value of the environment variable `RUBY_LOG`.
|
|
253
|
-
|
|
254
|
-
It essentially calls the function `set_level_from_env!(env_name = 'RUBY_LOG')`.
|
|
280
|
+
By default, `Sinlog::Logger` will attempt to read the value of the environment variable `RUBY_LOG`.
|
|
255
281
|
|
|
256
282
|
- If the environment variable does not exist, it uses `debug(0)`.
|
|
257
|
-
- If the environment variable exists but is empty, it uses `
|
|
258
|
-
- If the environment variable's value is invalid, it uses `
|
|
283
|
+
- If the environment variable exists but is empty, it uses `error(3)`.
|
|
284
|
+
- If the environment variable's value is invalid, it uses `error(3)`.
|
|
259
285
|
|
|
260
286
|
We can set the environment variable using POSIX-sh, and then the logger will automatically set the log level to `warn` (the value of `RUBY_LOG`) during initialization.
|
|
261
287
|
|
|
@@ -275,7 +301,7 @@ export XX_CLI_LOG=info
|
|
|
275
301
|
Ruby:
|
|
276
302
|
|
|
277
303
|
```ruby
|
|
278
|
-
logger = Sinlog.
|
|
304
|
+
logger = Sinlog.logger(env_name:"XX_CLI_LOG")
|
|
279
305
|
|
|
280
306
|
logger.debug "This message will not be displayed because the current log level is INFO(1)."
|
|
281
307
|
logger.info "Hello!"
|
|
@@ -305,13 +331,13 @@ log.error "What happened! QuQ"
|
|
|
305
331
|
|
|
306
332
|
### Other Logger Methods
|
|
307
333
|
|
|
308
|
-
In addition to `.reopen` and `.level`, we can also call other methods from Ruby's standard library logger on `Sinlog.
|
|
334
|
+
In addition to `.reopen` and `.level`, we can also call other methods from Ruby's standard library logger on `Sinlog.logger`.
|
|
309
335
|
|
|
310
336
|
### Notes
|
|
311
337
|
|
|
312
|
-
Sinlog uses the Singleton pattern, meaning the entire program will share the same instance (logger).
|
|
338
|
+
`Sinlog::Logger` uses the Singleton pattern, meaning the entire program will share the same instance (logger).
|
|
313
339
|
|
|
314
|
-
Modifying Sinlog in class A of the same program will affect Sinlog in class B.
|
|
340
|
+
Modifying `Sinlog.logger` (a.k.a. `Sinlog::Logger.instance.logger`) in class A of the same program will affect `Sinlog::Logger` in class B.
|
|
315
341
|
|
|
316
342
|
## Side Note
|
|
317
343
|
|
|
@@ -328,7 +354,7 @@ The API might not fully adhere to idiomatic Ruby usage, so I appreciate your und
|
|
|
328
354
|
- e.g., `logger = Sinlog.logger_with_level(Sinlog::LV[:warn])`
|
|
329
355
|
- old: `Sinlog.instance.logger.tap { it.level = Sinlog::LV[:warn] }`
|
|
330
356
|
|
|
331
|
-
- add `LogExt`, `LogShortExt` and `
|
|
357
|
+
- add `LogExt`, `LogShortExt` and `Loggable`
|
|
332
358
|
|
|
333
359
|
- add sorbet **.rbi** files
|
|
334
360
|
|
|
@@ -336,6 +362,23 @@ Breaking changes:
|
|
|
336
362
|
- `fetch_env_and_update_log_level(ENV_NAME)` => `set_level_from_env!(ENV_NAME)`
|
|
337
363
|
- remove `LogLambdaExt` and related modules
|
|
338
364
|
|
|
365
|
+
### 0.0.6
|
|
366
|
+
|
|
367
|
+
- add `Sinlog::ShortMixin` module
|
|
368
|
+
- reimplement `Sinlog.logger` to make the API more user-friendly.
|
|
369
|
+
- We can now configure the log level via `Sinlog.logger(level: "info", env_name: "CUSTOM_ENV_LOG")`.
|
|
370
|
+
- **Note:** When both `level` and `env_name` are provided, `level` takes precedence.
|
|
371
|
+
|
|
372
|
+
Breaking changes:
|
|
373
|
+
|
|
374
|
+
- `using LogExt` => `using Sinlog::Refin`
|
|
375
|
+
- `using LogShortExt` => `using Sinlog::ShortRefin`
|
|
376
|
+
- `include Loggable` => `include Sinlog::Mixin`
|
|
377
|
+
- `Sinlog` class => `Sinlog` module
|
|
378
|
+
- private method: `Sinlog.initialize` => `Sinlog::Logger.initialize`
|
|
379
|
+
- remove `Sinlog.logger_with_level`
|
|
380
|
+
- change the default fallback log level from "unknown(5)" to "error(3)"
|
|
381
|
+
|
|
339
382
|
## License
|
|
340
383
|
|
|
341
384
|
[MIT License](../License)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'logger'
|
|
4
|
+
|
|
5
|
+
module Sinlog
|
|
6
|
+
StdLogger = ::Logger
|
|
7
|
+
|
|
8
|
+
# Define colors for different log levels
|
|
9
|
+
COLORS = {
|
|
10
|
+
debug: "\e[34m", # Blue
|
|
11
|
+
info: "\e[36m", # Cyan
|
|
12
|
+
warn: "\e[33m", # Yellow
|
|
13
|
+
error: "\e[31m", # Red
|
|
14
|
+
fatal: "\e[35m", # Magenta
|
|
15
|
+
unknown: "\e[0m" # Reset
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
# log levels
|
|
19
|
+
LV = {
|
|
20
|
+
debug: StdLogger::DEBUG,
|
|
21
|
+
info: StdLogger::INFO,
|
|
22
|
+
warn: StdLogger::WARN,
|
|
23
|
+
error: StdLogger::ERROR,
|
|
24
|
+
fatal: StdLogger::FATAL,
|
|
25
|
+
unknown: StdLogger::UNKNOWN
|
|
26
|
+
}.freeze
|
|
27
|
+
end
|