tty-logger 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +516 -1
- data/examples/console.rb +22 -0
- data/examples/error.rb +11 -0
- data/examples/handler.rb +19 -0
- data/examples/output.rb +15 -0
- data/examples/override.rb +29 -0
- data/examples/stream.rb +22 -0
- data/lib/tty/logger/config.rb +70 -0
- data/lib/tty/logger/event.rb +40 -0
- data/lib/tty/logger/formatters/json.rb +64 -0
- data/lib/tty/logger/formatters/text.rb +129 -0
- data/lib/tty/logger/handlers/base.rb +60 -0
- data/lib/tty/logger/handlers/console.rb +155 -0
- data/lib/tty/logger/handlers/null.rb +16 -0
- data/lib/tty/logger/handlers/stream.rb +63 -0
- data/lib/tty/logger/levels.rb +52 -0
- data/lib/tty/logger/version.rb +1 -1
- data/lib/tty/logger.rb +206 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/unit/add_handler_spec.rb +25 -0
- data/spec/unit/config_spec.rb +107 -0
- data/spec/unit/event_spec.rb +22 -0
- data/spec/unit/exception_spec.rb +45 -0
- data/spec/unit/formatter_spec.rb +70 -0
- data/spec/unit/formatters/json_spec.rb +41 -0
- data/spec/unit/formatters/text_spec.rb +82 -0
- data/spec/unit/handler_spec.rb +83 -0
- data/spec/unit/handlers/custom_spec.rb +26 -0
- data/spec/unit/handlers/null_spec.rb +15 -0
- data/spec/unit/handlers/stream_spec.rb +72 -0
- data/spec/unit/levels_spec.rb +40 -0
- data/spec/unit/log_metadata_spec.rb +55 -0
- data/spec/unit/log_spec.rb +140 -3
- data/spec/unit/output_spec.rb +40 -0
- data/tty-logger.gemspec +2 -0
- metadata +45 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdcb294c5428dc04d50845cb289e13bd679d78343e3557f7de4423798475c0af
|
4
|
+
data.tar.gz: 600425284bc08e004523363bdc78ee4a4b8a37b05f0930a2eef4d0b013eb7b38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 465c8e6fabae9d94f2b884715350ad3bbc1182f132d8e8b504d6b4b62a84669765d05ae60815bc79ffc83749ed43ab67a099f820708f1e1e3c3526b1de470258
|
7
|
+
data.tar.gz: d583794d9e15390b86138778cf5371a7401f1f1436af4c2fd15162dee2fdef54d9de663a96396915f5fd0c0139b1b42b8b20eda461f013903dcd6c831be6670a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,8 +4,37 @@
|
|
4
4
|
|
5
5
|
# TTY::Logger
|
6
6
|
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/tty-logger.svg)][gem]
|
8
|
+
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-logger.svg?branch=master)][travis]
|
9
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/vtrkdk0naknnxoog?svg=true)][appveyor]
|
10
|
+
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-logger/badges/gpa.svg)][codeclimate]
|
11
|
+
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-logger/badge.svg)][coverage]
|
12
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-logger.svg?branch=master)][inchpages]
|
13
|
+
|
14
|
+
[gitter]: https://gitter.im/piotrmurach/tty
|
15
|
+
[gem]: http://badge.fury.io/rb/tty-logger
|
16
|
+
[travis]: http://travis-ci.org/piotrmurach/tty-logger
|
17
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-logger
|
18
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-logger
|
19
|
+
[coverage]: https://coveralls.io/github/piotrmurach/tty-logger
|
20
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-logger
|
21
|
+
|
7
22
|
> A readable, structured and beautiful logging for the terminal
|
8
23
|
|
24
|
+
**TTY::Logger** provides independent logging component for [TTY toolkit](https://github.com/piotrmurach/tty).
|
25
|
+
|
26
|
+
![](assets/tty-logger-levels.png)
|
27
|
+
|
28
|
+
## Features
|
29
|
+
|
30
|
+
* Intuitive console output for an increased readability
|
31
|
+
* Ability to stream data to any IO object
|
32
|
+
* Supports structured data logging
|
33
|
+
* Formats and truncates messages to avoid clogging logging output
|
34
|
+
* Customizable styling of labels and symbols for console output
|
35
|
+
* Includes metadata information: time, location, scope
|
36
|
+
* Handles multiple logging outputs
|
37
|
+
|
9
38
|
## Installation
|
10
39
|
|
11
40
|
Add this line to your application's Gemfile:
|
@@ -22,10 +51,496 @@ Or install it yourself as:
|
|
22
51
|
|
23
52
|
$ gem install tty-logger
|
24
53
|
|
25
|
-
|
54
|
+
|
55
|
+
## Contents
|
56
|
+
|
57
|
+
* [1. Usage](#1-usage)
|
58
|
+
* [2. Synopsis](#2-synopsis)
|
59
|
+
* [2.1 Logging](#21-logging)
|
60
|
+
* [2.1.1 Exceptions](#211-exceptions)
|
61
|
+
* [2.2 Levels](#22-levels)
|
62
|
+
* [2.3 Structured Data](#23-structured-data)
|
63
|
+
* [2.4 Configuration](#24-configuration)
|
64
|
+
* [2.4.1 Metadata](#241-metadata)
|
65
|
+
* [2.5 Handlers](#25-handlers)
|
66
|
+
* [2.5.1 Console Handler](#251-console-handler)
|
67
|
+
* [2.5.2 Stream Handler](#252-stream-handler)
|
68
|
+
* [2.5.3 Custom Handler](#253-custom-handler)
|
69
|
+
* [2.5.4 Multiple Handlers](#254-multiple-handlers)
|
70
|
+
* [2.6 Formatters](#26-formatters)
|
71
|
+
* [2.7 Output streams](#27-output-streams)
|
72
|
+
|
73
|
+
## 1. Usage
|
74
|
+
|
75
|
+
Create logger:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
logger = TTY::Logger.new
|
79
|
+
```
|
80
|
+
|
81
|
+
And log information using any of the logger [types](#21-types):
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
logger.info "Deployed successfully"
|
85
|
+
logger.info "Deployed", "successfully"
|
86
|
+
logger.info { "Dynamically generated info" }
|
87
|
+
```
|
88
|
+
|
89
|
+
Include structured data:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
logger.info "Deployed successfully", myapp: "myapp", env: "prod"
|
93
|
+
# =>
|
94
|
+
# ✔ success Deployed successfully app=myapp env=prod
|
95
|
+
```
|
96
|
+
|
97
|
+
Add [metadata](#241-metadata) information:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
logger = TTY::Logger.new do |config|
|
101
|
+
config.metadata = [:date, :time]
|
102
|
+
end
|
103
|
+
logger.info "Deployed successfully", myapp: "myapp", env: "prod"
|
104
|
+
# =>
|
105
|
+
# [2019-07-17] [23:21:55.287] › ℹ info Info about the deploy app=myapp env=prod
|
106
|
+
```
|
107
|
+
|
108
|
+
Or change structured data [formatting](#26-formatters) display to `JSON`:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
logger = TTY::Logger.new do |config|
|
112
|
+
config.formatter = :json
|
113
|
+
end
|
114
|
+
logger.info "Deployed successfully"
|
115
|
+
# =>
|
116
|
+
# [2019-07-17] [23:21:55.287] › ℹ info Info about the deploy {"app":"myapp","env":"prod"}
|
117
|
+
```
|
118
|
+
|
119
|
+
## 2. Synopsis
|
120
|
+
|
121
|
+
## 2.1 Logging
|
122
|
+
|
123
|
+
There are many logger types to choose from:
|
124
|
+
|
125
|
+
* `debug` - logs message at `:debug` level
|
126
|
+
* `info` - logs message at `:info` level
|
127
|
+
* `success` - logs message at `:info` level
|
128
|
+
* `wait` - logs message at `:info` level
|
129
|
+
* `warn` - logs message at `:warn` level
|
130
|
+
* `error` - logs message at `:error` level
|
131
|
+
* `fatal` - logs message at `:fatal` level
|
132
|
+
|
133
|
+
To log a message, simply choose one of the above types and pass in the actual message. For example, to log successfully deployment at info level do:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
logger.success "Deployed successfully"
|
137
|
+
# =>
|
138
|
+
# ✔ success Deployed successfully
|
139
|
+
```
|
140
|
+
|
141
|
+
Or pass in multiple messages:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
logger.success "Deployed", "successfully"
|
145
|
+
# =>
|
146
|
+
# ✔ success Deployed successfully
|
147
|
+
```
|
148
|
+
|
149
|
+
You can delay message evaluation by passing it inside a block:
|
150
|
+
|
151
|
+
```
|
152
|
+
logger.info { "Dynamically generated info" }
|
153
|
+
# =>
|
154
|
+
# ✔ success Deployed successfully
|
155
|
+
```
|
156
|
+
|
157
|
+
### 2.1.1 Exceptions
|
158
|
+
|
159
|
+
You can also report on exceptions.
|
160
|
+
|
161
|
+
For example, let's say you caught an exception about incorrect data format and use `fatal` level to log it:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
begin
|
165
|
+
raise ArgumentError, "Wrong data"
|
166
|
+
rescue => ex
|
167
|
+
logger.fatal("Error:", error)
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
This will result in a message followed by a full backtrace:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
# =>
|
175
|
+
# ! fatal Error: Wrong data
|
176
|
+
# tty-logger/spec/unit/exception_spec.rb:12:in `block (2 levels) in <top (required)>'
|
177
|
+
# rspec-core-3.8.2/lib/rspec/core/example.rb:257:in `instance_exec'
|
178
|
+
# rspec-core-3.8.2/lib/rspec/core/example.rb:257:in `block in run'
|
179
|
+
```
|
180
|
+
|
181
|
+
### 2.2 Levels
|
182
|
+
|
183
|
+
The supported levels, ordered by precedence, are:
|
184
|
+
|
185
|
+
* `:debug` - for debug-related messages
|
186
|
+
* `:info` - for information of any kind
|
187
|
+
* `:warn` - for warnings
|
188
|
+
* `:error` - for errors
|
189
|
+
* `:fatal` - for fatal conditions
|
190
|
+
|
191
|
+
So the order is: `:debug` < `:info` < `:warn` < `:error` < `:fatal`
|
192
|
+
|
193
|
+
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
|
+
|
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 quality as valid level value.
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
TTY::Logger.new do |config|
|
199
|
+
config.level = :info # or "INFO" / TTY::Logger::INFO_LEVEL
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
Or you can specific level for each log events handler.
|
204
|
+
|
205
|
+
For example, to log messages above info level to a stream and only error level events to the console do:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
logger = TTY::Logger.new do |config|
|
209
|
+
config.handlers = [
|
210
|
+
[:console, level: :error],
|
211
|
+
[:stream, level: :info]
|
212
|
+
]
|
213
|
+
end
|
214
|
+
```
|
215
|
+
|
216
|
+
You can also change the [output streams](#27-output-streams) for each handler.
|
217
|
+
|
218
|
+
### 2.3 Structured data
|
219
|
+
|
220
|
+
To add global data available for all logger calls:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
224
|
+
|
225
|
+
logger.info("Deploying...")
|
226
|
+
# =>
|
227
|
+
# ℹ info Deploying... app=myapp env=prod
|
228
|
+
```
|
229
|
+
|
230
|
+
To only add data for a single log event:
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
logger = TTY::Logger.new
|
234
|
+
logger.wait "Ready to deploy", app: "myapp", env: "prod"
|
235
|
+
# =>
|
236
|
+
# … waiting Ready to deploy app=myapp env=prod
|
237
|
+
```
|
238
|
+
|
239
|
+
### 2.4 Configuration
|
240
|
+
|
241
|
+
All the configuration options can be changed globally via `configure` or per logger instance via object initialization.
|
242
|
+
|
243
|
+
* `:formatter` - the formatter used to display structured data. Defaults to `:text`. see [Formatters](#26-formatters) for more details.
|
244
|
+
* `:handlers` - the handlers used to log messages. Defaults to `[:console]`. See [Handlers](#25-handlers) for more details.
|
245
|
+
* `: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
|
+
* `:max_bytes` - the maximum message size to be logged in bytes. Defaults to `8192` bytes. The truncated message will have `...` at the end.
|
247
|
+
* `:max_depth` - the maximum depth for nested structured data. Defaults to `3`.
|
248
|
+
* `: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.
|
249
|
+
|
250
|
+
For example, to configure `:max_bytes`, `:level` and `:metadata` for all logger instances do:
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
TTY::Logger.configure do |config|
|
254
|
+
config.max_bytes = 2**10
|
255
|
+
config.level = :error
|
256
|
+
config.metadata = [:time, :date]
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
Or if you wish to setup configuration per logger instance use block:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
logger = TTY::Logger.new do |config|
|
264
|
+
config.max_bytes = 2**20
|
265
|
+
config.metadata = [:all]
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
### 2.4.1 Metadata
|
270
|
+
|
271
|
+
The `:metdata` configuration option can include the following symbols:
|
272
|
+
|
273
|
+
* `:pid` - the log event process identifier
|
274
|
+
* `:date` - the log event date
|
275
|
+
* `:time` - the log event time
|
276
|
+
* `:file` - the file with a line number the log event is triggered from
|
277
|
+
|
278
|
+
### 2.5 Handlers
|
279
|
+
|
280
|
+
`TTY::Logger` supports many ways to handle log messages.
|
281
|
+
|
282
|
+
The available handlers by default are:
|
283
|
+
|
284
|
+
* `:console` - log messages to the console, enabled by default
|
285
|
+
* `:null` - discards any log messages
|
286
|
+
* `:stream` - log messages to an `IO` stream, a file, a socket or a console.
|
287
|
+
|
288
|
+
You can also implement your own [custom handler](#253-custom-handler).
|
289
|
+
|
290
|
+
The handlers can be configured via global or instance configuration with `handlers`. The handler can be a name or a class name:
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
TTY::Logger.new do |config|
|
294
|
+
config.handlers = [:console]
|
295
|
+
end
|
296
|
+
```
|
297
|
+
|
298
|
+
Or using class name:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
TTY::Logger.new do |config|
|
302
|
+
config.handlers = [TTY::Logger::Handlers::Console]
|
303
|
+
end
|
304
|
+
```
|
305
|
+
|
306
|
+
Handlers can also be added/removed dynamically through `add_handler` or `remove_handler`.
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
logger = TTY::Logger.new
|
310
|
+
logger.add_handler(:console)
|
311
|
+
logger.remove_handler(:console)
|
312
|
+
```
|
313
|
+
|
314
|
+
#### 2.5.1 Console handler
|
315
|
+
|
316
|
+
The console handler prints log messages to the console. It supports the following options:
|
317
|
+
|
318
|
+
* `: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`
|
321
|
+
|
322
|
+
The supported options in the `:styles` are:
|
323
|
+
|
324
|
+
* `:label` - the name for the log message.
|
325
|
+
* `:symbol` - the graphics to display before the log message label.
|
326
|
+
* `:color` - the color for the log message.
|
327
|
+
* `:levelpad` - the extra amount of padding used to display log label.
|
328
|
+
|
329
|
+
See the [TTY::Logger::Handlers::Console]() for full list of styles.
|
330
|
+
|
331
|
+
Console handler has many defaults styles such as `success` and `error`:
|
26
332
|
|
27
333
|
```ruby
|
28
334
|
logger = TTY::Logger.new
|
335
|
+
logger.success("Default success")
|
336
|
+
logger.error("Default error")
|
337
|
+
# =>
|
338
|
+
# ✔ success Default success
|
339
|
+
# ⨯ error Default error
|
340
|
+
```
|
341
|
+
|
342
|
+
You can change console handler default style with a tuple of handler name and options hash.
|
343
|
+
|
344
|
+
In our example, we want to change the styling of `success` and `error`:
|
345
|
+
|
346
|
+
```ruby
|
347
|
+
new_styles = {
|
348
|
+
styles: {
|
349
|
+
success: {
|
350
|
+
symbol: "+",
|
351
|
+
label: "Ohh yes"
|
352
|
+
},
|
353
|
+
error: {
|
354
|
+
symbol: "!",
|
355
|
+
label: "Dooh",
|
356
|
+
levelpad: 3
|
357
|
+
}
|
358
|
+
}
|
359
|
+
}
|
360
|
+
```
|
361
|
+
|
362
|
+
And then use the `new_styles` when providing `handlers` configuration:
|
363
|
+
|
364
|
+
```ruby
|
365
|
+
new_style = TTY::Logger.new do |config|
|
366
|
+
config.handlers = [:console, new_styles]
|
367
|
+
end
|
368
|
+
|
369
|
+
new_style.success("Custom success")
|
370
|
+
new_style.error("Custom error")
|
371
|
+
# =>
|
372
|
+
+ Ohh yes Custom success
|
373
|
+
! Dooh Custom error
|
374
|
+
```
|
375
|
+
|
376
|
+
#### 2.5.2 Stream handler
|
377
|
+
|
378
|
+
To send log event data outside of console to another service or `IO` stream, you can use `:stream` handler.
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
logger = TTY::Logger.new(output: output) do |config|
|
382
|
+
config.handlers = [:stream]
|
383
|
+
config.metadata = [:all]
|
384
|
+
end
|
385
|
+
```
|
386
|
+
|
387
|
+
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
|
+
|
389
|
+
```ruby
|
390
|
+
loggger.info("Info about the deploy", app="myap", env="prod")
|
391
|
+
# =>
|
392
|
+
# pid=18315 date="2019-07-21" time="15:42:12.463" path="examples/stream.rb:17:in`<main>`"
|
393
|
+
# level=info message="Info about the deploy" app=myapp env=prod
|
394
|
+
```
|
395
|
+
|
396
|
+
You can change stream formatter for ease of working with external services such as `Logstash`. For example, to use `:stream` handler with `:json` format do:
|
397
|
+
|
398
|
+
```ruby
|
399
|
+
logger = TTY::Logger.new(output: output) do |config|
|
400
|
+
config.handlers = [[:stream, formatter: :json]]
|
401
|
+
config.metadata = [:all]
|
402
|
+
end
|
403
|
+
```
|
404
|
+
|
405
|
+
This will output JSON formatted text streamed to console.
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
loggger.info("Info about the deploy", app="myap", env="prod")
|
409
|
+
# =>
|
410
|
+
# {"pid":18513,"date":"2019-07-21","time":"15:54:09.924","path":"examples/stream.rb:17:in`<main>`",
|
411
|
+
# "level":"info","message":"Info about the deploy","app":"myapp","env":"prod"}
|
412
|
+
```
|
413
|
+
|
414
|
+
#### 2.5.3 Custom Handler
|
415
|
+
|
416
|
+
You can create your own log event handler if the default ones don't match your needs.
|
417
|
+
|
418
|
+
The design of your handler should include two calls:
|
419
|
+
|
420
|
+
* `initialize` - where all dependencies get injected
|
421
|
+
* `call` - where the log event is handled
|
422
|
+
|
423
|
+
We start with the implementation of the `initialize` method. This method by default is injected with `:config` key that includes all global configuration options. The `:output` key for displaying log message in the console and `:formatter`.
|
424
|
+
|
425
|
+
In our case we also add custom `:label`:
|
426
|
+
|
427
|
+
```ruby
|
428
|
+
class MyHandler
|
429
|
+
def initialize(output: nil, config: nil, formatter: nil, label: nil)
|
430
|
+
@label = label
|
431
|
+
@output = output
|
432
|
+
end
|
433
|
+
end
|
434
|
+
```
|
435
|
+
|
436
|
+
Next is the `call` method that accepts the log `event`.
|
437
|
+
|
438
|
+
The `event` has the following attributes:
|
439
|
+
|
440
|
+
* `message` - the array of message parts to be printed
|
441
|
+
* `fields` - the structured data supplied with the event
|
442
|
+
* `metadata` - the additional info about the event. See [metadata](#241-metadata) section for details.
|
443
|
+
|
444
|
+
We add implementation of `call`:
|
445
|
+
|
446
|
+
```ruby
|
447
|
+
class MyHandler
|
448
|
+
def initialize(output: nil, config: nil, label: nil)
|
449
|
+
@label = label
|
450
|
+
@output = output
|
451
|
+
end
|
452
|
+
|
453
|
+
def call(event)
|
454
|
+
@output.puts "(#{@label}) #{event.message.join}"
|
455
|
+
end
|
456
|
+
end
|
457
|
+
```
|
458
|
+
|
459
|
+
Once you have your custom handler, you need to register it with the logger. You can do so using the `handlers` configuration option:
|
460
|
+
|
461
|
+
```ruby
|
462
|
+
logger = TTY::Logger.new do |config|
|
463
|
+
config.handlers = [[MyHandler, label: "myhandler"]]
|
464
|
+
end
|
465
|
+
```
|
466
|
+
|
467
|
+
Or add your handler dynamically after logger initialization:
|
468
|
+
|
469
|
+
```ruby
|
470
|
+
logger = TTY::Logger.new
|
471
|
+
logger.add_handler [MyHandler, label: "myhandler"]
|
472
|
+
```
|
473
|
+
|
474
|
+
#### 2.5.4 Multiple Handlers
|
475
|
+
|
476
|
+
You can define as many handlers as you need. For example, you may log messages both to console and stream:
|
477
|
+
|
478
|
+
```ruby
|
479
|
+
logger = TTY::Logger.new do |config|
|
480
|
+
config.handlers = [:console, :stream]
|
481
|
+
end
|
482
|
+
```
|
483
|
+
|
484
|
+
Each handler can have its own configuration. For example, you can register `:console` handler to log messages above error level and `:stream` that logs any message with info or higher level:
|
485
|
+
|
486
|
+
```ruby
|
487
|
+
logger = TTY::Logger.new do |config|
|
488
|
+
config.handlers = [
|
489
|
+
[:console, level: :error],
|
490
|
+
[:stream, level: :info]
|
491
|
+
]
|
492
|
+
end
|
493
|
+
```
|
494
|
+
|
495
|
+
### 2.6 Formatters
|
496
|
+
|
497
|
+
The available formatters are:
|
498
|
+
|
499
|
+
* `:json`
|
500
|
+
* `:text`
|
501
|
+
|
502
|
+
You can configure format for all the handlers:
|
503
|
+
|
504
|
+
```ruby
|
505
|
+
TTY::Logger.new do |config|
|
506
|
+
config.formatter = :json
|
507
|
+
end
|
508
|
+
```
|
509
|
+
|
510
|
+
Or specify a different formatter for each handler. For example, let's say you want to log to console twice, once with default formatter and once with `:json` formatter:
|
511
|
+
|
512
|
+
```ruby
|
513
|
+
TTY::Logger.new do |config|
|
514
|
+
config.handlers = [:console, [:console, formatter: :json]]
|
515
|
+
end
|
516
|
+
```
|
517
|
+
### 2.7 Output Streams
|
518
|
+
|
519
|
+
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
|
+
|
521
|
+
```ruby
|
522
|
+
logger = TTY::Logger.new do |config|
|
523
|
+
config.output = File.open("errors.log", "a")
|
524
|
+
end
|
525
|
+
```
|
526
|
+
|
527
|
+
You can also specify multiple streams that all log messages will be sent to:
|
528
|
+
|
529
|
+
```ruby
|
530
|
+
logger = TTY::Logger.new do |config|
|
531
|
+
config.output = [$stderr, File.open("errors.log", "a")]
|
532
|
+
end
|
533
|
+
```
|
534
|
+
|
535
|
+
Conversely, you can specify different output for each of the handlers used. For example, you can output all messages above info level to a file with a stream handler and only show error messages in the console with a nicely formatted output.
|
536
|
+
|
537
|
+
```ruby
|
538
|
+
logger = TTY::Logger.new do |config|
|
539
|
+
config.handlers = [
|
540
|
+
[:console, output: $stderr, level: :error],
|
541
|
+
[:stream, output: File.open("errors.log", "a"), level: :info)]
|
542
|
+
]
|
543
|
+
end
|
29
544
|
```
|
30
545
|
|
31
546
|
## Development
|
data/examples/console.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "../lib/tty/logger"
|
2
|
+
|
3
|
+
TTY::Logger.configure do |config|
|
4
|
+
config.max_bytes = 2**5
|
5
|
+
config.metadata = [:all]
|
6
|
+
config.handlers = [[:console, formatter: :text]]
|
7
|
+
config.level = :debug
|
8
|
+
end
|
9
|
+
|
10
|
+
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
11
|
+
|
12
|
+
logger.with(path: "/var/www/example.com").info("Deploying", "code")
|
13
|
+
|
14
|
+
puts "Levels:"
|
15
|
+
|
16
|
+
logger.debug("Debugging deployment")
|
17
|
+
logger.info("Info about the deploy")
|
18
|
+
logger.warn("Lack of resources")
|
19
|
+
logger.error("Failed to deploy")
|
20
|
+
logger.fatal("Terribly failed to deploy")
|
21
|
+
logger.success("Deployed successfully")
|
22
|
+
logger.wait("Ready to deploy")
|
data/examples/error.rb
ADDED
data/examples/handler.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative "../lib/tty/logger"
|
2
|
+
|
3
|
+
class MyHandler
|
4
|
+
def initialize(options = {})
|
5
|
+
@name = options[:name]
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(event)
|
9
|
+
puts "(#{@name}) #{event.metadata[:name]} #{event.message}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
TTY::Logger.configure do |config|
|
14
|
+
config.handlers = [[MyHandler, {name: :hello}]]
|
15
|
+
end
|
16
|
+
|
17
|
+
logger = TTY::Logger.new
|
18
|
+
|
19
|
+
logger.info("Custom logging")
|
data/examples/output.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "../lib/tty/logger"
|
2
|
+
|
3
|
+
file = File.open("errors.log", "a")
|
4
|
+
|
5
|
+
TTY::Logger.configure do |config|
|
6
|
+
config.metadata = [:all]
|
7
|
+
config.handlers = [:stream]
|
8
|
+
config.output = file
|
9
|
+
end
|
10
|
+
|
11
|
+
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
12
|
+
|
13
|
+
logger.error("Failed to deploy")
|
14
|
+
|
15
|
+
file.close
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "../lib/tty/logger"
|
2
|
+
|
3
|
+
logger = TTY::Logger.new
|
4
|
+
|
5
|
+
logger.success("Default success")
|
6
|
+
logger.error("Default error")
|
7
|
+
|
8
|
+
puts
|
9
|
+
|
10
|
+
new_style = TTY::Logger.new do |config|
|
11
|
+
config.handlers = [
|
12
|
+
[:console, {
|
13
|
+
styles: {
|
14
|
+
success: {
|
15
|
+
symbol: "+",
|
16
|
+
label: "Ohh yes"
|
17
|
+
},
|
18
|
+
error: {
|
19
|
+
symbol: "!",
|
20
|
+
label: "Dooh",
|
21
|
+
levelpad: 3
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}]
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
new_style.success("Custom success")
|
29
|
+
new_style.error("Custom error")
|
data/examples/stream.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "../lib/tty/logger"
|
2
|
+
|
3
|
+
TTY::Logger.configure do |config|
|
4
|
+
config.max_bytes = 2**5
|
5
|
+
config.metadata = [:all]
|
6
|
+
config.handlers = [[:stream, formatter: :text]]
|
7
|
+
config.level = :debug
|
8
|
+
end
|
9
|
+
|
10
|
+
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
11
|
+
|
12
|
+
logger.with(path: "/var/www/example.com").info("Deploying", "code")
|
13
|
+
|
14
|
+
puts "Levels:"
|
15
|
+
|
16
|
+
logger.debug("Debugging deployment")
|
17
|
+
logger.info("Info about the deploy")
|
18
|
+
logger.warn("Lack of resources")
|
19
|
+
logger.error("Failed to deploy")
|
20
|
+
logger.fatal("Terribly failed to deploy")
|
21
|
+
logger.success("Deployed successfully")
|
22
|
+
logger.wait("Ready to deploy")
|