tty-logger 0.1.0 → 0.6.0
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/CHANGELOG.md +62 -0
- data/README.md +300 -48
- data/lib/tty/logger.rb +187 -51
- data/lib/tty/logger/config.rb +63 -5
- data/lib/tty/logger/data_filter.rb +118 -0
- data/lib/tty/logger/event.rb +2 -2
- data/lib/tty/logger/formatters/json.rb +3 -4
- data/lib/tty/logger/formatters/text.rb +7 -6
- data/lib/tty/logger/handlers/base.rb +13 -0
- data/lib/tty/logger/handlers/console.rb +37 -14
- data/lib/tty/logger/levels.rb +34 -18
- data/lib/tty/logger/version.rb +3 -3
- metadata +15 -52
- data/Rakefile +0 -8
- data/examples/console.rb +0 -22
- data/examples/error.rb +0 -11
- data/examples/handler.rb +0 -19
- data/examples/output.rb +0 -15
- data/examples/override.rb +0 -29
- data/examples/stream.rb +0 -22
- data/spec/spec_helper.rb +0 -31
- data/spec/unit/add_handler_spec.rb +0 -25
- data/spec/unit/config_spec.rb +0 -107
- data/spec/unit/event_spec.rb +0 -22
- data/spec/unit/exception_spec.rb +0 -45
- data/spec/unit/formatter_spec.rb +0 -70
- data/spec/unit/formatters/json_spec.rb +0 -41
- data/spec/unit/formatters/text_spec.rb +0 -82
- data/spec/unit/handler_spec.rb +0 -83
- data/spec/unit/handlers/custom_spec.rb +0 -26
- data/spec/unit/handlers/null_spec.rb +0 -15
- data/spec/unit/handlers/stream_spec.rb +0 -72
- data/spec/unit/levels_spec.rb +0 -40
- data/spec/unit/log_metadata_spec.rb +0 -55
- data/spec/unit/log_spec.rb +0 -144
- data/spec/unit/output_spec.rb +0 -40
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-logger.gemspec +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 708549fdd3524759b7404ed40af85915383fbd0be763133ce06b569931dd776b
|
4
|
+
data.tar.gz: 8279b0f105fb3fe7c04e82a129a898f831c48b1b9141d52532f44270ff431d0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2198bb08a4e783e13910e44934c4d4f7a4eb27c5f724f6d0911acb31f54bac546e239ed04565893ca2993a516924ebba2f6d3a2d00b8ad57e35c4c44db69ecd2
|
7
|
+
data.tar.gz: c89656676101aae012ad6d3cc236deb11fc80dc7e5026be7a64c77bb25917d40425085fb3a906b0d61436c69ef4b3e67c7fc4ad178a60718bdc347918e5327ba
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,69 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.6.0] - 2020-12-05
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add :enable_color option to control coloring in the console handler
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
* Change #add_handler to accept handler configuration options as an extra parameter
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
* Fix removing handlers by name or type
|
13
|
+
|
14
|
+
## [v0.5.0] - 2020-09-27
|
15
|
+
|
16
|
+
### Added
|
17
|
+
* Add :message_format option to customize how messages are displayed in the console
|
18
|
+
by Josh Greenwood (@JoshTGreenwood)
|
19
|
+
|
20
|
+
### Fixed
|
21
|
+
* Fix to select event name from valid log types or current level
|
22
|
+
by Ryan Schlesinger (@ryansch)
|
23
|
+
* Fix duplicate filters attribute definition in TTY::Logger::Config
|
24
|
+
|
25
|
+
## [v0.4.0] - 2020-07-29
|
26
|
+
|
27
|
+
### Added
|
28
|
+
* Allow editing logger configuration at runtime ([#10](https://github.com/piotrmurach/tty-logger/pull/10))
|
29
|
+
* Support for the `<<` streaming operator ([#9](https://github.com/piotrmurach/tty-logger/pull/9)))
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
* Change gemspec to update pastel version and restrict only to minor version
|
33
|
+
|
34
|
+
### Fixed
|
35
|
+
* Fix to filter sensitive information from exceptions
|
36
|
+
|
37
|
+
## [v0.3.0] - 2020-01-01
|
38
|
+
|
39
|
+
### Added
|
40
|
+
* Add ability to filter sensitive information out of structured data
|
41
|
+
|
42
|
+
### Changed
|
43
|
+
* Remove the test and task files from the gemspec
|
44
|
+
|
45
|
+
### Fixed
|
46
|
+
* Fix console handler highlighting of nested hash keys
|
47
|
+
|
48
|
+
## [v0.2.0] - 2019-09-30
|
49
|
+
|
50
|
+
### Added
|
51
|
+
* Add ability to add structured data inside logging block
|
52
|
+
* Add ability to filter sensitive data
|
53
|
+
* Add ability to define custom log types
|
54
|
+
* Add ability to temporarily log at different level
|
55
|
+
* Add performance tests
|
56
|
+
|
57
|
+
### Changed
|
58
|
+
* Change to dynamically define log types
|
59
|
+
|
3
60
|
## [v0.1.0] - 2019-07-21
|
4
61
|
|
5
62
|
* Initial implementation and release
|
6
63
|
|
64
|
+
[v0.6.0]: https://github.com/piotrmurach/tty-logger/compare/v0.5.0..v0.6.0
|
65
|
+
[v0.5.0]: https://github.com/piotrmurach/tty-logger/compare/v0.4.0..v0.5.0
|
66
|
+
[v0.4.0]: https://github.com/piotrmurach/tty-logger/compare/v0.3.0..v0.4.0
|
67
|
+
[v0.3.0]: https://github.com/piotrmurach/tty-logger/compare/v0.2.0..v0.3.0
|
68
|
+
[v0.2.0]: https://github.com/piotrmurach/tty-logger/compare/v0.1.0..v0.2.0
|
7
69
|
[v0.1.0]: https://github.com/piotrmurach/tty-logger/compare/v0.1.0
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
<div align="center">
|
2
|
-
<a href="https://
|
2
|
+
<a href="https://ttytoolkit.org" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
|
3
3
|
</div>
|
4
4
|
|
5
|
-
# TTY::Logger
|
5
|
+
# TTY::Logger [][gitter]
|
6
6
|
|
7
7
|
[][gem]
|
8
|
-
[][gh_actions_ci]
|
9
9
|
[][appveyor]
|
10
10
|
[][codeclimate]
|
11
11
|
[][coverage]
|
@@ -13,7 +13,7 @@
|
|
13
13
|
|
14
14
|
[gitter]: https://gitter.im/piotrmurach/tty
|
15
15
|
[gem]: http://badge.fury.io/rb/tty-logger
|
16
|
-
[
|
16
|
+
[gh_actions_ci]: https://github.com/piotrmurach/tty-logger/actions?query=workflow%3ACI
|
17
17
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-logger
|
18
18
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-logger
|
19
19
|
[coverage]: https://coveralls.io/github/piotrmurach/tty-logger
|
@@ -30,6 +30,8 @@
|
|
30
30
|
* Intuitive console output for an increased readability
|
31
31
|
* Ability to stream data to any IO object
|
32
32
|
* Supports structured data logging
|
33
|
+
* Filters sensitive data
|
34
|
+
* Allows to define custom log types
|
33
35
|
* Formats and truncates messages to avoid clogging logging output
|
34
36
|
* Customizable styling of labels and symbols for console output
|
35
37
|
* Includes metadata information: time, location, scope
|
@@ -40,7 +42,7 @@
|
|
40
42
|
Add this line to your application's Gemfile:
|
41
43
|
|
42
44
|
```ruby
|
43
|
-
gem
|
45
|
+
gem "tty-logger"
|
44
46
|
```
|
45
47
|
|
46
48
|
And then execute:
|
@@ -58,17 +60,23 @@ Or install it yourself as:
|
|
58
60
|
* [2. Synopsis](#2-synopsis)
|
59
61
|
* [2.1 Logging](#21-logging)
|
60
62
|
* [2.1.1 Exceptions](#211-exceptions)
|
63
|
+
* [2.1.2 Types](#212-types)
|
61
64
|
* [2.2 Levels](#22-levels)
|
65
|
+
* [2.2.1 Scoped Level](#22-scoped-level)
|
62
66
|
* [2.3 Structured Data](#23-structured-data)
|
63
67
|
* [2.4 Configuration](#24-configuration)
|
64
68
|
* [2.4.1 Metadata](#241-metadata)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
* [2.
|
69
|
-
* [2.
|
70
|
-
|
71
|
-
|
69
|
+
* [2.4.2 Filters](#242-filters)
|
70
|
+
* [2.5 Cloning](#25-cloning)
|
71
|
+
* [2.6 Handlers](#26-handlers)
|
72
|
+
* [2.6.1 Console Handler](#261-console-handler)
|
73
|
+
* [2.6.2 Stream Handler](#262-stream-handler)
|
74
|
+
* [2.6.3 Custom Handler](#263-custom-handler)
|
75
|
+
* [2.6.4 Multiple Handlers](#264-multiple-handlers)
|
76
|
+
* [2.7 Formatters](#27-formatters)
|
77
|
+
* [2.8 Output streams](#28-output-streams)
|
78
|
+
* [3. Community Extensions](#3-community-extensions)
|
79
|
+
* [3.1 Sentry Handler](#31-sentry-handler)
|
72
80
|
|
73
81
|
## 1. Usage
|
74
82
|
|
@@ -78,7 +86,7 @@ Create logger:
|
|
78
86
|
logger = TTY::Logger.new
|
79
87
|
```
|
80
88
|
|
81
|
-
And log information using any of the logger [types](#
|
89
|
+
And log information using any of the logger [built-in types](#212-types):
|
82
90
|
|
83
91
|
```ruby
|
84
92
|
logger.info "Deployed successfully"
|
@@ -89,7 +97,7 @@ logger.info { "Dynamically generated info" }
|
|
89
97
|
Include structured data:
|
90
98
|
|
91
99
|
```ruby
|
92
|
-
logger.
|
100
|
+
logger.success "Deployed successfully", myapp: "myapp", env: "prod"
|
93
101
|
# =>
|
94
102
|
# ✔ success Deployed successfully app=myapp env=prod
|
95
103
|
```
|
@@ -105,7 +113,7 @@ logger.info "Deployed successfully", myapp: "myapp", env: "prod"
|
|
105
113
|
# [2019-07-17] [23:21:55.287] › ℹ info Info about the deploy app=myapp env=prod
|
106
114
|
```
|
107
115
|
|
108
|
-
Or change structured data [formatting](#
|
116
|
+
Or change structured data [formatting](#27-formatters) display to `JSON`:
|
109
117
|
|
110
118
|
```ruby
|
111
119
|
logger = TTY::Logger.new do |config|
|
@@ -148,13 +156,23 @@ logger.success "Deployed", "successfully"
|
|
148
156
|
|
149
157
|
You can delay message evaluation by passing it inside a block:
|
150
158
|
|
159
|
+
```ruby
|
160
|
+
logger.success { "Dynamically generated info" }
|
161
|
+
# =>
|
162
|
+
# ✔ success Dynamically generated info
|
151
163
|
```
|
152
|
-
|
164
|
+
|
165
|
+
Similar to regular logging, you cal split your message into chunks inside a block:
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
logger.success { ["Dynamically", "generated", "info"] }
|
153
169
|
# =>
|
154
|
-
# ✔ success
|
170
|
+
# ✔ success Dynamically generated info
|
155
171
|
```
|
156
172
|
|
157
|
-
|
173
|
+
The above comes handy when paired with [structured data](#23-structured-data).
|
174
|
+
|
175
|
+
#### 2.1.1 Exceptions
|
158
176
|
|
159
177
|
You can also report on exceptions.
|
160
178
|
|
@@ -164,7 +182,7 @@ For example, let's say you caught an exception about incorrect data format and u
|
|
164
182
|
begin
|
165
183
|
raise ArgumentError, "Wrong data"
|
166
184
|
rescue => ex
|
167
|
-
logger.fatal("Error:",
|
185
|
+
logger.fatal("Error:", ex)
|
168
186
|
end
|
169
187
|
```
|
170
188
|
|
@@ -178,6 +196,51 @@ This will result in a message followed by a full backtrace:
|
|
178
196
|
# rspec-core-3.8.2/lib/rspec/core/example.rb:257:in `block in run'
|
179
197
|
```
|
180
198
|
|
199
|
+
#### 2.1.2 Types
|
200
|
+
|
201
|
+
You can define custom log types via the `types` configuration option:
|
202
|
+
|
203
|
+
For example, if you want to add `thanks` and `done` log types, you need to provide their names along with logging levels. You can further customise the `:console` output with your desired styling:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
logger = TTY::Logger.new do |config|
|
207
|
+
config.types = {
|
208
|
+
thanks: {level: :info},
|
209
|
+
done: {level: :info}
|
210
|
+
}
|
211
|
+
config.handlers = [
|
212
|
+
[:console, {
|
213
|
+
styles: {
|
214
|
+
thanks: {
|
215
|
+
symbol: "❤️ ",
|
216
|
+
label: "thanks",
|
217
|
+
color: :magenta,
|
218
|
+
levelpad: 0
|
219
|
+
},
|
220
|
+
done: {
|
221
|
+
symbol: "!!",
|
222
|
+
label: "done",
|
223
|
+
color: :green,
|
224
|
+
levelpad: 2
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}]
|
228
|
+
]
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
Once defined, you can call new log types:
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
logger.thanks("Great work!")
|
236
|
+
logger.done("Work done!")
|
237
|
+
# =>
|
238
|
+
# ❤️ thanks Great work!
|
239
|
+
# !! done Work done!
|
240
|
+
```
|
241
|
+
|
242
|
+

|
243
|
+
|
181
244
|
### 2.2 Levels
|
182
245
|
|
183
246
|
The supported levels, ordered by precedence, are:
|
@@ -192,17 +255,17 @@ So the order is: `:debug` < `:info` < `:warn` < `:error` < `:fatal`
|
|
192
255
|
|
193
256
|
For example, `:info` takes precedence over `:debug`. If your log level is set to `:info`, `:info`, `:warn`, `:error` and `:fatal` will be printed to the console. If your log level is set to `:warn`, only `:warn`, `:error` and `:fatal` will be printed.
|
194
257
|
|
195
|
-
You can set level using the `level` configuration option. The value can be a symbol, a string or level constant. For example, `:info`, `INFO` or `TTY::Logger::INFO_LEVEL` will
|
258
|
+
You can set level using the `level` configuration option. The value can be a symbol, a string or level constant. For example, `:info`, `INFO` or `TTY::Logger::INFO_LEVEL` will qualify as valid level value.
|
196
259
|
|
197
260
|
```ruby
|
198
261
|
TTY::Logger.new do |config|
|
199
|
-
config.level = :info # or "INFO"
|
262
|
+
config.level = :info # or "INFO" or TTY::Logger::INFO_LEVEL
|
200
263
|
end
|
201
264
|
```
|
202
265
|
|
203
266
|
Or you can specific level for each log events handler.
|
204
267
|
|
205
|
-
For example, to log messages above info level to a stream and only error level events to the console do:
|
268
|
+
For example, to log messages above `:info` level to a stream and only `:error` level events to the console do:
|
206
269
|
|
207
270
|
```ruby
|
208
271
|
logger = TTY::Logger.new do |config|
|
@@ -213,7 +276,34 @@ logger = TTY::Logger.new do |config|
|
|
213
276
|
end
|
214
277
|
```
|
215
278
|
|
216
|
-
You can also change the [output streams](#
|
279
|
+
You can also change the [output streams](#28-output-streams) for each handler.
|
280
|
+
|
281
|
+
#### 2.2.1 Scoped Level
|
282
|
+
|
283
|
+
You can temporarily change level, raise it or lower it by using the `log_at` call. By default messages are logged at `:info` level, but you can change this for the duration of a block:
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
logger = TTY::Logger.new
|
287
|
+
|
288
|
+
logger.info("not logged")
|
289
|
+
|
290
|
+
logger.log_at :debug do
|
291
|
+
logger.debug("logged")
|
292
|
+
end
|
293
|
+
# =>
|
294
|
+
# • debug logged
|
295
|
+
```
|
296
|
+
|
297
|
+
Or elevate a level to an error with a constant `ERROR_LEVEL`:
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
logger.log_at TTY::Logger::ERROR_LEVEL do
|
301
|
+
logger.debug("not logged")
|
302
|
+
logger.error("logged")
|
303
|
+
end
|
304
|
+
# =>
|
305
|
+
# ⨯ error logged
|
306
|
+
```
|
217
307
|
|
218
308
|
### 2.3 Structured data
|
219
309
|
|
@@ -236,16 +326,28 @@ logger.wait "Ready to deploy", app: "myapp", env: "prod"
|
|
236
326
|
# … waiting Ready to deploy app=myapp env=prod
|
237
327
|
```
|
238
328
|
|
329
|
+
You can delay data evaluation until it's evaluated by passing it inside a block:
|
330
|
+
|
331
|
+
```ruby
|
332
|
+
logger.wait { ["Ready to deploy", {app: "myapp", env: "prod"}] }
|
333
|
+
# =>
|
334
|
+
# … waiting Ready to deploy app=myapp env=prod
|
335
|
+
```
|
336
|
+
|
239
337
|
### 2.4 Configuration
|
240
338
|
|
241
|
-
All the configuration options can be changed globally via `configure` or per logger instance
|
339
|
+
All the configuration options can be changed globally via `configure` or per logger instance.
|
242
340
|
|
243
|
-
* `:
|
244
|
-
* `:
|
341
|
+
* `:filters` - the storage of placeholders to filter sensitive data out from the logs. Defaults to `{}`.
|
342
|
+
* `:formatter` - the formatter used to display structured data. Defaults to `:text`. See [Formatters](#27-formatters) for more details.
|
343
|
+
* `:handlers` - the handlers used to log messages. Defaults to `[:console]`. See [Handlers](#26-handlers) for more details.
|
245
344
|
* `:level` - the logging level. Any message logged below this level will be simply ignored. Each handler may have it's own default level. Defaults to `:info`
|
246
345
|
* `:max_bytes` - the maximum message size to be logged in bytes. Defaults to `8192` bytes. The truncated message will have `...` at the end.
|
247
346
|
* `:max_depth` - the maximum depth for nested structured data. Defaults to `3`.
|
248
347
|
* `:metadata` - the meta info to display before the message, can be `:pid`, `:date`, `:time` or `:file`. Defaults to empty array `[]`, no metadata. Setting this to `:all` will print all the metadata.
|
348
|
+
* `:types` - the new custom log types. Defaults to `{}`.
|
349
|
+
* `:date_format` - uses `strftime` format to display dates. Defaults to `"%F"`.
|
350
|
+
* `:time_format` - uses `strftime` format to display times. Defaults to `"%T.%3N"`.
|
249
351
|
|
250
352
|
For example, to configure `:max_bytes`, `:level` and `:metadata` for all logger instances do:
|
251
353
|
|
@@ -266,6 +368,14 @@ logger = TTY::Logger.new do |config|
|
|
266
368
|
end
|
267
369
|
```
|
268
370
|
|
371
|
+
You can also change the logger's configuration at runtime:
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
logger.configure do |config|
|
375
|
+
config.level = :debug
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
269
379
|
### 2.4.1 Metadata
|
270
380
|
|
271
381
|
The `:metdata` configuration option can include the following symbols:
|
@@ -275,7 +385,130 @@ The `:metdata` configuration option can include the following symbols:
|
|
275
385
|
* `:time` - the log event time
|
276
386
|
* `:file` - the file with a line number the log event is triggered from
|
277
387
|
|
278
|
-
### 2.
|
388
|
+
### 2.4.2 Filters
|
389
|
+
|
390
|
+
You can filter sensitive data out of log output with `filters` configuration option. The `filters` can be further configured to remove info from log message with `message` or structured data with `data`. Both methods, as a value accept a list of sensitive items to search for.
|
391
|
+
|
392
|
+
If you want to filter sensitive information from log messages use `message`:
|
393
|
+
|
394
|
+
```ruby
|
395
|
+
logger = TTY::Logger.new(output: output) do |config|
|
396
|
+
config.filters.message = %w[secret password]
|
397
|
+
end
|
398
|
+
```
|
399
|
+
|
400
|
+
Which by default will replace each matching string with `[FILTERED]` placeholder:
|
401
|
+
|
402
|
+
```ruby
|
403
|
+
logger.info("Super secret info with password")
|
404
|
+
# =>
|
405
|
+
# ℹ info Super [FILTERED] info with [FILTERED]
|
406
|
+
```
|
407
|
+
|
408
|
+
You can also replace each data item with a custom placeholder. To do so use a `:mask` keyword with a replacement placeholder.
|
409
|
+
|
410
|
+
For example, to replace "secret" content with placeholder `"<SECRET>"` do:
|
411
|
+
|
412
|
+
```ruby
|
413
|
+
logger = TTY::Logger.new do |config|
|
414
|
+
config.filters.message = %w[secret]
|
415
|
+
config.filters.mask = "<SECRET>"
|
416
|
+
end
|
417
|
+
```
|
418
|
+
|
419
|
+
When logged, it will produce:
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
logger.info("Super secret info")
|
423
|
+
# =>
|
424
|
+
# ℹ info Super <SECRET> info
|
425
|
+
```
|
426
|
+
|
427
|
+
To filter out sensitive information out of structured data use `data` method. By default any value matching a parameter name will be filtered regardless of the level of nesting. If you wish to filter only a specific deeply nested key use a dot notation like `params.card.password` to only filter `{params: {card: {password: "Secret123"}}}`.
|
428
|
+
|
429
|
+
For example to filter out a `:password` from data do:
|
430
|
+
|
431
|
+
```ruby
|
432
|
+
logger = TTY::Logger.new do |config|
|
433
|
+
config.filters.data = %i[password]
|
434
|
+
end
|
435
|
+
```
|
436
|
+
|
437
|
+
This will filter out any key matching password:
|
438
|
+
|
439
|
+
```ruby
|
440
|
+
logger.info("Secret info", password: "Secret123", email: "")
|
441
|
+
# =>
|
442
|
+
# ℹ info Secret info password="[FILTERED]" email="secret@example.com"
|
443
|
+
```
|
444
|
+
|
445
|
+
But also any nested data item:
|
446
|
+
|
447
|
+
```ruby
|
448
|
+
logger.info("Secret info", params: {password: "Secret123", email: ""})
|
449
|
+
# =>
|
450
|
+
# ℹ info Secret info params={password="[FILTERED]" email="secret@example.com"}
|
451
|
+
```
|
452
|
+
|
453
|
+
You're not limited to using only direct string comparison. You can also match based on regular expressions. For example, to match keys starting with `ba` we can add a following filter:
|
454
|
+
|
455
|
+
```ruby
|
456
|
+
logger = TTY::Logger.new do |config|
|
457
|
+
config.filters.data = [/ba/]
|
458
|
+
end
|
459
|
+
```
|
460
|
+
|
461
|
+
Then appropriate values will be masked:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
logger.info("Filtering data", {"foo" => {"bar" => "val", "baz" => "val"}})
|
465
|
+
# =>
|
466
|
+
# ℹ info Filtering data foo={bar="[FILTERED]" baz="[FILTERED]"}
|
467
|
+
```
|
468
|
+
|
469
|
+
You can mix and match. To filter keys based on pattern inside a deeply nested hash use dot notation with regular expression. For example, to find keys for the `:foo` parent key that starts with `:b` character, we could do:
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
logger = TTY::Logger.new do |config|
|
473
|
+
config.filters.data = [/^foo\.b/]
|
474
|
+
end
|
475
|
+
```
|
476
|
+
|
477
|
+
Then only keys under the `:foo` key will be filtered:
|
478
|
+
|
479
|
+
```ruby
|
480
|
+
logger.info("Filtering data", {"foo" => {"bar" => "val"}, "baz" => {"bar" => val"}})
|
481
|
+
# =>
|
482
|
+
# ℹ info Filtering data foo={bar="[FILTERED]"} baz={bar=val}
|
483
|
+
```
|
484
|
+
|
485
|
+
### 2.5 Cloning
|
486
|
+
|
487
|
+
You can create a copy of a logger with the current configuration using the `copy` method.
|
488
|
+
|
489
|
+
For example, given the following logger with `:app` and `:env` data:
|
490
|
+
|
491
|
+
```ruby
|
492
|
+
logger = TTY::Logger.new(fields: {app: "parent", env: "prod"})
|
493
|
+
```
|
494
|
+
|
495
|
+
We can create a copy with a custom configuration that changes filtered message content and `:app` data:
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
child_logger = logger.copy(app: "child") do |config|
|
499
|
+
config.filters = ["logging"]
|
500
|
+
end
|
501
|
+
```
|
502
|
+
|
503
|
+
```ruby
|
504
|
+
logger.info("Parent logging")
|
505
|
+
child_logger.warn("Child logging")
|
506
|
+
# =>
|
507
|
+
# ℹ info Parent logging app=parent env=prod
|
508
|
+
# ⚠ warning Child [FILTERED] app=child env=prod
|
509
|
+
```
|
510
|
+
|
511
|
+
### 2.6 Handlers
|
279
512
|
|
280
513
|
`TTY::Logger` supports many ways to handle log messages.
|
281
514
|
|
@@ -285,7 +518,7 @@ The available handlers by default are:
|
|
285
518
|
* `:null` - discards any log messages
|
286
519
|
* `:stream` - log messages to an `IO` stream, a file, a socket or a console.
|
287
520
|
|
288
|
-
You can also implement your own [custom handler](#
|
521
|
+
You can also implement your own [custom handler](#263-custom-handler).
|
289
522
|
|
290
523
|
The handlers can be configured via global or instance configuration with `handlers`. The handler can be a name or a class name:
|
291
524
|
|
@@ -311,13 +544,16 @@ logger.add_handler(:console)
|
|
311
544
|
logger.remove_handler(:console)
|
312
545
|
```
|
313
546
|
|
314
|
-
#### 2.
|
547
|
+
#### 2.6.1 Console Handler
|
315
548
|
|
316
549
|
The console handler prints log messages to the console. It supports the following options:
|
317
550
|
|
318
551
|
* `:styles` - a hash of styling options.
|
319
|
-
* `:formatter` - the formatter for log messages. Defaults to `:text
|
320
|
-
* `:output` - the device to log error messages to. Defaults to `$stderr
|
552
|
+
* `:formatter` - the formatter for log messages. Defaults to `:text`.
|
553
|
+
* `:output` - the device to log error messages to. Defaults to `$stderr`.
|
554
|
+
* `:message_format` - uses `sprintf` format to display messages. Defaults to `"%-25s"`.
|
555
|
+
* `:enable_color` - when `true` forces colored output, when `false` disables colored output.
|
556
|
+
Defaults to `nil` which performs automatic terminal color support detection.
|
321
557
|
|
322
558
|
The supported options in the `:styles` are:
|
323
559
|
|
@@ -326,9 +562,9 @@ The supported options in the `:styles` are:
|
|
326
562
|
* `:color` - the color for the log message.
|
327
563
|
* `:levelpad` - the extra amount of padding used to display log label.
|
328
564
|
|
329
|
-
See the [TTY::Logger::Handlers::Console]() for full list of styles.
|
565
|
+
See the [TTY::Logger::Handlers::Console](https://github.com/piotrmurach/tty-logger/blob/master/lib/tty/logger/handlers/console.rb) for full list of styles.
|
330
566
|
|
331
|
-
Console handler has many
|
567
|
+
Console handler has many default styles such as `success` and `error`:
|
332
568
|
|
333
569
|
```ruby
|
334
570
|
logger = TTY::Logger.new
|
@@ -339,7 +575,7 @@ logger.error("Default error")
|
|
339
575
|
# ⨯ error Default error
|
340
576
|
```
|
341
577
|
|
342
|
-
You can change
|
578
|
+
You can change the default styling with a tuple of handler name and options hash.
|
343
579
|
|
344
580
|
In our example, we want to change the styling of `success` and `error`:
|
345
581
|
|
@@ -353,7 +589,7 @@ new_styles = {
|
|
353
589
|
error: {
|
354
590
|
symbol: "!",
|
355
591
|
label: "Dooh",
|
356
|
-
levelpad: 3
|
592
|
+
levelpad: 3 # the amount of extra padding to align level names in a column
|
357
593
|
}
|
358
594
|
}
|
359
595
|
}
|
@@ -362,18 +598,27 @@ new_styles = {
|
|
362
598
|
And then use the `new_styles` when providing `handlers` configuration:
|
363
599
|
|
364
600
|
```ruby
|
365
|
-
|
366
|
-
config.handlers = [:console, new_styles]
|
601
|
+
styled_logger = TTY::Logger.new do |config|
|
602
|
+
config.handlers = [[:console, new_styles]]
|
367
603
|
end
|
368
604
|
|
369
|
-
|
370
|
-
|
605
|
+
styled_logger.success("Custom success")
|
606
|
+
styled_logger.error("Custom error")
|
371
607
|
# =>
|
372
|
-
+ Ohh yes Custom success
|
373
|
-
! Dooh Custom error
|
608
|
+
# + Ohh yes Custom success
|
609
|
+
# ! Dooh Custom error
|
374
610
|
```
|
375
611
|
|
376
|
-
|
612
|
+
To increase message padding to a percentage of terminal width (depends on [tty-screen](https://github.com/piotrmurach/tty-screen/)):
|
613
|
+
|
614
|
+
```ruby
|
615
|
+
TTY::Logger.new do |config|
|
616
|
+
padding = (TTY::Screen.columns * 0.4).to_i
|
617
|
+
config.handlers = [[:console, { message_format: "%-#{padding}s" }]]
|
618
|
+
end
|
619
|
+
```
|
620
|
+
|
621
|
+
#### 2.6.2 Stream handler
|
377
622
|
|
378
623
|
To send log event data outside of console to another service or `IO` stream, you can use `:stream` handler.
|
379
624
|
|
@@ -387,7 +632,7 @@ end
|
|
387
632
|
By default, the output will be a plain text streamed to console. The text contains key and value pairs of all the metadata and the message of the log event.
|
388
633
|
|
389
634
|
```ruby
|
390
|
-
|
635
|
+
logger.info("Info about the deploy", app:"myap", env:"prod")
|
391
636
|
# =>
|
392
637
|
# pid=18315 date="2019-07-21" time="15:42:12.463" path="examples/stream.rb:17:in`<main>`"
|
393
638
|
# level=info message="Info about the deploy" app=myapp env=prod
|
@@ -405,13 +650,13 @@ end
|
|
405
650
|
This will output JSON formatted text streamed to console.
|
406
651
|
|
407
652
|
```ruby
|
408
|
-
|
653
|
+
logger.info("Info about the deploy", app="myap", env="prod")
|
409
654
|
# =>
|
410
655
|
# {"pid":18513,"date":"2019-07-21","time":"15:54:09.924","path":"examples/stream.rb:17:in`<main>`",
|
411
656
|
# "level":"info","message":"Info about the deploy","app":"myapp","env":"prod"}
|
412
657
|
```
|
413
658
|
|
414
|
-
#### 2.
|
659
|
+
#### 2.6.3 Custom Handler
|
415
660
|
|
416
661
|
You can create your own log event handler if the default ones don't match your needs.
|
417
662
|
|
@@ -471,7 +716,7 @@ logger = TTY::Logger.new
|
|
471
716
|
logger.add_handler [MyHandler, label: "myhandler"]
|
472
717
|
```
|
473
718
|
|
474
|
-
#### 2.
|
719
|
+
#### 2.6.4 Multiple Handlers
|
475
720
|
|
476
721
|
You can define as many handlers as you need. For example, you may log messages both to console and stream:
|
477
722
|
|
@@ -492,7 +737,7 @@ logger = TTY::Logger.new do |config|
|
|
492
737
|
end
|
493
738
|
```
|
494
739
|
|
495
|
-
### 2.
|
740
|
+
### 2.7 Formatters
|
496
741
|
|
497
742
|
The available formatters are:
|
498
743
|
|
@@ -514,7 +759,8 @@ TTY::Logger.new do |config|
|
|
514
759
|
config.handlers = [:console, [:console, formatter: :json]]
|
515
760
|
end
|
516
761
|
```
|
517
|
-
|
762
|
+
|
763
|
+
### 2.8 Output Streams
|
518
764
|
|
519
765
|
By default all log events are output to `stderr`. You can change this using configuration `output` option. Any `IO`-like stream such as file, socket or console can be used. For example, to log all messages to a file do:
|
520
766
|
|
@@ -543,6 +789,12 @@ logger = TTY::Logger.new do |config|
|
|
543
789
|
end
|
544
790
|
```
|
545
791
|
|
792
|
+
## 3. Community Extensions
|
793
|
+
|
794
|
+
### 3.1 Sentry Handler
|
795
|
+
|
796
|
+
[tty-logger-raven](https://github.com/ianks/tty-logger-raven) provides an extension for Sentry.io.
|
797
|
+
|
546
798
|
## Development
|
547
799
|
|
548
800
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|