lumberjack 1.2.9 → 1.3.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/ARCHITECTURE.md +244 -0
  3. data/CHANGELOG.md +78 -2
  4. data/README.md +176 -58
  5. data/VERSION +1 -1
  6. data/lib/lumberjack/context.rb +5 -5
  7. data/lib/lumberjack/device/date_rolling_log_file.rb +1 -1
  8. data/lib/lumberjack/device/log_file.rb +1 -1
  9. data/lib/lumberjack/device/multi.rb +1 -1
  10. data/lib/lumberjack/device/null.rb +1 -1
  11. data/lib/lumberjack/device/rolling_log_file.rb +2 -2
  12. data/lib/lumberjack/device/size_rolling_log_file.rb +1 -1
  13. data/lib/lumberjack/device/writer.rb +13 -9
  14. data/lib/lumberjack/device.rb +1 -1
  15. data/lib/lumberjack/formatter/date_time_formatter.rb +2 -2
  16. data/lib/lumberjack/formatter/exception_formatter.rb +2 -2
  17. data/lib/lumberjack/formatter/id_formatter.rb +1 -1
  18. data/lib/lumberjack/formatter/inspect_formatter.rb +1 -1
  19. data/lib/lumberjack/formatter/multiply_formatter.rb +25 -0
  20. data/lib/lumberjack/formatter/object_formatter.rb +1 -1
  21. data/lib/lumberjack/formatter/pretty_print_formatter.rb +1 -1
  22. data/lib/lumberjack/formatter/redact_formatter.rb +23 -0
  23. data/lib/lumberjack/formatter/round_formatter.rb +21 -0
  24. data/lib/lumberjack/formatter/string_formatter.rb +1 -1
  25. data/lib/lumberjack/formatter/strip_formatter.rb +1 -1
  26. data/lib/lumberjack/formatter/structured_formatter.rb +1 -1
  27. data/lib/lumberjack/formatter/tagged_message.rb +39 -0
  28. data/lib/lumberjack/formatter/truncate_formatter.rb +1 -1
  29. data/lib/lumberjack/formatter.rb +29 -14
  30. data/lib/lumberjack/log_entry.rb +25 -15
  31. data/lib/lumberjack/logger.rb +132 -40
  32. data/lib/lumberjack/rack/context.rb +21 -2
  33. data/lib/lumberjack/rack/request_id.rb +7 -3
  34. data/lib/lumberjack/rack/unit_of_work.rb +6 -2
  35. data/lib/lumberjack/rack.rb +1 -1
  36. data/lib/lumberjack/severity.rb +13 -1
  37. data/lib/lumberjack/tag_formatter.rb +102 -27
  38. data/lib/lumberjack/tagged_logger_support.rb +6 -1
  39. data/lib/lumberjack/tags.rb +1 -7
  40. data/lib/lumberjack/template.rb +3 -3
  41. data/lib/lumberjack/utils.rb +133 -0
  42. data/lib/lumberjack.rb +12 -6
  43. data/lumberjack.gemspec +2 -2
  44. metadata +12 -6
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
- ![Continuous Integration](https://github.com/bdurand/lumberjack/workflows/Continuous%20Integration/badge.svg)
2
- [![Maintainability](https://api.codeclimate.com/v1/badges/a0abc03721fff9b0cde1/maintainability)](https://codeclimate.com/github/bdurand/lumberjack/maintainability)
3
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
4
-
5
1
  # Lumberjack
6
2
 
7
- Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It uses nearly the same API as the Logger class in the Ruby standard library and as ActiveSupport::BufferedLogger in Rails.
3
+ [![Continuous Integration](https://github.com/bdurand/lumberjack/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/bdurand/lumberjack/actions/workflows/continuous_integration.yml)
4
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
5
+ [![Gem Version](https://badge.fury.io/rb/lumberjack.svg)](https://badge.fury.io/rb/lumberjack)
6
+
7
+ Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It uses nearly the same API as the Logger class in the Ruby standard library and as ActiveSupport::BufferedLogger in Rails. It is designed with structured logging in mind, but can be used for simple text logging as well.
8
8
 
9
9
  ## Usage
10
10
 
11
- This code aims to be extremely simple to use. The core interface is the Lumberjack::Logger which is used to log messages (which can be any object) with a specified Severity. Each logger has a level associated with it and messages are only written if their severity is greater than or equal to the level.
11
+ This code aims to be extremely simple to use and matches the standard Ruby `Logger` interface. The core interface is the Lumberjack::Logger which is used to log messages (which can be any object) with a specified Severity. Each logger has a level associated with it and messages are only written if their severity is greater than or equal to the level.
12
12
 
13
13
  ```ruby
14
14
  logger = Lumberjack::Logger.new("logs/application.log") # Open a new log file with INFO level
@@ -23,27 +23,25 @@ This code aims to be extremely simple to use. The core interface is the Lumberja
23
23
  logger.info("End request")
24
24
  ```
25
25
 
26
- This is all you need to know to log messages.
27
-
28
26
  ## Features
29
27
 
30
- ### Meta data
28
+ ### Metadata
31
29
 
32
30
  When messages are added to the log, additional data about the message is kept in a Lumberjack::LogEntry. This means you don't need to worry about adding the time or process id to your log messages as they will be automatically recorded.
33
31
 
34
32
  The following information is recorded for each message:
35
33
 
36
- * severity - The severity recorded for the message.
37
- * time - The time at which the message was recorded.
38
- * program name - The name of the program logging the message. This can be either set for all messages or customized with each message.
39
- * process id - The process id (pid) of the process that logged the message.
40
- * tags - An map of name value pairs for addition information about the log context.
34
+ - severity - The severity recorded for the message.
35
+ - time - The time at which the message was recorded.
36
+ - program name - The name of the program logging the message. This can be either set for all messages or customized with each message.
37
+ - process id - The process id (pid) of the process that logged the message.
38
+ - tags - A map of name value pairs for additional information about the log context.
41
39
 
42
40
  ### Tags
43
41
 
44
- You can use tags to provide additional meta data about a log message or the context that the log message is being made in. Using tags can keep your log messages clean. You can avoid string interpolation to add additional meta data.
42
+ You can use tags to provide additional meta data about a log message or the context that the log message is being made in. Using tags can keep your log messages clean. You can avoid string interpolation to add additional meta data. Tags enable a structured logging approach where you can add additional information to log messages without changing the message format.
45
43
 
46
- Each of the logger methods includes an additional argument that can be used to specify tags on a messsage:
44
+ Each of the logger methods includes an additional argument that can be used to specify tags on a message:
47
45
 
48
46
  ```ruby
49
47
  logger.info("request completed", duration: elapsed_time, status: response.status)
@@ -88,9 +86,55 @@ logger.info("no requests") # Will not include the `request_id` tag
88
86
 
89
87
  Tag keys are always converted to strings. Tags are inherited so that message tags take precedence over block tags which take precedence over global tags.
90
88
 
89
+ #### Structured Logging with Tags
90
+
91
+ Tags are particularly powerful for structured logging, where you want to capture machine-readable data alongside human-readable log messages. Instead of embedding variable data directly in log messages (which makes parsing difficult), you can use tags to separate the static message from the dynamic data.
92
+
93
+ ```ruby
94
+ # Instead of this (harder to parse)
95
+ logger.info("User john_doe logged in from IP 192.168.1.100 in 0.25 seconds")
96
+
97
+ # Do this (structured and parseable)
98
+ logger.info("User logged in", {
99
+ user_id: "john_doe",
100
+ ip_address: "192.168.1.100",
101
+ duration: 0.25,
102
+ action: "login"
103
+ })
104
+ ```
105
+
106
+ This approach provides several benefits:
107
+
108
+ - **Consistent message format** - The base message stays the same while data varies
109
+ - **Easy filtering and searching** - You can search by specific tag values
110
+ - **Better analytics** - Aggregate data by tag values (e.g., average login duration)
111
+ - **Machine processing** - Automated systems can easily extract and process tag data
112
+
113
+ You can also use nested structures in tags for complex data:
114
+
115
+ ```ruby
116
+ logger.info("API request completed", {
117
+ request: {
118
+ method: "POST",
119
+ path: "/api/users",
120
+ user_agent: request.user_agent
121
+ },
122
+ response: {
123
+ status: 201,
124
+ duration_ms: 150
125
+ },
126
+ user: {
127
+ id: current_user.id,
128
+ role: current_user.role
129
+ }
130
+ })
131
+ ```
132
+
133
+ When combined with structured output devices (like [`lumberjack_json_device`](https://github.com/bdurand/lumberjack_json_device)), this creates logs that are both human-readable and machine-processable, making them ideal for log aggregation systems, monitoring, and analytics.
134
+
91
135
  #### Compatibility with ActiveSupport::TaggedLogging
92
136
 
93
- `Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::TaggedLogging`. This is so that other code that expect to have a logger that responds to the `tagged` method will work. Any tags added with the `tagged` method will be appended to an array in the the "tagged" tag.
137
+ `Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::TaggedLogging`. This is so that other code that expects to have a logger that responds to the `tagged` method will work. Any tags added with the `tagged` method will be appended to an array in the "tagged" tag.
94
138
 
95
139
  ```ruby
96
140
  logger.tagged("foo", "bar=1", "other") do
@@ -102,38 +146,37 @@ end
102
146
 
103
147
  The built in `Lumberjack::Device::Writer` class has built in support for including tags in the output using the `Lumberjack::Template` class.
104
148
 
105
- You can specify any tag name you want in a template as well as the `:tags` macro for all tags. If a tag name has been used as it's own macro, it will not be included in the `:tags` macro.
106
-
107
- #### Unit Of Work
108
-
109
- Lumberjack 1.0 had a concept of a unit of work id that could be used to tie log messages together. This has been replaced by tags. There is still an implementation of `Lumberjack.unit_of_work`, but it is just a wrapper on the tag implementation.
149
+ You can specify any tag name you want in a template as well as the `:tags` macro for all tags. If a tag name has been used as its own macro, it will not be included in the `:tags` macro.
110
150
 
111
151
  ### Pluggable Devices
112
152
 
113
153
  When a Logger logs a LogEntry, it sends it to a Lumberjack::Device. Lumberjack comes with a variety of devices for logging to IO streams or files.
114
154
 
115
- * Lumberjack::Device::Writer - Writes log entries to an IO stream.
116
- * Lumberjack::Device::LogFile - Writes log entries to a file.
117
- * Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
118
- * Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
119
- * Lumberjack::Device::Multi - This device wraps mulitiple other devices and will write log entries to each of them.
120
- * Lumberjack::Device::Null - This device produces no output and is intended for testing environments.
155
+ - Lumberjack::Device::Writer - Writes log entries to an IO stream.
156
+ - Lumberjack::Device::LogFile - Writes log entries to a file.
157
+ - Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
158
+ - Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
159
+ - Lumberjack::Device::Multi - This device wraps multiple other devices and will write log entries to each of them.
160
+ - Lumberjack::Device::Null - This device produces no output and is intended for testing environments.
121
161
 
122
- If you'd like to send you log to a different kind of output, you just need to extend the Device class and implement the +write+ method. Or check out these plugins:
162
+ If you'd like to send your log to a different kind of output, you just need to extend the Device class and implement the `write` method. Or check out these plugins:
123
163
 
124
- * [lumberjack_syslog_device](https://github.com/bdurand/lumberjack_syslog_device) - send your log messages to the system wide syslog service
125
- * [lumberjack_mongo_device](https://github.com/bdurand/lumberjack_mongo_device) - store your log messages to a [MongoDB](http://www.mongodb.org/) NoSQL data store
126
- * [lumberjack-couchdb-driver](https://github.com/narkisr/lumberjack-couchdb-driver) - store your log messages to a [CouchDB](http://couchdb.apache.org/) NoSQL data store
127
- * [lumberjack_heroku_device](https://github.com/tonycoco/lumberjack_heroku_device) - log to Heroku's logging system
164
+ - [lumberjack_json_device](https://github.com/bdurand/lumberjack_json_device) - output your log messages as stream of JSON objects for structured logging.
165
+ - [lumberjack_syslog_device](https://github.com/bdurand/lumberjack_syslog_device) - send your log messages to the system wide syslog service
166
+ - [lumberjack_mongo_device](https://github.com/bdurand/lumberjack_mongo_device) - store your log messages to a [MongoDB](http://www.mongodb.org/) NoSQL data store
167
+ - [lumberjack_redis_device](https://github.com/bdurand/lumberjack_redis_device) - store your log messages in a [Redis](https://redis.io/) data store
168
+ - [lumberjack-couchdb-driver](https://github.com/narkisr/lumberjack-couchdb-driver) - store your log messages to a [CouchDB](http://couchdb.apache.org/) NoSQL data store
169
+ - [lumberjack_heroku_device](https://github.com/tonycoco/lumberjack_heroku_device) - log to Heroku's logging system
170
+ - [lumberjack_capture_device](https://github.com/bdurand/lumberjack_capture_device) - capture log messages in memory in test environments so that you can include log output assertions in your tests.
128
171
 
129
172
  ### Customize Formatting
130
173
 
131
174
  #### Formatters
132
175
 
133
- The message you send to the logger can be any object type and does not need to be a string. You can specify a `Lumberjack::Formatter` to instruct the logger how to format objects before outputting them to the device. You do this by mapping classes or modules to formatter code. This code can be either a block or an object that responds to the `call` method. The formatter will be called with the object logged as the message and the returned value will be what is sent to the device.
176
+ The message you send to the logger can be any object type and does not need to be a string. You can specify how to format different object types with a formatter. The formatter is responsible for converting the object into a string representation for logging. You do this by mapping classes or modules to formatter code. This code can be either a block or an object that responds to the `call` method. The formatter will be called with the object logged as the message and the returned value will be what is sent to the device.
134
177
 
135
178
  ```ruby
136
- # Format all floating point number with three significant digits.
179
+ # Format all floating point numbers with three significant digits.
137
180
  logger.formatter.add(Float) { |value| value.round(3) }
138
181
 
139
182
  # Format all enumerable objects as a comma delimited string.
@@ -147,25 +190,53 @@ There are several built in classes you can add as formatters. You can use a symb
147
190
  logger.formatter.add(Hash, Lumberjack::Formatter::PrettyPrintFormatter.new) # alternative using a formatter instance
148
191
  ```
149
192
 
150
- * `:object` - `Lumberjack::Formatter::ObjectFormatter` - no op conversion that returns the object itself.
151
- * `:string` - `Lumberjack::Formatter::StringFormatter` - calls `to_s` on the object.
152
- * `:strip` - `Lumberjack::Formatter::StripFormatter` - calls `to_s.strip` on the object.
153
- * `:inspect` - `Lumberjack::Formatter::InspectFormatter` - calls `inspect` on the object.
154
- * `:exception` - `Lumberjack::Formatter::ExceptionFormatter` - special formatter for exceptions which logs them as multi line statements with the message and backtrace.
155
- * `:date_time` - `Lumberjack::Formatter::DateTimeFormatter` - special formatter for dates and times to format them using `strftime`.
156
- * `:pretty_print` - `Lumberjack::Formatter::PrettyPrintFormatter` - returns the pretty print format of the object.
157
- * `:id` - `Lumberjack::Formatter::IdFormatter` - returns a hash of the object with keys for the id attribute and class.
158
- * `:structured` - `Lumberjack::Formatter::StructuredFormatter` - crawls the object and applies the formatter recursively to Enumerable objects found in it (arrays, hashes, etc.).
193
+ - `:object` - `Lumberjack::Formatter::ObjectFormatter` - no op conversion that returns the object itself.
194
+ - `:string` - `Lumberjack::Formatter::StringFormatter` - calls `to_s` on the object.
195
+ - `:strip` - `Lumberjack::Formatter::StripFormatter` - calls `to_s.strip` on the object.
196
+ - `:inspect` - `Lumberjack::Formatter::InspectFormatter` - calls `inspect` on the object.
197
+ - `:exception` - `Lumberjack::Formatter::ExceptionFormatter` - special formatter for exceptions which logs them as multi-line statements with the message and backtrace.
198
+ - `:date_time` - `Lumberjack::Formatter::DateTimeFormatter` - special formatter for dates and times to format them using `strftime`.
199
+ - `:pretty_print` - `Lumberjack::Formatter::PrettyPrintFormatter` - returns the pretty print format of the object.
200
+ - `:id` - `Lumberjack::Formatter::IdFormatter` - returns a hash of the object with keys for the id attribute and class.
201
+ - `:structured` - `Lumberjack::Formatter::StructuredFormatter` - crawls the object and applies the formatter recursively to Enumerable objects found in it (arrays, hashes, etc.).
159
202
 
160
203
  To define your own formatter, either provide a block or an object that responds to `call` with a single argument.
161
204
 
205
+ #### Default Formatter
206
+
207
+ The default formatter is applied to all objects being logged. This includes both messages and tags.
208
+
162
209
  The default formatter will pass through values for strings, numbers, and booleans, and use the `:inspect` formatter for all objects except for exceptions which will be formatted with the `:exception` formatter.
163
210
 
211
+ #### Message Formatter
212
+
213
+ You can add a formatter for just the log message with the `message_formatter` method. This formatter will only apply to the message and not to any tags.
214
+
215
+ ```ruby
216
+ logger.message_formatter.add(String, :truncate, 1000) # Will truncate all string messages to 1000 characters
217
+ ```
218
+
219
+ ##### Extracting Tags from Messages
220
+
221
+ If you are using structured logging, you can use a formatter to extract tags from the log message by adding a formatter that returns a `Lumberjack::Formatter::TaggedMessage`. For example, if you want to extract metadata from exceptions and add them as tags, you could do this:
222
+
223
+ ```ruby
224
+ logger.message_formatter.add(Exception, ->(e) {
225
+ Lumberjack::Formatter::TaggedMessage.new(e.inspect, {
226
+ "error.message": e.message,
227
+ "error.class": e.class.name,
228
+ "error.trace": e.backtrace
229
+ })
230
+ })
231
+
232
+ logger.error(exception) # Will log the exception and add tags for the message, class, and trace.
233
+ ```
234
+
164
235
  #### Tag Formatters
165
236
 
166
- The `logger.formatter` will only apply to log messages. You can use `logger.tag_formatter` to register formatters for tags. You can register both default formatters that will apply to all tag values, as well as tag specifice formatters that will apply only to objects with a specific tag name.
237
+ The `logger.formatter` will only apply to log messages. You can use `logger.tag_formatter` to register formatters for tags. You can register both default formatters that will apply to all tag values, as well as tag specific formatters that will apply only to objects with a specific tag name.
167
238
 
168
- The fomatter values can be either a `Lumberjack::Formatter` or a block or an object that responds to `call`. If you supply a `Lumberjack::Formatter`, the tag value will be passed through the rules for that formatter. If you supply a block or other object, it will be called with the tag value.
239
+ The formatter values can be either a `Lumberjack::Formatter` or a block or an object that responds to `call`. If you supply a `Lumberjack::Formatter`, the tag value will be passed through the rules for that formatter. If you supply a block or other object, it will be called with the tag value.
169
240
 
170
241
  ```ruby
171
242
  # These will all do the same thing formatting all tag values with `inspect`
@@ -176,11 +247,20 @@ logger.tag_formatter.default { |value| value.inspect }
176
247
  # This will register formatters only on specific tag names
177
248
  logger.tag_formatter.add(:thread) { |thread| "Thread(#{thread.name})" }
178
249
  logger.tag_formatter.add(:current_user, Lumberjack::Formatter::IdFormatter.new)
250
+
251
+ # You can also register formatters for tag values by class
252
+ logger.tag_formatter.add(Numeric, &:round)
253
+
254
+ # Tag formatters will be applied to nested hashes and arrays as well.
255
+
256
+ # Name formatters use dot syntax to apply to nested hashes.
257
+ logger.tag_formatter.add("user.username", &:upcase)
258
+ # logger.tag(user: {username: "john_doe"}) # Will log the tag as {"user" => "username" => "JOHN_DOE"}
179
259
  ```
180
260
 
181
261
  #### Templates
182
262
 
183
- If you use the built in `Lumberjack::Writer` derived devices, you can also customize the Template used to format the LogEntry.
263
+ If you use the built-in `Lumberjack::Writer` derived devices, you can also customize the Template used to format the LogEntry.
184
264
 
185
265
  See `Lumberjack::Template` for a complete list of macros you can use in the template. You can also use a block that receives a `Lumberjack::LogEntry` as a template.
186
266
 
@@ -202,7 +282,7 @@ See `Lumberjack::Template` for a complete list of macros you can use in the temp
202
282
 
203
283
  ### Buffered Logging
204
284
 
205
- The logger has hooks for devices that support buffering to potentially increase performance by batching physical writes. Log entries are not guaranteed to be written until the Lumberjack::Logger#flush method is called. Buffering can improve performance if I/O is slow or there high overhead writing to the log device.
285
+ The logger has hooks for devices that support buffering to potentially increase performance by batching physical writes. Log entries are not guaranteed to be written until the Lumberjack::Logger#flush method is called. Buffering can improve performance if I/O is slow or there is high overhead writing to the log device.
206
286
 
207
287
  You can use the `:flush_seconds` option on the logger to periodically flush the log. This is usually a good idea so you can more easily debug hung processes. Without periodic flushing, a process that hangs may never write anything to the log because the messages are sitting in a buffer. By turning on periodic flushing, the logged messages will be written which can greatly aid in debugging the problem.
208
288
 
@@ -222,28 +302,38 @@ The built in devices include two that can automatically roll log files based eit
222
302
 
223
303
  There is a similar feature in the standard library Logger class, but the implementation here is safe to use with multiple processes writing to the same log file.
224
304
 
225
- ## Difference Standard Library Logger
305
+ ## Integrations
306
+
307
+ Lumberjack has built in support for logging extensions in Rails.
308
+
309
+ You can use the [`lumberjack_sidekiq`](https://github.com/bdurand/lumberjack_sidekiq) gem to replace Sidekiq's default logger with Lumberjack. This allows you to use all of Lumberjack's features, such as structured logging and tag support, in your Sidekiq jobs.
226
310
 
227
- `Lumberjack::Logger` does not extend from the `Logger` class in the standard library, but it does implement a compantible API. The main difference is in the flow of how messages are ultimately sent to devices for output.
311
+ If you are using DataDog for logging, you can use the [`lumberjack_data_dog`](https://github.com/bdurand/lumberjack_data_dog) gem to format your logs in DataDog's standard attributes format.
312
+
313
+ ## Differences from Standard Library Logger
314
+
315
+ `Lumberjack::Logger` does not extend from the `Logger` class in the standard library, but it does implement a compatible API. The main difference is in the flow of how messages are ultimately sent to devices for output.
228
316
 
229
317
  The standard library Logger logic converts the log entries to strings and then sends the string to the device to be written to a stream. Lumberjack, on the other hand, sends structured data in the form of a `Lumberjack::LogEntry` to the device and lets the device worry about how to format it. The reason for this flip is to better support structured data logging. Devices (even ones that write to streams) can format the entire payload including non-string objects and tags however they need to.
230
318
 
231
- The logging methods (`debug`, 'info', 'warn', 'error', 'fatal') are overloaded with an additional argument for setting tags on the log entry.
319
+ The logging methods (`debug`, `info`, `warn`, `error`, `fatal`) are overloaded with an additional argument for setting tags on the log entry.
232
320
 
233
321
  ## Examples
234
322
 
235
- These example are for Rails applications, but there is no dependency on Rails for using this gem. Most of the examples are applicable to any Ruby application.
323
+ These examples are for Rails applications, but there is no dependency on Rails for using this gem. Most of the examples are applicable to any Ruby application.
236
324
 
237
325
  In a Rails application you can replace the default production logger by adding this to your config/environments/production.rb file:
238
326
 
239
327
  ```ruby
240
- # Use the ActionDispatch request id as the unit of work id. This will use just the first chunk of the request id.
241
- # If you want to use an abbreviated request id for terseness, change the last argument to `true`
242
- config.middleware.insert_after ActionDispatch::RequestId, Lumberjack::Rack::RequestId, false
243
- # Use a custom unit of work id to each request
244
- # config.middleware.insert(0, Lumberjack::Rack::UnitOfWork)
328
+ # Add the ActionDispatch request id as a global tag on all log entries.
329
+ config.middleware.insert_after(
330
+ ActionDispatch::RequestId,
331
+ Lumberjack::Rack::Context,
332
+ request_id: ->(env) { env["action_dispatch.request_id"] }
333
+ )
245
334
  # Change the logger to use Lumberjack
246
- log_file_path = Rails.root + "log" + "#{Rails.env}.log"
335
+ log_file = Rails.root + "log" + "#{Rails.env}.log"
336
+ # log_file = $stdout # or write to stdout instead of a file
247
337
  config.logger = Lumberjack::Logger.new(log_file, :level => :warn)
248
338
  ```
249
339
 
@@ -276,3 +366,31 @@ To send log messages to syslog instead of to a file, you could use this (require
276
366
  ```ruby
277
367
  config.logger = Lumberjack::Logger.new(Lumberjack::SyslogDevice.new)
278
368
  ```
369
+
370
+ ## Installation
371
+
372
+ Add this line to your application's Gemfile:
373
+
374
+ ```ruby
375
+ gem "lumberjack"
376
+ ```
377
+
378
+ And then execute:
379
+ ```bash
380
+ $ bundle install
381
+ ```
382
+
383
+ Or install it yourself as:
384
+ ```bash
385
+ $ gem install lumberjack
386
+ ```
387
+
388
+ ## Contributing
389
+
390
+ Open a pull request on GitHub.
391
+
392
+ Please use the [standardrb](https://github.com/testdouble/standard) syntax and lint your code with `standardrb --fix` before submitting.
393
+
394
+ ## License
395
+
396
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.9
1
+ 1.3.0
@@ -5,7 +5,7 @@ module Lumberjack
5
5
  class Context
6
6
  attr_reader :tags
7
7
 
8
- # @param [Context] parent_context A parent context to inherit tags from.
8
+ # @param parent_context [Context] A parent context to inherit tags from.
9
9
  def initialize(parent_context = nil)
10
10
  @tags = {}
11
11
  @tags.merge!(parent_context.tags) if parent_context
@@ -13,7 +13,7 @@ module Lumberjack
13
13
 
14
14
  # Set tags on the context.
15
15
  #
16
- # @param [Hash] tags The tags to set.
16
+ # @param tags [Hash] The tags to set.
17
17
  # @return [void]
18
18
  def tag(tags)
19
19
  tags.each do |key, value|
@@ -23,7 +23,7 @@ module Lumberjack
23
23
 
24
24
  # Get a context tag.
25
25
  #
26
- # @param [String, Symbol] key The tag key.
26
+ # @param key [String, Symbol] The tag key.
27
27
  # @return [Object] The tag value.
28
28
  def [](key)
29
29
  @tags[key.to_s]
@@ -31,8 +31,8 @@ module Lumberjack
31
31
 
32
32
  # Set a context tag.
33
33
  #
34
- # @param [String, Symbol] key The tag key.
35
- # @param [Object] value The tag value.
34
+ # @param key [String, Symbol] The tag key.
35
+ # @param value [Object] The tag value.
36
36
  # @return [void]
37
37
  def []=(key, value)
38
38
  @tags[key.to_s] = value
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
4
 
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  require "fileutils"
4
4
 
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Device
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Device
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Device
@@ -18,7 +18,7 @@ module Lumberjack
18
18
  def initialize(path, options = {})
19
19
  @path = File.expand_path(path)
20
20
  @keep = options[:keep]
21
- super(path, options)
21
+ super
22
22
  @file_inode = begin
23
23
  stream.lstat.ino
24
24
  rescue
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Device
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Device
@@ -55,9 +55,8 @@ module Lumberjack
55
55
  # :additional_lines and :time_format options will be passed through to the
56
56
  # Template constuctor.
57
57
  #
58
- # The default template is "[:time :severity :progname(:pid) #:unit_of_work_id] :message"
59
- # with additional lines formatted as "\n [#:unit_of_work_id] :message". The unit of
60
- # work id will only appear if it is present.
58
+ # The default template is "[:time :severity :progname(:pid)] :message"
59
+ # with additional lines formatted as "\n :message".
61
60
  #
62
61
  # The size of the internal buffer in bytes can be set by providing :buffer_size (defaults to 32K).
63
62
  #
@@ -68,12 +67,12 @@ module Lumberjack
68
67
  @stream = stream
69
68
  @stream.sync = true if @stream.respond_to?(:sync=)
70
69
  @buffer = Buffer.new
71
- @buffer_size = (options[:buffer_size] || 0)
72
- template = (options[:template] || DEFAULT_FIRST_LINE_TEMPLATE)
70
+ @buffer_size = options[:buffer_size] || 0
71
+ template = options[:template] || DEFAULT_FIRST_LINE_TEMPLATE
73
72
  if template.respond_to?(:call)
74
73
  @template = template
75
74
  else
76
- additional_lines = (options[:additional_lines] || DEFAULT_ADDITIONAL_LINES_TEMPLATE)
75
+ additional_lines = options[:additional_lines] || DEFAULT_ADDITIONAL_LINES_TEMPLATE
77
76
  @template = Template.new(template, additional_lines: additional_lines, time_format: options[:time_format])
78
77
  end
79
78
  end
@@ -150,6 +149,13 @@ module Lumberjack
150
149
  end
151
150
  end
152
151
 
152
+ # Return the underlying stream. Provided for API compatibility with Logger devices.
153
+ #
154
+ # @return [IO] The underlying stream.
155
+ def dev
156
+ @stream
157
+ end
158
+
153
159
  protected
154
160
 
155
161
  # Set the underlying stream.
@@ -163,8 +169,6 @@ module Lumberjack
163
169
  def write_to_stream(lines)
164
170
  return if lines.empty?
165
171
  lines = lines.first if lines.is_a?(Array) && lines.size == 1
166
-
167
- out = nil
168
172
  out = if lines.is_a?(Array)
169
173
  "#{lines.join(Lumberjack::LINE_SEPARATOR)}#{Lumberjack::LINE_SEPARATOR}"
170
174
  else
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  # This is an abstract class for logging devices. Subclasses must implement the +write+ method and
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter
@@ -16,7 +16,7 @@ module Lumberjack
16
16
  if @format && obj.respond_to?(:strftime)
17
17
  obj.strftime(@format)
18
18
  elsif obj.respond_to?(:iso8601)
19
- obj.iso8601
19
+ obj.iso8601(6)
20
20
  else
21
21
  obj.to_s
22
22
  end
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter
@@ -16,7 +16,7 @@ module Lumberjack
16
16
  end
17
17
 
18
18
  def call(exception)
19
- message = "#{exception.class.name}: #{exception.message}"
19
+ message = +"#{exception.class.name}: #{exception.message}"
20
20
  trace = exception.backtrace
21
21
  if trace
22
22
  trace = clean_backtrace(trace)
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lumberjack
4
+ class Formatter
5
+ # This formatter can be used to multiply a numeric value by a specified multiplier and
6
+ # optionally round to a specified number of decimal places.
7
+ class MultiplyFormatter
8
+ # @param multiplier [Numeric] The multiplier to apply to the value.
9
+ # @param decimals [Integer, nil] The number of decimal places to round the result to.
10
+ # If nil, no rounding is applied.
11
+ def initialize(multiplier, decimals = nil)
12
+ @multiplier = multiplier
13
+ @decimals = decimals
14
+ end
15
+
16
+ def call(value)
17
+ return value unless value.is_a?(Numeric)
18
+
19
+ value *= @multiplier
20
+ value = value.round(@decimals) if @decimals
21
+ value
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  require "pp"
4
4
  require "stringio"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lumberjack
4
+ class Formatter
5
+ # Log sensitive information in a redacted format showing the firat and last
6
+ # characters of the value, with the rest replaced by asterisks. The number of
7
+ # characters shown is dependent onthe length of the value; short values will
8
+ # not show any characters in order to avoid revealing too much information.
9
+ class RedactFormatter
10
+ def call(obj)
11
+ return obj unless obj.is_a?(String)
12
+
13
+ if obj.length > 8
14
+ "#{obj[0..1]}#{"*" * (obj.length - 4)}#{obj[-2..-1]}"
15
+ elsif obj.length > 5
16
+ "#{obj[0]}#{"*" * (obj.length - 2)}#{obj[-1]}"
17
+ else
18
+ "*****"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lumberjack
4
+ class Formatter
5
+ # Round numeric values to a set number of decimal places. This is useful when logging
6
+ # floating point numbers to reduce noise and rounding errors in the logs.
7
+ class RoundFormatter
8
+ def initialize(precision = 3)
9
+ @precision = precision
10
+ end
11
+
12
+ def call(obj)
13
+ if obj.is_a?(Numeric)
14
+ obj.round(@precision)
15
+ else
16
+ obj
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literals: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
4
  class Formatter