sinlog 0.0.2 → 0.0.3
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/License +1 -1
- data/docs/Readme-zh.md +114 -100
- data/docs/Readme.md +139 -111
- data/lib/{init.rb → sinlog/init.rb} +26 -10
- data/lib/sinlog/log_ext.rb +74 -0
- data/lib/sinlog/log_short_ext.rb +81 -0
- data/lib/sinlog/loggable.rb +57 -0
- data/lib/sinlog/version.rb +1 -1
- data/lib/sinlog.rb +40 -9
- data/rbi/sinlog/init.rbi +34 -0
- data/rbi/sinlog/log_ext.rbi +37 -0
- data/rbi/sinlog/log_short_ext.rbi +37 -0
- data/rbi/sinlog/loggable.rbi +25 -0
- data/rbi/sinlog/version.rbi +3 -0
- metadata +13 -7
- data/lib/lambda.rb +0 -184
data/docs/Readme.md
CHANGED
|
@@ -4,6 +4,9 @@ A very, very simple Ruby singleton logger with colored log levels.
|
|
|
4
4
|
|
|
5
5
|
> Singleton means that the entire program will share the same instance (logger).
|
|
6
6
|
|
|
7
|
+
[](https://rubygems.org/gems/sinlog)
|
|
8
|
+
<!-- [](https://www.rubydoc.info/gems/sinlog) -->
|
|
9
|
+
|
|
7
10
|
---
|
|
8
11
|
|
|
9
12
|
| Language/語言 | ID |
|
|
@@ -12,10 +15,6 @@ A very, very simple Ruby singleton logger with colored log levels.
|
|
|
12
15
|
| [简体中文](./Readme-zh.md) | zh-Hans-CN |
|
|
13
16
|
| [繁體中文](./Readme-zh-Hant.md) | zh-Hant-TW |
|
|
14
17
|
|
|
15
|
-
> Want to support Mehr Sprachen/Más idiomas/Autres langues/Другие языки/...?
|
|
16
|
-
>
|
|
17
|
-
> Please feel free to send me an issue!
|
|
18
|
-
|
|
19
18
|
---
|
|
20
19
|
|
|
21
20
|
<details>
|
|
@@ -23,10 +22,17 @@ A very, very simple Ruby singleton logger with colored log levels.
|
|
|
23
22
|
Table of Contents (click to expand)
|
|
24
23
|
</summary>
|
|
25
24
|
|
|
26
|
-
- [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
- [Quick Start](#quick-start)
|
|
26
|
+
- [Installation](#installation)
|
|
27
|
+
- [Comparison](#comparison)
|
|
28
|
+
- [Method List](#method-list)
|
|
29
|
+
- [Loggable \& LogExt](#loggable--logext)
|
|
30
|
+
- [LogShortExt](#logshortext)
|
|
31
|
+
- [Examples](#examples)
|
|
32
|
+
- [Classic Method Call (Neither Mixin nor Refinement)](#classic-method-call-neither-mixin-nor-refinement)
|
|
33
|
+
- [Mixin](#mixin)
|
|
34
|
+
- [Refinement](#refinement)
|
|
35
|
+
- [Learn Sinlog API by Example](#learn-sinlog-api-by-example)
|
|
30
36
|
- [Classic Method Call](#classic-method-call)
|
|
31
37
|
- [Advanced](#advanced)
|
|
32
38
|
- [Real World Example](#real-world-example)
|
|
@@ -36,148 +42,150 @@ Table of Contents (click to expand)
|
|
|
36
42
|
- [Other Logger Methods](#other-logger-methods)
|
|
37
43
|
- [Notes](#notes)
|
|
38
44
|
- [Side Note](#side-note)
|
|
45
|
+
- [Changelog](#changelog)
|
|
46
|
+
- [0.0.3](#003)
|
|
39
47
|
- [License](#license)
|
|
40
48
|
|
|
41
49
|
</details>
|
|
42
50
|
|
|
43
|
-
##
|
|
51
|
+
## Quick Start
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
## Installation
|
|
46
54
|
|
|
47
55
|
```sh
|
|
56
|
+
# POSIX-sh
|
|
57
|
+
#
|
|
48
58
|
gem install sinlog
|
|
49
59
|
```
|
|
50
60
|
|
|
51
|
-
|
|
61
|
+
### Comparison
|
|
52
62
|
|
|
53
|
-
|
|
63
|
+
| Module | Type | Methods |
|
|
64
|
+
| ----------- | ---------- | -------------------------------------------- |
|
|
65
|
+
| Loggable | Mixin | `log_dbg`, `log_info`, etc. |
|
|
66
|
+
| LogExt | Refinement | `log_dbg`, `log_info`, etc. |
|
|
67
|
+
| LogShortExt | Refinement | `dbg`, `info`, `warn`, `err`, `fatal`, `unk` |
|
|
54
68
|
|
|
55
|
-
|
|
69
|
+
### Method List
|
|
56
70
|
|
|
57
|
-
|
|
71
|
+
#### Loggable & LogExt
|
|
58
72
|
|
|
59
|
-
|
|
60
|
-
|
|
73
|
+
* `log_dbg` – DEBUG
|
|
74
|
+
* `log_info` – INFO
|
|
75
|
+
* `log_warn` – WARN
|
|
76
|
+
* `log_err` – ERROR
|
|
77
|
+
* `log_fatal` – FATAL
|
|
78
|
+
* `log_unk` – UNKNOWN
|
|
61
79
|
|
|
62
|
-
|
|
63
|
-
# It provides: dbg, info, warning, err, fatal, unk
|
|
64
|
-
# We can call them using .tap(&dbg) or .then(&dbg).
|
|
80
|
+
#### LogShortExt
|
|
65
81
|
|
|
66
|
-
|
|
67
|
-
irb(main):004> 'information'.tap(&info)
|
|
82
|
+
`LogShortExt` works the same way as `LogExt`, except for method naming:
|
|
68
83
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
# If you really need warn, then call include Sinlog::LambdaWarnExt
|
|
72
|
-
irb(main):005> 'warning'.tap(&warning)
|
|
84
|
+
- `LogExt` methods use the `log_` prefix.
|
|
85
|
+
- `LogShortExt` methods do not.
|
|
73
86
|
|
|
74
|
-
|
|
75
|
-
irb(main):007> 'fatal'.tap(&fatal)
|
|
76
|
-
irb(main):008> 'unknown'.tap(&unk)
|
|
77
|
-
```
|
|
87
|
+
---
|
|
78
88
|
|
|
79
|
-
|
|
89
|
+
- `dbg` – DEBUG
|
|
90
|
+
- `info` – INFO
|
|
91
|
+
- `warn` – WARN
|
|
92
|
+
- `err` – ERROR
|
|
93
|
+
- `fatal` – FATAL
|
|
94
|
+
- `unk` – UNKNOWN
|
|
80
95
|
|
|
81
|
-
|
|
96
|
+
> ⚠️ Note: `LogShortExt` defines a `warn` method, which overrides Ruby’s built-in `warn`.
|
|
97
|
+
> If you need to call the original `warn "msg"`, use `Kernel.warn "msg"` instead.
|
|
98
|
+
>
|
|
99
|
+
> If this is a concern, use `using Sinlog::LogExt` instead of `using Sinlog::LogShortExt`.
|
|
82
100
|
|
|
83
|
-
|
|
84
|
-
- info
|
|
85
|
-
- warning
|
|
86
|
-
- wng (same as warning, just a different name)
|
|
87
|
-
- err
|
|
88
|
-
- fatal
|
|
89
|
-
- unk
|
|
101
|
+
### Examples
|
|
90
102
|
|
|
91
|
-
####
|
|
103
|
+
#### Classic Method Call (Neither Mixin nor Refinement)
|
|
92
104
|
|
|
93
|
-
|
|
94
|
-
|
|
105
|
+
```ruby
|
|
106
|
+
require 'sinlog'
|
|
95
107
|
|
|
96
|
-
|
|
97
|
-
|
|
108
|
+
log = Sinlog.logger
|
|
109
|
+
log.info "Information"
|
|
110
|
+
log.debug "This is a debug message"
|
|
111
|
+
```
|
|
98
112
|
|
|
99
|
-
|
|
113
|
+
#### Mixin
|
|
100
114
|
|
|
101
|
-
|
|
115
|
+
```ruby
|
|
116
|
+
require 'sinlog'
|
|
117
|
+
include Sinlog::Loggable
|
|
118
|
+
"Hello".log_info
|
|
119
|
+
```
|
|
102
120
|
|
|
103
|
-
|
|
121
|
+
#### Refinement
|
|
104
122
|
|
|
105
123
|
```ruby
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
irb(main):010> "debug".tap(&log_dbg)
|
|
111
|
-
irb(main):011> "information".tap(&log_info)
|
|
124
|
+
require 'sinlog'
|
|
125
|
+
using Sinlog::LogExt
|
|
126
|
+
{ dir: "/path/to/xx" }.log_info
|
|
127
|
+
```
|
|
112
128
|
|
|
113
|
-
|
|
114
|
-
irb(main):012> "warning".tap(&log_warn)
|
|
129
|
+
## Learn Sinlog API by Example
|
|
115
130
|
|
|
116
|
-
|
|
117
|
-
irb(main):014> "fatal".tap(&log_fatal)
|
|
118
|
-
irb(main):015> "unknown".tap(&log_unk)
|
|
119
|
-
```
|
|
131
|
+
<img src="../assets/img/preview.png" alt="preview">
|
|
120
132
|
|
|
121
133
|
```ruby
|
|
122
|
-
|
|
123
|
-
irb(main):016> require 'pathname'
|
|
124
|
-
|
|
125
|
-
irb(main):017> Pathname('lib/lambda.rb').tap do
|
|
126
|
-
"Filename: #{it}".then(&log_info)
|
|
127
|
-
"size: #{
|
|
128
|
-
it
|
|
129
|
-
.tap{ '⚠️ Getting file size might fail'.then(&log_warn) }
|
|
130
|
-
.size
|
|
131
|
-
}".then(&log_info)
|
|
132
|
-
end
|
|
133
|
-
```
|
|
134
|
+
require 'sinlog'
|
|
134
135
|
|
|
135
|
-
|
|
136
|
+
class A
|
|
137
|
+
using Sinlog::LogShortExt
|
|
136
138
|
|
|
137
|
-
|
|
139
|
+
def self.log
|
|
140
|
+
'Hello, this is a debug message.'.dbg
|
|
141
|
+
'Just some info.'.info
|
|
138
142
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
- log_fatal
|
|
146
|
-
- log_unk
|
|
143
|
+
'FBI, open the door!'.warn
|
|
144
|
+
{ error: "IO", type: "InvalidData" }.err
|
|
145
|
+
'Error occurred, continuing may break things.'.err
|
|
146
|
+
'Bzzzz... it is bro...ken...nnn~'.fatal
|
|
147
|
+
end
|
|
148
|
+
end
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
Sinlog::LV[:info].then do
|
|
151
|
+
Sinlog.logger_with_level it
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
A.log
|
|
155
|
+
```
|
|
149
156
|
|
|
150
|
-
|
|
157
|
+
### Classic Method Call
|
|
151
158
|
|
|
152
|
-
|
|
159
|
+
If you prefer the traditional style (`log.info(msg)` instead of `msg.info`):
|
|
153
160
|
|
|
154
161
|
```ruby
|
|
155
|
-
|
|
162
|
+
require 'sinlog'
|
|
156
163
|
|
|
157
|
-
|
|
164
|
+
log = Sinlog.logger
|
|
158
165
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
166
|
+
log.debug 'debug'
|
|
167
|
+
log.info 'information'
|
|
168
|
+
log.warn 'warning'
|
|
169
|
+
log.error 'error'
|
|
170
|
+
log.fatal 'fatal'
|
|
171
|
+
log.unknown 'unknown'
|
|
165
172
|
```
|
|
166
173
|
|
|
167
|
-
Sinlog.
|
|
174
|
+
> The data type of `Sinlog.logger` is Ruby’s standard library `Logger`.
|
|
168
175
|
|
|
169
|
-
|
|
176
|
+
In addition to the common methods listed above, you can also use other methods such as `.reopen`.
|
|
177
|
+
For details, see <https://docs.ruby-lang.org/en/3.4/Logger.html>
|
|
170
178
|
|
|
171
|
-
- debug
|
|
172
|
-
- info
|
|
173
|
-
- warn
|
|
174
|
-
- error
|
|
175
|
-
- fatal
|
|
176
|
-
- unknown
|
|
179
|
+
- `debug`
|
|
180
|
+
- `info`
|
|
181
|
+
- `warn`
|
|
182
|
+
- `error`
|
|
183
|
+
- `fatal`
|
|
184
|
+
- `unknown`
|
|
177
185
|
|
|
178
186
|
## Advanced
|
|
179
187
|
|
|
180
|
-
After trying it out ourselves, we have a basic understanding of `sinlog`.
|
|
188
|
+
After trying it out ourselves, we have a basic understanding of `sinlog`.
|
|
181
189
|
In most cases, knowing its basic usage is sufficient.
|
|
182
190
|
|
|
183
191
|
If you're interested, let's continue exploring together.
|
|
@@ -192,7 +200,7 @@ require 'sinlog'
|
|
|
192
200
|
class EpubProcessor
|
|
193
201
|
def initialize(epub_file, logger = nil)
|
|
194
202
|
@epub = epub_file
|
|
195
|
-
@logger = logger || Sinlog.instance.tap { it.
|
|
203
|
+
@logger = logger || Sinlog.instance.tap { it.set_level_from_env!("XX_LOG") }.logger
|
|
196
204
|
@logger.debug "EpubProcessor class initialization completed."
|
|
197
205
|
end
|
|
198
206
|
end
|
|
@@ -220,9 +228,11 @@ p Sinlog::LV
|
|
|
220
228
|
# => {debug: 0, info: 1, warn: 2, error: 3, fatal: 4, unknown: 5}
|
|
221
229
|
|
|
222
230
|
# Change the log level to warn
|
|
223
|
-
log = Sinlog.
|
|
224
|
-
#
|
|
225
|
-
#
|
|
231
|
+
log = Sinlog.logger_with_level(Sinlog::LV[:warn])
|
|
232
|
+
# OR:
|
|
233
|
+
# log = Sinlog.logger.tap { it.level = Sinlog::LV[:warn] }
|
|
234
|
+
# OR:
|
|
235
|
+
# log = Sinlog.instance.logger.tap { it.level = 2 }
|
|
226
236
|
|
|
227
237
|
log.error "This message will be displayed! Lower level WARN (2) will display higher level ERROR (3) logs."
|
|
228
238
|
log.info "This message will not be displayed! Higher level WARN (2) will not display lower level INFO (1) logs."
|
|
@@ -234,14 +244,14 @@ log.info "This message will not be displayed! Higher level WARN (2) will not dis
|
|
|
234
244
|
|
|
235
245
|
### Environment Variables
|
|
236
246
|
|
|
237
|
-
In the real world, for client applications, the end users are typically regular users.
|
|
247
|
+
In the real world, for client applications, the end users are typically regular users.
|
|
238
248
|
To allow them to configure `log.level` directly, we can use environment variables.
|
|
239
249
|
|
|
240
250
|
> Using environment variables is simple and efficient.
|
|
241
251
|
|
|
242
252
|
By default, Sinlog will attempt to read the value of the environment variable `RUBY_LOG`.
|
|
243
253
|
|
|
244
|
-
It essentially calls the function `
|
|
254
|
+
It essentially calls the function `set_level_from_env!(env_name = 'RUBY_LOG')`.
|
|
245
255
|
|
|
246
256
|
- If the environment variable does not exist, it uses `debug(0)`.
|
|
247
257
|
- If the environment variable exists but is empty, it uses `unknown(5)`.
|
|
@@ -265,7 +275,7 @@ export XX_CLI_LOG=info
|
|
|
265
275
|
Ruby:
|
|
266
276
|
|
|
267
277
|
```ruby
|
|
268
|
-
logger = Sinlog.instance.tap { it.
|
|
278
|
+
logger = Sinlog.instance.tap { it.set_level_from_env!("XX_CLI_LOG") }.logger
|
|
269
279
|
|
|
270
280
|
logger.debug "This message will not be displayed because the current log level is INFO(1)."
|
|
271
281
|
logger.info "Hello!"
|
|
@@ -275,11 +285,11 @@ logger.info "Hello!"
|
|
|
275
285
|
|
|
276
286
|
By default, Sinlog outputs to `STDERR`.
|
|
277
287
|
|
|
278
|
-
If you need to customize the log output path, you can call the
|
|
288
|
+
If you need to customize the log output path, you can call the Logger's `reopen` method.
|
|
279
289
|
|
|
280
290
|
```ruby
|
|
281
291
|
# Logs will be output to the file a.log
|
|
282
|
-
log = Sinlog.
|
|
292
|
+
log = Sinlog.logger.tap { it.reopen("a.log") }
|
|
283
293
|
|
|
284
294
|
log.error "What happened! QuQ"
|
|
285
295
|
```
|
|
@@ -287,7 +297,7 @@ log.error "What happened! QuQ"
|
|
|
287
297
|
OR:
|
|
288
298
|
|
|
289
299
|
```ruby
|
|
290
|
-
log = Sinlog.
|
|
300
|
+
log = Sinlog.logger
|
|
291
301
|
log.reopen("a.log")
|
|
292
302
|
|
|
293
303
|
log.error "What happened! QuQ"
|
|
@@ -305,9 +315,27 @@ Modifying Sinlog in class A of the same program will affect Sinlog in class B.
|
|
|
305
315
|
|
|
306
316
|
## Side Note
|
|
307
317
|
|
|
308
|
-
This is the first Ruby gem I have released.
|
|
318
|
+
This is the first Ruby gem I have released.
|
|
309
319
|
The API might not fully adhere to idiomatic Ruby usage, so I appreciate your understanding.
|
|
310
320
|
|
|
321
|
+
## Changelog
|
|
322
|
+
|
|
323
|
+
### 0.0.3
|
|
324
|
+
|
|
325
|
+
- `Sinlog.instance.logger` can be simplified => `Sinlog.logger`
|
|
326
|
+
|
|
327
|
+
- add `Sinlog.logger_with_level`
|
|
328
|
+
- e.g., `logger = Sinlog.logger_with_level(Sinlog::LV[:warn])`
|
|
329
|
+
- old: `Sinlog.instance.logger.tap { it.level = Sinlog::LV[:warn] }`
|
|
330
|
+
|
|
331
|
+
- add `LogExt`, `LogShortExt` and `Loggleable`
|
|
332
|
+
|
|
333
|
+
- add sorbet **.rbi** files
|
|
334
|
+
|
|
335
|
+
Breaking changes:
|
|
336
|
+
- `fetch_env_and_update_log_level(ENV_NAME)` => `set_level_from_env!(ENV_NAME)`
|
|
337
|
+
- remove `LogLambdaExt` and related modules
|
|
338
|
+
|
|
311
339
|
## License
|
|
312
340
|
|
|
313
341
|
[MIT License](../License)
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'singleton'
|
|
4
|
+
require 'logger'
|
|
5
|
+
|
|
3
6
|
# Logger Singleton Class
|
|
4
7
|
class Sinlog
|
|
5
8
|
include Singleton
|
|
@@ -27,9 +30,9 @@ class Sinlog
|
|
|
27
30
|
|
|
28
31
|
# Example:
|
|
29
32
|
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
+
# logger = Sinlog.instance.logger
|
|
34
|
+
# logger.info "Information"
|
|
35
|
+
# logger.debug "This is a debug message"
|
|
33
36
|
#
|
|
34
37
|
# The log output format will be similar to:
|
|
35
38
|
#
|
|
@@ -42,7 +45,7 @@ class Sinlog
|
|
|
42
45
|
# If this variable is not set, the default level is DEBUG.
|
|
43
46
|
def initialize
|
|
44
47
|
@logger = Logger.new($stderr)
|
|
45
|
-
|
|
48
|
+
set_level_from_env!
|
|
46
49
|
@logger.formatter = proc do |severity, datetime, progname, msg|
|
|
47
50
|
color = COLORS[severity.downcase.to_sym]
|
|
48
51
|
reset = COLORS[:unknown]
|
|
@@ -52,6 +55,20 @@ class Sinlog
|
|
|
52
55
|
end
|
|
53
56
|
end
|
|
54
57
|
|
|
58
|
+
def self.logger
|
|
59
|
+
instance.logger
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Example:
|
|
63
|
+
#
|
|
64
|
+
# require 'sinlog'
|
|
65
|
+
# dbg_logger = Sinlog.with_level(Sinlog::LV[:debug])
|
|
66
|
+
# dbg_logger.info "This is an info message"
|
|
67
|
+
# dbg_logger.debug "This is a debug message"
|
|
68
|
+
def self.logger_with_level(level = LV[:warn])
|
|
69
|
+
instance.logger.tap { _1.level = level }
|
|
70
|
+
end
|
|
71
|
+
|
|
55
72
|
# Set the `@logger.level` (**log level**) based on the value of an environment variable.
|
|
56
73
|
#
|
|
57
74
|
# If env_name is not specified, it reads the value of the `RUBY_LOG` environment variable.
|
|
@@ -64,13 +81,12 @@ class Sinlog
|
|
|
64
81
|
#
|
|
65
82
|
# Example:
|
|
66
83
|
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
# logger = Sinlog.instance.tap { it.fetch_env_and_update_log_level("XX_LOG") }.logger
|
|
84
|
+
# ENV["XX_LOG"] = "info" # or setenv in posix-sh: export XX_LOG=info
|
|
85
|
+
# logger = Sinlog.instance.tap { it.set_level_from_env!("XX_LOG") }.logger
|
|
70
86
|
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
def
|
|
87
|
+
# logger.debug "This message will not be displayed because the current log level is info"
|
|
88
|
+
# logger.info "Hello!"
|
|
89
|
+
def set_level_from_env!(env_name = 'RUBY_LOG')
|
|
74
90
|
env_lv = ENV[env_name]&.downcase&.to_sym || :debug
|
|
75
91
|
|
|
76
92
|
(LV[env_lv] || Logger::UNKNOWN)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Sinlog
|
|
4
|
+
# LogExt is a Refinement that adds convenient logging methods to all objects.
|
|
5
|
+
#
|
|
6
|
+
# When activated with `using Sinlog::LogExt`, any object can call:
|
|
7
|
+
#
|
|
8
|
+
# == Overview
|
|
9
|
+
#
|
|
10
|
+
# * `log_dbg` – Logs the object at DEBUG level
|
|
11
|
+
# * `log_info` – Logs the object at INFO level
|
|
12
|
+
# * `log_warn` – Logs the object at WARN level
|
|
13
|
+
# * `log_err` – Logs the object at ERROR level
|
|
14
|
+
# * `log_fatal` – Logs the object at FATAL level
|
|
15
|
+
# * `log_unk` – Logs the object at UNKNOWN level
|
|
16
|
+
#
|
|
17
|
+
# == Example
|
|
18
|
+
#
|
|
19
|
+
# require 'sinlog'
|
|
20
|
+
#
|
|
21
|
+
# class A
|
|
22
|
+
# using Sinlog::LogExt
|
|
23
|
+
# def demo
|
|
24
|
+
# "Something happened".log_warn
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
module LogExt
|
|
29
|
+
# Logs the current object at *debug* level using Sinlog.logger
|
|
30
|
+
refine Object do
|
|
31
|
+
def log_dbg
|
|
32
|
+
Sinlog.logger.debug(self)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Logs the current object at *information* level using Sinlog.logger
|
|
37
|
+
refine Object do
|
|
38
|
+
# logger.info
|
|
39
|
+
def log_info
|
|
40
|
+
Sinlog.logger.info(self)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Logs the current object at *warning* level using Sinlog.logger
|
|
45
|
+
refine Object do
|
|
46
|
+
def log_warn
|
|
47
|
+
Sinlog.logger.warn(self)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Logs the current object at *error* level using Sinlog.logger
|
|
52
|
+
refine Object do
|
|
53
|
+
def log_err
|
|
54
|
+
Sinlog.logger.error(self)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Logs the current object at *fatal* level using Sinlog.logger
|
|
59
|
+
refine Object do
|
|
60
|
+
def log_fatal
|
|
61
|
+
Sinlog.logger.fatal(self)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Logs the current object at *unknown* level using Sinlog.logger
|
|
66
|
+
refine Object do
|
|
67
|
+
def log_unk
|
|
68
|
+
Sinlog.logger.unknown(self)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# -----
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Sinlog
|
|
4
|
+
# LogShortExt is a Refinement that adds convenient logging methods to all objects.
|
|
5
|
+
#
|
|
6
|
+
# Similar to LogExt, but methods omit the `log_` prefix.
|
|
7
|
+
#
|
|
8
|
+
# For example:
|
|
9
|
+
# * `"msg".err` instead of `"msg".log_err`;
|
|
10
|
+
# * `"msg".warn` instead of `"msg".log_warn`
|
|
11
|
+
#
|
|
12
|
+
# When activated with `using Sinlog::LogShortExt`, any object can call:
|
|
13
|
+
#
|
|
14
|
+
# == Overview
|
|
15
|
+
#
|
|
16
|
+
# * `dbg` – Logs the object at DEBUG level
|
|
17
|
+
# * `info` – Logs the object at INFO level
|
|
18
|
+
# * `warn` – Logs the object at WARN level
|
|
19
|
+
# * `err` – Logs the object at ERROR level
|
|
20
|
+
# * `fatal` – Logs the object at FATAL level
|
|
21
|
+
# * `unk` – Logs the object at UNKNOWN level
|
|
22
|
+
#
|
|
23
|
+
# == Example
|
|
24
|
+
#
|
|
25
|
+
# require 'sinlog'
|
|
26
|
+
#
|
|
27
|
+
# class A
|
|
28
|
+
# using Sinlog::LogShortExt
|
|
29
|
+
#
|
|
30
|
+
# def demo
|
|
31
|
+
# "Something happened".warn
|
|
32
|
+
# end
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
module LogShortExt
|
|
36
|
+
# Logs the current object at *debug* level using Sinlog.logger
|
|
37
|
+
refine Object do
|
|
38
|
+
def dbg
|
|
39
|
+
Sinlog.logger.debug(self)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Logs the current object at *information* level using Sinlog.logger
|
|
44
|
+
refine Object do
|
|
45
|
+
# logger.info
|
|
46
|
+
def info
|
|
47
|
+
Sinlog.logger.info(self)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Logs the current object at *warning* level using Sinlog.logger
|
|
52
|
+
refine Object do
|
|
53
|
+
def warn
|
|
54
|
+
Sinlog.logger.warn(self)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Logs the current object at *error* level using Sinlog.logger
|
|
59
|
+
refine Object do
|
|
60
|
+
def err
|
|
61
|
+
Sinlog.logger.error(self)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Logs the current object at *fatal* level using Sinlog.logger
|
|
66
|
+
refine Object do
|
|
67
|
+
def fatal
|
|
68
|
+
Sinlog.logger.fatal(self)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Logs the current object at *unknown* level using Sinlog.logger
|
|
73
|
+
refine Object do
|
|
74
|
+
def unk
|
|
75
|
+
Sinlog.logger.unknown(self)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# -----
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Sinlog
|
|
4
|
+
# Provides logging helpers for any object.
|
|
5
|
+
#
|
|
6
|
+
# == Overview
|
|
7
|
+
#
|
|
8
|
+
# * `log_dbg` – Logs the object at DEBUG level
|
|
9
|
+
# * `log_info` – Logs the object at INFO level
|
|
10
|
+
# * `log_warn` – Logs the object at WARN level
|
|
11
|
+
# * `log_err` – Logs the object at ERROR level
|
|
12
|
+
# * `log_fatal` – Logs the object at FATAL level
|
|
13
|
+
# * `log_unk` – Logs the object at UNKNOWN level
|
|
14
|
+
#
|
|
15
|
+
# == Example
|
|
16
|
+
#
|
|
17
|
+
# require 'sinlog'
|
|
18
|
+
#
|
|
19
|
+
# include Sinlog::Loggleable
|
|
20
|
+
#
|
|
21
|
+
# "A debug message".log_dbg
|
|
22
|
+
# "Hello, world".log_info
|
|
23
|
+
#
|
|
24
|
+
module Loggleable
|
|
25
|
+
# Logs the current object at *debug* level using Sinlog.logger
|
|
26
|
+
def log_dbg
|
|
27
|
+
Sinlog.logger.debug(self)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Logs the current object at *information* level using Sinlog.logger
|
|
31
|
+
# logger.info
|
|
32
|
+
def log_info
|
|
33
|
+
Sinlog.logger.info(self)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Logs the current object at *warning* level using Sinlog.logger
|
|
37
|
+
def log_warn
|
|
38
|
+
Sinlog.logger.warn(self)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Logs the current object at *error* level using Sinlog.logger
|
|
42
|
+
def log_err
|
|
43
|
+
Sinlog.logger.error(self)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Logs the current object at *fatal* level using Sinlog.logger
|
|
47
|
+
def log_fatal
|
|
48
|
+
Sinlog.logger.fatal(self)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Logs the current object at *unknown* level using Sinlog.logger
|
|
52
|
+
def log_unk
|
|
53
|
+
Sinlog.logger.unknown(self)
|
|
54
|
+
end
|
|
55
|
+
# -----
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/sinlog/version.rb
CHANGED