ougai-formatters-customizable 1.0.0 → 1.0.1
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/README.md +311 -311
- data/lib/ougai/formatters/colors.rb +91 -91
- data/lib/ougai/formatters/colors/configuration.rb +119 -119
- data/lib/ougai/formatters/customizable.rb +160 -160
- data/lib/ougai/formatters/customizable/version.rb +8 -8
- metadata +21 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75e41c645ad212b04c6f76b7526973b5ca00e8aae5a26bc006bfc5645752b8c9
|
4
|
+
data.tar.gz: 63d45496264c090e129cf4e0c1229434e08753c395cbc8a27fffc2dd8e370210
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f60f37079c299160c14ff6b047a9b5011ffb1e09cc8d34485e3737a6f8cefd9f0ad8b663ebf52685f698f0fa5a5777e5b41e141f3503388f95df86201f53c9b
|
7
|
+
data.tar.gz: 8770a45afa7182cb2ea04720ffaafe258b43b89fad6d095af518f57b3aa007e245229fcc22373c4e6d20ce1cfdbc21e46bffb2745ed45c5a126fd67403576ec8
|
data/README.md
CHANGED
@@ -1,311 +1,311 @@
|
|
1
|
-
# Ougai-formatters-customizable
|
2
|
-
|
3
|
-
[](https://badge.fury.io/rb/ougai-formatters-customizable)
|
4
|
-
[](https://travis-ci.com/Al-un/ougai-formatters-customizable)
|
5
|
-
[](https://codeclimate.com/github/Al-un/ougai-formatters-customizable/maintainability)
|
6
|
-
[](https://codeclimate.com/github/Al-un/ougai-formatters-customizable/test_coverage)
|
7
|
-
|
8
|
-
A fully customizable formatters for [Ougai](https://github.com/tilfin/ougai)
|
9
|
-
library. Customization is about formatting and colorization
|
10
|
-
|
11
|
-
**Formatting**
|
12
|
-
|
13
|
-
Ougai log printing can be split in three components:
|
14
|
-
|
15
|
-
1. Main log message: usually timestamp, log severity and a message
|
16
|
-
2. Data: the structured logging, represented by a Hash
|
17
|
-
3. Errors
|
18
|
-
|
19
|
-
**Colorization**
|
20
|
-
|
21
|
-
Each part of the main log message can be colored independently. Colorization can
|
22
|
-
be extended to custom formatters as well.
|
23
|
-
|
24
|
-
## Usage
|
25
|
-
|
26
|
-
In your Gemfile, add *ougai-formatters-customizable* and its dependencies:
|
27
|
-
|
28
|
-
```ruby
|
29
|
-
gem '
|
30
|
-
gem 'ougai'
|
31
|
-
gem 'ougai-formatters-customizable'
|
32
|
-
```
|
33
|
-
|
34
|
-
Then initialize a formatter and assign it to your logger:
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
formatter = Ougai::Formatters::Customizable.new
|
38
|
-
# See Ougai documentation about how to initialize a Ougai logger
|
39
|
-
logger.formatter = formatter
|
40
|
-
```
|
41
|
-
|
42
|
-
The default *Customizable* configuration is exactly identical to a
|
43
|
-
*Ougai::Formatters::Readable* as-of Ougai 1.7.0.
|
44
|
-
|
45
|
-
#### Datetime format
|
46
|
-
|
47
|
-
Inherited from Ruby logger formatters, you can assign a datetime format:
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
formatter.datetime_format = '%H:%M:%S.%L' # print time only such as '15:42:36.246'
|
51
|
-
```
|
52
|
-
|
53
|
-
#### Message formatter: `format_msg`
|
54
|
-
|
55
|
-
Main log message formatter is a `proc` which takes four arguments:
|
56
|
-
|
57
|
-
- [String] severity: log severity. Is in capital letters
|
58
|
-
- [String] datetime: log timestamp. Is already formatted according to `datetime_format`.
|
59
|
-
Has to be treated like a String
|
60
|
-
- [String] progname: optional program name
|
61
|
-
- [Hash] data: structured log data. The main message is logged under the `:msg` key.
|
62
|
-
|
63
|
-
Custom message formatter can be assigned at initialization via the key `format_msg`:
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
formatter = Ougai::Formatters::Customizable.new(
|
67
|
-
format_msg: proc do |severity, datetime, _progname, data|
|
68
|
-
msg = data.delete(:msg)
|
69
|
-
format('%s %s: %s', severity, datetime, msg)
|
70
|
-
end
|
71
|
-
)
|
72
|
-
```
|
73
|
-
|
74
|
-
**Notes**
|
75
|
-
|
76
|
-
- It is recommended that this proc removes the `:msg` key from `data` to avoid
|
77
|
-
duplicates
|
78
|
-
- Although not mandatory, this formatter aims at outputting a single line String
|
79
|
-
|
80
|
-
#### Data formatter: `format_data`
|
81
|
-
|
82
|
-
Data formatter is a `proc` which takes only `data` as argument. Custom data
|
83
|
-
formatter can be assigned at initialization via `format_data` key:
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
formatter = Ougai::Formatters::Customizable.new(
|
87
|
-
format_data: proc do |data|
|
88
|
-
data.ai #
|
89
|
-
end
|
90
|
-
)
|
91
|
-
```
|
92
|
-
|
93
|
-
**Notes**
|
94
|
-
|
95
|
-
- Data formatter must return `nil` if `data` is empty.
|
96
|
-
- Default data formatter takes the `excluded_fields` option into account. You
|
97
|
-
need to add it to your custom formatter if you want to keep it.
|
98
|
-
|
99
|
-
#### Error formatter: `format_err`
|
100
|
-
|
101
|
-
Error formatter is a `proc` with only `data` as argument and can be assigned at
|
102
|
-
initialization via the `format_err` key:
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
formatter = Ougai::Formatters::Customizable.new(
|
106
|
-
format_err: proc do |data|
|
107
|
-
next nil unless data.key?(:err)
|
108
|
-
|
109
|
-
err = data.delete(:err)
|
110
|
-
" #{err[:name]} (#{err[:message]})"
|
111
|
-
end
|
112
|
-
)
|
113
|
-
```
|
114
|
-
|
115
|
-
**Notes**
|
116
|
-
|
117
|
-
- Error formatter must return `nil` if `data` does not contain the `:err` key
|
118
|
-
- Error formatter must remove `:err` key
|
119
|
-
- Default error formatter takes the `trace_indent` option into account. You need
|
120
|
-
to add it to your custom formatter if you want to keep it
|
121
|
-
|
122
|
-
#### Colorization
|
123
|
-
|
124
|
-
Colorization is handled by an instance of `Ougai::Formatters::Colors::Configuration`
|
125
|
-
and is basically a mapping *subject => value* to define the colors. Default subject
|
126
|
-
are:
|
127
|
-
|
128
|
-
- `:severity`: log severity coloring
|
129
|
-
- `:datetime`: datetime coloring
|
130
|
-
- `:msg`: log main message coloring
|
131
|
-
|
132
|
-
You can add your own subject if you need it in your custom formatters.
|
133
|
-
|
134
|
-
Values can have three types:
|
135
|
-
|
136
|
-
- String: this color is applied to the subject regardless the situation
|
137
|
-
- Hash: the color is defined by log severity. Non defined severity colors are
|
138
|
-
fetched from the `default` severity
|
139
|
-
- Symbol: the color is copied from the referenced symbol
|
140
|
-
|
141
|
-
Example:
|
142
|
-
|
143
|
-
```ruby
|
144
|
-
color_configuration = Ougai::Formatters::Colors::Configuration.new(
|
145
|
-
severity: {
|
146
|
-
trace: Ougai::Formatters::Colors::WHITE,
|
147
|
-
debug: Ougai::Formatters::Colors::GREEN,
|
148
|
-
info: Ougai::Formatters::Colors::CYAN,
|
149
|
-
warn: Ougai::Formatters::Colors::YELLOW,
|
150
|
-
error: Ougai::Formatters::Colors::RED,
|
151
|
-
fatal: Ougai::Formatters::Colors::PURPLE
|
152
|
-
},
|
153
|
-
msg: :severity,
|
154
|
-
datetime: {
|
155
|
-
default: Ougai::Formatters::Colors::PURPLE,
|
156
|
-
error: Ougai::Formatters::Colors::RED,
|
157
|
-
fatal: Ougai::Formatters::Colors::RED
|
158
|
-
},
|
159
|
-
custom: Ougai::Formatters::Colors::BLUE
|
160
|
-
)
|
161
|
-
```
|
162
|
-
|
163
|
-
- *Severity* has a different color dependending on log severity
|
164
|
-
- Main log *message* color is identical to severity color
|
165
|
-
- *Datetime* has a red color for *error* and *fatal* logs. Otherwise it is
|
166
|
-
colored in purple.
|
167
|
-
- A *custom* subject is always colored in blue regardless log severity
|
168
|
-
|
169
|
-
**Notes**
|
170
|
-
|
171
|
-
- If `:severity` is not defined, it is loaded from a default configuration
|
172
|
-
- If `:severity` is partially defined, missing severities are fetched from
|
173
|
-
default configuration
|
174
|
-
- Circular references are not checked and infinite loops can then be triggered.
|
175
|
-
|
176
|
-
## Integration
|
177
|
-
|
178
|
-
#### Lograge / Lograge-sql
|
179
|
-
|
180
|
-
I initially made this gem to couple Ougai with [lograge](https://github.com/roidrage/lograge)/[lograge-sql](https://github.com/iMacTia/lograge-sql). Lograge logs has to be
|
181
|
-
formatted in a way so that our custom formatters can catch it:
|
182
|
-
|
183
|
-
```ruby
|
184
|
-
# config/initializers/lograge.rb
|
185
|
-
config.lograge.formatter = Class.new do |fmt|
|
186
|
-
def fmt.call(data)
|
187
|
-
{ request: data }
|
188
|
-
end
|
189
|
-
end
|
190
|
-
```
|
191
|
-
|
192
|
-
I chose this format because I am also using Loggly and it is pretty convenient
|
193
|
-
to filter by `json.request.*` to fetch Lograge logs.
|
194
|
-
|
195
|
-
If using lograge-sql, make sure that Lograge format it as a Hash so that we can
|
196
|
-
leverage our main message formatter and data formatter:
|
197
|
-
|
198
|
-
```ruby
|
199
|
-
# config/initializers/lograge.rb
|
200
|
-
config.lograge_sql.extract_event = proc do |event|
|
201
|
-
{
|
202
|
-
name: event.payload[:name],
|
203
|
-
duration: event.duration.to_f.round(2),
|
204
|
-
sql: event.payload[:sql]
|
205
|
-
}
|
206
|
-
end
|
207
|
-
config.lograge_sql.formatter = proc do |sql_queries|
|
208
|
-
sql_queries
|
209
|
-
end
|
210
|
-
```
|
211
|
-
|
212
|
-
Wrap everything together example:
|
213
|
-
|
214
|
-
```ruby
|
215
|
-
# Define our colors
|
216
|
-
color_configuration = Ougai::Formatters::Colors::Configuration.new(
|
217
|
-
severity: {
|
218
|
-
trace: Ougai::Formatters::Colors::WHITE,
|
219
|
-
debug: Ougai::Formatters::Colors::GREEN,
|
220
|
-
info: Ougai::Formatters::Colors::CYAN,
|
221
|
-
warn: Ougai::Formatters::Colors::YELLOW,
|
222
|
-
error: Ougai::Formatters::Colors::RED,
|
223
|
-
fatal: Ougai::Formatters::Colors::PURPLE
|
224
|
-
},
|
225
|
-
msg: :severity,
|
226
|
-
datetime: {
|
227
|
-
default: Ougai::Formatters::Colors::PURPLE,
|
228
|
-
error: Ougai::Formatters::Colors::RED,
|
229
|
-
fatal: Ougai::Formatters::Colors::RED
|
230
|
-
}
|
231
|
-
)
|
232
|
-
|
233
|
-
# Lograge specific configuration
|
234
|
-
EXCLUDED_FIELD = [:credit_card] # example only
|
235
|
-
LOGRAGE_REJECT = [:sql_queries, :sql_queries_count]
|
236
|
-
|
237
|
-
# Console formatter configuration
|
238
|
-
console_formatter = Ougai::Formatters::Customizable.new(
|
239
|
-
format_msg: proc do |severity, datetime, _progname, data|
|
240
|
-
# Remove :msg regardless the outcome
|
241
|
-
msg = data.delete(:msg)
|
242
|
-
# Lograge specfic stuff: do not print sql queries in main log message
|
243
|
-
if data.key?(:request)
|
244
|
-
lograge = data[:request].reject { |k, _v| LOGRAGE_REJECT.include?(k) }
|
245
|
-
.map { |key, val| "#{key}: #{val}" }
|
246
|
-
.join(', ')
|
247
|
-
msg = color_config.color(:msg, lograge, severity)
|
248
|
-
# Standard text
|
249
|
-
else
|
250
|
-
msg = color_config.color(:msg, msg, severity)
|
251
|
-
end
|
252
|
-
|
253
|
-
# Standardize output
|
254
|
-
format('%s %s: %s',
|
255
|
-
color_config.color(:severity, severity, severity),
|
256
|
-
color_config.color(:datetime, datetime, severity),
|
257
|
-
msg)
|
258
|
-
end,
|
259
|
-
format_data: proc do |data|
|
260
|
-
# Lograge specfic stuff: main controller output handled by msg formatter
|
261
|
-
if data.key?(:request)
|
262
|
-
lograge_data = data[:request]
|
263
|
-
# concatenate SQL queries
|
264
|
-
if lograge_data.key?(:sql_queries)
|
265
|
-
lograge_data[:sql_queries].map do |sql_query|
|
266
|
-
format('%<duration>6.2fms %<name>25s %<sql>s', sql_query)
|
267
|
-
end
|
268
|
-
.join("\n")
|
269
|
-
# no queries: nothing to print
|
270
|
-
else
|
271
|
-
nil
|
272
|
-
end
|
273
|
-
# Default styling
|
274
|
-
else
|
275
|
-
# report excluded field parameter here: no need to add it to options
|
276
|
-
EXCLUDED_FIELD.each { |field| data.delete(field) }
|
277
|
-
next nil if data.empty?
|
278
|
-
|
279
|
-
# report plain parameter here: no need to add it to options
|
280
|
-
data.ai(plain: false)
|
281
|
-
end
|
282
|
-
end
|
283
|
-
)
|
284
|
-
console_formatter.datetime_format = '%H:%M:%S.%L' # local development: need only time
|
285
|
-
|
286
|
-
# Define console logger
|
287
|
-
console_logger = Log::Ougai::Logger.new(STDOUT)
|
288
|
-
console_logger.formatter = console_formatter
|
289
|
-
|
290
|
-
# Not this gem related: define file logger
|
291
|
-
file_logger = Log::Ougai::Logger.new(Rails.root.join('log/ougai.log'))
|
292
|
-
file_logger.formatter = Ougai::Formatters::Bunyan.new
|
293
|
-
|
294
|
-
# Extend console logger to file logger
|
295
|
-
console_logger.extend(Ougai::Logger.broadcast(file_logger))
|
296
|
-
|
297
|
-
# Assign Ougai logger
|
298
|
-
config.logger = console_logger
|
299
|
-
```
|
300
|
-
|
301
|
-
Output looks like
|
302
|
-

|
303
|
-
|
304
|
-
|
305
|
-
## Contributing
|
306
|
-
|
307
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/Al-un/ougai-formatters-customizable.
|
308
|
-
|
309
|
-
## License
|
310
|
-
|
311
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
1
|
+
# Ougai-formatters-customizable
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/ougai-formatters-customizable)
|
4
|
+
[](https://travis-ci.com/Al-un/ougai-formatters-customizable)
|
5
|
+
[](https://codeclimate.com/github/Al-un/ougai-formatters-customizable/maintainability)
|
6
|
+
[](https://codeclimate.com/github/Al-un/ougai-formatters-customizable/test_coverage)
|
7
|
+
|
8
|
+
A fully customizable formatters for [Ougai](https://github.com/tilfin/ougai)
|
9
|
+
library. Customization is about formatting and colorization
|
10
|
+
|
11
|
+
**Formatting**
|
12
|
+
|
13
|
+
Ougai log printing can be split in three components:
|
14
|
+
|
15
|
+
1. Main log message: usually timestamp, log severity and a message
|
16
|
+
2. Data: the structured logging, represented by a Hash
|
17
|
+
3. Errors
|
18
|
+
|
19
|
+
**Colorization**
|
20
|
+
|
21
|
+
Each part of the main log message can be colored independently. Colorization can
|
22
|
+
be extended to custom formatters as well.
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
In your Gemfile, add *ougai-formatters-customizable* and its dependencies:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'amazing_print'
|
30
|
+
gem 'ougai'
|
31
|
+
gem 'ougai-formatters-customizable'
|
32
|
+
```
|
33
|
+
|
34
|
+
Then initialize a formatter and assign it to your logger:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
formatter = Ougai::Formatters::Customizable.new
|
38
|
+
# See Ougai documentation about how to initialize a Ougai logger
|
39
|
+
logger.formatter = formatter
|
40
|
+
```
|
41
|
+
|
42
|
+
The default *Customizable* configuration is exactly identical to a
|
43
|
+
*Ougai::Formatters::Readable* as-of Ougai 1.7.0.
|
44
|
+
|
45
|
+
#### Datetime format
|
46
|
+
|
47
|
+
Inherited from Ruby logger formatters, you can assign a datetime format:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
formatter.datetime_format = '%H:%M:%S.%L' # print time only such as '15:42:36.246'
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Message formatter: `format_msg`
|
54
|
+
|
55
|
+
Main log message formatter is a `proc` which takes four arguments:
|
56
|
+
|
57
|
+
- [String] severity: log severity. Is in capital letters
|
58
|
+
- [String] datetime: log timestamp. Is already formatted according to `datetime_format`.
|
59
|
+
Has to be treated like a String
|
60
|
+
- [String] progname: optional program name
|
61
|
+
- [Hash] data: structured log data. The main message is logged under the `:msg` key.
|
62
|
+
|
63
|
+
Custom message formatter can be assigned at initialization via the key `format_msg`:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
formatter = Ougai::Formatters::Customizable.new(
|
67
|
+
format_msg: proc do |severity, datetime, _progname, data|
|
68
|
+
msg = data.delete(:msg)
|
69
|
+
format('%s %s: %s', severity, datetime, msg)
|
70
|
+
end
|
71
|
+
)
|
72
|
+
```
|
73
|
+
|
74
|
+
**Notes**
|
75
|
+
|
76
|
+
- It is recommended that this proc removes the `:msg` key from `data` to avoid
|
77
|
+
duplicates
|
78
|
+
- Although not mandatory, this formatter aims at outputting a single line String
|
79
|
+
|
80
|
+
#### Data formatter: `format_data`
|
81
|
+
|
82
|
+
Data formatter is a `proc` which takes only `data` as argument. Custom data
|
83
|
+
formatter can be assigned at initialization via `format_data` key:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
formatter = Ougai::Formatters::Customizable.new(
|
87
|
+
format_data: proc do |data|
|
88
|
+
data.ai # Amazing-print printing
|
89
|
+
end
|
90
|
+
)
|
91
|
+
```
|
92
|
+
|
93
|
+
**Notes**
|
94
|
+
|
95
|
+
- Data formatter must return `nil` if `data` is empty.
|
96
|
+
- Default data formatter takes the `excluded_fields` option into account. You
|
97
|
+
need to add it to your custom formatter if you want to keep it.
|
98
|
+
|
99
|
+
#### Error formatter: `format_err`
|
100
|
+
|
101
|
+
Error formatter is a `proc` with only `data` as argument and can be assigned at
|
102
|
+
initialization via the `format_err` key:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
formatter = Ougai::Formatters::Customizable.new(
|
106
|
+
format_err: proc do |data|
|
107
|
+
next nil unless data.key?(:err)
|
108
|
+
|
109
|
+
err = data.delete(:err)
|
110
|
+
" #{err[:name]} (#{err[:message]})"
|
111
|
+
end
|
112
|
+
)
|
113
|
+
```
|
114
|
+
|
115
|
+
**Notes**
|
116
|
+
|
117
|
+
- Error formatter must return `nil` if `data` does not contain the `:err` key
|
118
|
+
- Error formatter must remove `:err` key
|
119
|
+
- Default error formatter takes the `trace_indent` option into account. You need
|
120
|
+
to add it to your custom formatter if you want to keep it
|
121
|
+
|
122
|
+
#### Colorization
|
123
|
+
|
124
|
+
Colorization is handled by an instance of `Ougai::Formatters::Colors::Configuration`
|
125
|
+
and is basically a mapping *subject => value* to define the colors. Default subject
|
126
|
+
are:
|
127
|
+
|
128
|
+
- `:severity`: log severity coloring
|
129
|
+
- `:datetime`: datetime coloring
|
130
|
+
- `:msg`: log main message coloring
|
131
|
+
|
132
|
+
You can add your own subject if you need it in your custom formatters.
|
133
|
+
|
134
|
+
Values can have three types:
|
135
|
+
|
136
|
+
- String: this color is applied to the subject regardless the situation
|
137
|
+
- Hash: the color is defined by log severity. Non defined severity colors are
|
138
|
+
fetched from the `default` severity
|
139
|
+
- Symbol: the color is copied from the referenced symbol
|
140
|
+
|
141
|
+
Example:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
color_configuration = Ougai::Formatters::Colors::Configuration.new(
|
145
|
+
severity: {
|
146
|
+
trace: Ougai::Formatters::Colors::WHITE,
|
147
|
+
debug: Ougai::Formatters::Colors::GREEN,
|
148
|
+
info: Ougai::Formatters::Colors::CYAN,
|
149
|
+
warn: Ougai::Formatters::Colors::YELLOW,
|
150
|
+
error: Ougai::Formatters::Colors::RED,
|
151
|
+
fatal: Ougai::Formatters::Colors::PURPLE
|
152
|
+
},
|
153
|
+
msg: :severity,
|
154
|
+
datetime: {
|
155
|
+
default: Ougai::Formatters::Colors::PURPLE,
|
156
|
+
error: Ougai::Formatters::Colors::RED,
|
157
|
+
fatal: Ougai::Formatters::Colors::RED
|
158
|
+
},
|
159
|
+
custom: Ougai::Formatters::Colors::BLUE
|
160
|
+
)
|
161
|
+
```
|
162
|
+
|
163
|
+
- *Severity* has a different color dependending on log severity
|
164
|
+
- Main log *message* color is identical to severity color
|
165
|
+
- *Datetime* has a red color for *error* and *fatal* logs. Otherwise it is
|
166
|
+
colored in purple.
|
167
|
+
- A *custom* subject is always colored in blue regardless log severity
|
168
|
+
|
169
|
+
**Notes**
|
170
|
+
|
171
|
+
- If `:severity` is not defined, it is loaded from a default configuration
|
172
|
+
- If `:severity` is partially defined, missing severities are fetched from
|
173
|
+
default configuration
|
174
|
+
- Circular references are not checked and infinite loops can then be triggered.
|
175
|
+
|
176
|
+
## Integration
|
177
|
+
|
178
|
+
#### Lograge / Lograge-sql
|
179
|
+
|
180
|
+
I initially made this gem to couple Ougai with [lograge](https://github.com/roidrage/lograge)/[lograge-sql](https://github.com/iMacTia/lograge-sql). Lograge logs has to be
|
181
|
+
formatted in a way so that our custom formatters can catch it:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
# config/initializers/lograge.rb
|
185
|
+
config.lograge.formatter = Class.new do |fmt|
|
186
|
+
def fmt.call(data)
|
187
|
+
{ request: data }
|
188
|
+
end
|
189
|
+
end
|
190
|
+
```
|
191
|
+
|
192
|
+
I chose this format because I am also using Loggly and it is pretty convenient
|
193
|
+
to filter by `json.request.*` to fetch Lograge logs.
|
194
|
+
|
195
|
+
If using lograge-sql, make sure that Lograge format it as a Hash so that we can
|
196
|
+
leverage our main message formatter and data formatter:
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
# config/initializers/lograge.rb
|
200
|
+
config.lograge_sql.extract_event = proc do |event|
|
201
|
+
{
|
202
|
+
name: event.payload[:name],
|
203
|
+
duration: event.duration.to_f.round(2),
|
204
|
+
sql: event.payload[:sql]
|
205
|
+
}
|
206
|
+
end
|
207
|
+
config.lograge_sql.formatter = proc do |sql_queries|
|
208
|
+
sql_queries
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
Wrap everything together example:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# Define our colors
|
216
|
+
color_configuration = Ougai::Formatters::Colors::Configuration.new(
|
217
|
+
severity: {
|
218
|
+
trace: Ougai::Formatters::Colors::WHITE,
|
219
|
+
debug: Ougai::Formatters::Colors::GREEN,
|
220
|
+
info: Ougai::Formatters::Colors::CYAN,
|
221
|
+
warn: Ougai::Formatters::Colors::YELLOW,
|
222
|
+
error: Ougai::Formatters::Colors::RED,
|
223
|
+
fatal: Ougai::Formatters::Colors::PURPLE
|
224
|
+
},
|
225
|
+
msg: :severity,
|
226
|
+
datetime: {
|
227
|
+
default: Ougai::Formatters::Colors::PURPLE,
|
228
|
+
error: Ougai::Formatters::Colors::RED,
|
229
|
+
fatal: Ougai::Formatters::Colors::RED
|
230
|
+
}
|
231
|
+
)
|
232
|
+
|
233
|
+
# Lograge specific configuration
|
234
|
+
EXCLUDED_FIELD = [:credit_card] # example only
|
235
|
+
LOGRAGE_REJECT = [:sql_queries, :sql_queries_count]
|
236
|
+
|
237
|
+
# Console formatter configuration
|
238
|
+
console_formatter = Ougai::Formatters::Customizable.new(
|
239
|
+
format_msg: proc do |severity, datetime, _progname, data|
|
240
|
+
# Remove :msg regardless the outcome
|
241
|
+
msg = data.delete(:msg)
|
242
|
+
# Lograge specfic stuff: do not print sql queries in main log message
|
243
|
+
if data.key?(:request)
|
244
|
+
lograge = data[:request].reject { |k, _v| LOGRAGE_REJECT.include?(k) }
|
245
|
+
.map { |key, val| "#{key}: #{val}" }
|
246
|
+
.join(', ')
|
247
|
+
msg = color_config.color(:msg, lograge, severity)
|
248
|
+
# Standard text
|
249
|
+
else
|
250
|
+
msg = color_config.color(:msg, msg, severity)
|
251
|
+
end
|
252
|
+
|
253
|
+
# Standardize output
|
254
|
+
format('%s %s: %s',
|
255
|
+
color_config.color(:severity, severity, severity),
|
256
|
+
color_config.color(:datetime, datetime, severity),
|
257
|
+
msg)
|
258
|
+
end,
|
259
|
+
format_data: proc do |data|
|
260
|
+
# Lograge specfic stuff: main controller output handled by msg formatter
|
261
|
+
if data.key?(:request)
|
262
|
+
lograge_data = data[:request]
|
263
|
+
# concatenate SQL queries
|
264
|
+
if lograge_data.key?(:sql_queries)
|
265
|
+
lograge_data[:sql_queries].map do |sql_query|
|
266
|
+
format('%<duration>6.2fms %<name>25s %<sql>s', sql_query)
|
267
|
+
end
|
268
|
+
.join("\n")
|
269
|
+
# no queries: nothing to print
|
270
|
+
else
|
271
|
+
nil
|
272
|
+
end
|
273
|
+
# Default styling
|
274
|
+
else
|
275
|
+
# report excluded field parameter here: no need to add it to options
|
276
|
+
EXCLUDED_FIELD.each { |field| data.delete(field) }
|
277
|
+
next nil if data.empty?
|
278
|
+
|
279
|
+
# report plain parameter here: no need to add it to options
|
280
|
+
data.ai(plain: false)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
)
|
284
|
+
console_formatter.datetime_format = '%H:%M:%S.%L' # local development: need only time
|
285
|
+
|
286
|
+
# Define console logger
|
287
|
+
console_logger = Log::Ougai::Logger.new(STDOUT)
|
288
|
+
console_logger.formatter = console_formatter
|
289
|
+
|
290
|
+
# Not this gem related: define file logger
|
291
|
+
file_logger = Log::Ougai::Logger.new(Rails.root.join('log/ougai.log'))
|
292
|
+
file_logger.formatter = Ougai::Formatters::Bunyan.new
|
293
|
+
|
294
|
+
# Extend console logger to file logger
|
295
|
+
console_logger.extend(Ougai::Logger.broadcast(file_logger))
|
296
|
+
|
297
|
+
# Assign Ougai logger
|
298
|
+
config.logger = console_logger
|
299
|
+
```
|
300
|
+
|
301
|
+
Output looks like
|
302
|
+

|
303
|
+
|
304
|
+
|
305
|
+
## Contributing
|
306
|
+
|
307
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Al-un/ougai-formatters-customizable.
|
308
|
+
|
309
|
+
## License
|
310
|
+
|
311
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|