lumberjack 1.2.10 → 1.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5ab13ef762e8cdcb989ce1937c117fdbf938edc723c48f338b74edcd5d8e776
4
- data.tar.gz: a003d08ef71531551083e4794807d3469fd9691abdc0686e5cc36e6859199922
3
+ metadata.gz: 9350195577ae682b1f668a183aed2c0e1635e2bad8abb9af751c9f11f4b33fee
4
+ data.tar.gz: 3ebedf23e6eb462df564030b332ee9fd0f71d9198d6276480405ca21ce28d3f6
5
5
  SHA512:
6
- metadata.gz: b0b527513bd37ca507bfe63c83cd00831f544a9ecdcedc13aeb566a3e74ae4ff0469eefed220da79c7cbeb385aa0fcff249b2480d74c95a1ff21899a8e723afb
7
- data.tar.gz: 109411706ed4976f9711d6fcc17dd93bd7700cf72e01f40eaadba5701284e4175aa3791a311a19ab78eaedc34270ff9b16cd4ca394bb01a90b6c29d8e295f8e0
6
+ metadata.gz: 6c8230a0536fe98b8f817c275a5cc6e9d5764ea5dab8f0b1b92e1c274280c1528cebfb94767538eecf886240dc6a3fa6fe6adededffaac38815577bcfb739675
7
+ data.tar.gz: 3e64d451e6aa85298c3a5877bcfcef109381acd09553a250861886528ea352e5f5194db323cf0c253f2018dc130f4323f21ed1d51753cfcad7c3dbb09390affc
data/ARCHITECTURE.md ADDED
@@ -0,0 +1,244 @@
1
+ # Lumberjack Gem Architecture
2
+
3
+ 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. The gem is designed with structured logging in mind, but can be used for simple text logging as well.
4
+
5
+ ## Overview
6
+
7
+ The Lumberjack architecture follows a clean separation of concerns with the following main components:
8
+
9
+ - **Logger**: The main interface for creating log entries
10
+ - **LogEntry**: Data structure that captures log messages and metadata
11
+ - **Device**: Abstraction for different output destinations
12
+ - **Formatter**: Handles message formatting and transformation
13
+ - **TagFormatter**: Specialized formatting for tags
14
+ - **Template**: Template engine for customizing log output format
15
+ - **Context**: Thread-local context for managing tags across log entries
16
+ - **Severity**: Log level management and filtering
17
+
18
+ ## Core Architecture
19
+
20
+ ```mermaid
21
+ classDiagram
22
+ class Logger {
23
+ +Device device
24
+ +Formatter formatter
25
+ +TagFormatter tag_formatter
26
+ +Integer level
27
+ +String progname
28
+ +Hash tags
29
+ +initialize(device, options)
30
+ +debug(message, progname, tags)
31
+ +info(message, progname, tags)
32
+ +warn(message, progname, tags)
33
+ +error(message, progname, tags)
34
+ +fatal(message, progname, tags)
35
+ +add_entry(severity, message, progname, tags)
36
+ +tag(tags_hash)
37
+ +flush()
38
+ +close()
39
+ }
40
+
41
+ class LogEntry {
42
+ +Time time
43
+ +Integer severity
44
+ +Object message
45
+ +String progname
46
+ +Integer pid
47
+ +Hash tags
48
+ +initialize(time, severity, message, progname, pid, tags)
49
+ +severity_label()
50
+ +tag(name)
51
+ +to_s()
52
+ }
53
+
54
+ class Device {
55
+ <<abstract>>
56
+ +write(entry)*
57
+ +flush()
58
+ +close()
59
+ +reopen()
60
+ +datetime_format()
61
+ +datetime_format=(format)
62
+ }
63
+
64
+ class Formatter {
65
+ +Hash class_formatters
66
+ +Hash module_formatters
67
+ +add(classes, formatter)
68
+ +remove(classes)
69
+ +format(message)
70
+ +clear()
71
+ }
72
+
73
+ class TagFormatter {
74
+ +Hash formatters
75
+ +Object default_formatter
76
+ +default(formatter)
77
+ +add(names, formatter)
78
+ +remove(names)
79
+ +format(tags)
80
+ +clear()
81
+ }
82
+
83
+ class Template {
84
+ +String first_line_template
85
+ +String additional_line_template
86
+ +String datetime_format
87
+ +compile(template)
88
+ +call(entry)
89
+ +datetime_format=(format)
90
+ }
91
+
92
+ class Context {
93
+ +Hash tags
94
+ +initialize(parent_context)
95
+ +tag(tags)
96
+ +\[](key)
97
+ +\[]=(key, value)
98
+ +reset()
99
+ }
100
+
101
+ class Severity {
102
+ <<module>>
103
+ +level_to_label(severity)
104
+ +label_to_level(label)
105
+ +coerce(value)
106
+ }
107
+
108
+ Logger --> LogEntry : creates
109
+ Logger --> Device : writes to
110
+ Logger --> Formatter : uses
111
+ Logger --> TagFormatter : uses
112
+ Logger --> Severity : includes
113
+ Device --> Template : may use
114
+ Formatter --> LogEntry : formats message
115
+ TagFormatter --> LogEntry : formats tags
116
+ Logger <-- Context : provides tags
117
+ ```
118
+
119
+ ## Device Hierarchy
120
+
121
+ The Device system provides a pluggable architecture for different output destinations:
122
+
123
+ ```mermaid
124
+ classDiagram
125
+ class Device {
126
+ <<abstract>>
127
+ +write(entry)*
128
+ +flush()
129
+ +close()
130
+ +reopen()
131
+ }
132
+
133
+ class Writer {
134
+ +IO stream
135
+ +Template template
136
+ +Buffer buffer
137
+ +Integer buffer_size
138
+ +write(entry)
139
+ +flush()
140
+ +before_flush()
141
+ }
142
+
143
+ class LogFile {
144
+ +String file_path
145
+ +write(entry)
146
+ +reopen(file_path)
147
+ +close()
148
+ }
149
+
150
+ class RollingLogFile {
151
+ <<abstract>>
152
+ +roll_file?()
153
+ +roll_file!()
154
+ +archive_file_suffix()
155
+ }
156
+
157
+ class DateRollingLogFile {
158
+ +String roll
159
+ +roll_file?()
160
+ +archive_file_suffix()
161
+ }
162
+
163
+ class SizeRollingLogFile {
164
+ +Integer max_size
165
+ +Integer keep
166
+ +roll_file?()
167
+ +archive_file_suffix()
168
+ }
169
+
170
+ class Multi {
171
+ +Array~Device~ devices
172
+ +write(entry)
173
+ +flush()
174
+ +close()
175
+ }
176
+
177
+ class Null {
178
+ +write(entry)
179
+ }
180
+
181
+ Device <|-- Writer
182
+ Device <|-- Multi
183
+ Device <|-- Null
184
+ Writer <|-- LogFile
185
+ LogFile <|-- RollingLogFile
186
+ RollingLogFile <|-- DateRollingLogFile
187
+ RollingLogFile <|-- SizeRollingLogFile
188
+ Multi --> Device : contains multiple
189
+ ```
190
+
191
+ ## Data Flow
192
+
193
+ The logging process follows this flow:
194
+
195
+ ```mermaid
196
+ sequenceDiagram
197
+ participant Client
198
+ participant Logger
199
+ participant Formatter
200
+ participant TagFormatter
201
+ participant LogEntry
202
+ participant Device
203
+ participant Template
204
+
205
+ Client->>Logger: info("message", tags: {key: value})
206
+ Logger->>Logger: Check severity level
207
+ Logger->>Formatter: format(message)
208
+ Formatter-->>Logger: formatted_message
209
+ Logger->>TagFormatter: format(tags)
210
+ TagFormatter-->>Logger: formatted_tags
211
+ Logger->>LogEntry: new(time, severity, message, progname, pid, tags)
212
+ LogEntry-->>Logger: log_entry
213
+ Logger->>Device: write(log_entry)
214
+ Device->>Template: call(log_entry) [if Writer device]
215
+ Template-->>Device: formatted_string
216
+ Device->>Device: Write to output destination
217
+ ```
218
+
219
+ ## Key Features
220
+
221
+ ### Thread Safety
222
+ - Logger operations are thread-safe
223
+ - Context provides thread-local tag storage
224
+ - Devices handle concurrent writes appropriately
225
+
226
+ ### Structured Logging
227
+ - LogEntry captures structured data beyond just the message
228
+ - Tags provide key-value metadata
229
+ - Formatters can handle complex object serialization
230
+
231
+ ### Pluggable Architecture
232
+ - Device abstraction allows custom output destinations
233
+ - Formatter system enables custom message transformation
234
+ - TagFormatter provides specialized tag handling
235
+
236
+ ### Performance Optimization
237
+ - Buffered writing in Writer devices
238
+ - Lazy evaluation of expensive operations
239
+ - Configurable flush intervals
240
+
241
+ ### ActiveSupport Compatibility
242
+ - TaggedLoggerSupport module provides Rails compatibility
243
+ - Compatible API with standard library Logger
244
+ - Supports ActiveSupport::TaggedLogging interface
data/CHANGELOG.md CHANGED
@@ -4,6 +4,58 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.3.2
8
+
9
+ ### Fixed
10
+
11
+ - Fixed `NoMethodError` when setting the device via the `Lumberjack::Logger#device=` method.
12
+
13
+ ## 1.3.1
14
+
15
+ ### Added
16
+
17
+ - Added `Lumberjack::Logger#context` method to set up a context block for the logger. This is the same as calling `Lumberjack::Logger#tag` with an empty hash.
18
+ - Log entries now remove empty tag values so they don't have to be removed downstream.
19
+
20
+ ### Fixed
21
+
22
+ - ActiveSupport::TaggedLogger now calls `Lumberjack::Logger#tag_globally` to prevent deprecation warnings.
23
+
24
+ ## 1.3.0
25
+
26
+ ### Added
27
+
28
+ - Added `Lumberjack::Formatter::TaggedMessage` to allow extracting tags from log messages via a formatter in order to better support structured logging of objects.
29
+ - Added built in `:round` formatter to round numbers to a specified number of decimal places.
30
+ - Added built in `:redact` formatter to redact sensitive information from log tags.
31
+ - Added support in `Lumberjack::TagFormatter` for class formatters. Class formatters will be applied to any tag values that match the class.
32
+ - Apply formatters to enumerable values in tags. Name formatters are applied using dot syntax when a tag value contains a hash.
33
+ - Added support for a dedicated message formatter that can override the default formatter on the log message.
34
+ - Added support for setting tags from the request environment in `Lumberjack::Rack::Context` middleware.
35
+ - Added helper methods to generate global PID's and thread ids.
36
+ - Added `Lumberjack::Logger#tag_globally` to explicitly set a global tag for all loggers.
37
+ - Added `Lumberjack::Logger#tag_value` to get the value of a tag by name from the current tag context.
38
+ - Added `Lumberjack::Utils.hostname` to get the hostname in UTF-8 encoding.
39
+ - Added `Lumberjack::Utils.global_pid` to get a global process id in a consistent format.
40
+ - Added `Lumberjack::Utils.global_thread_id` to get a thread id in a consistent format.
41
+ - Added `Lumberjack::Utils.thread_name` to get a thread name in a consistent format.
42
+ - Added support for `ActiveSupport::Logging.logger_outputs_to?` to check if a logger is outputting to a specific IO stream.
43
+ - Added `Lumberjack::Logger#log_at` method to temporarily set the log level for a block of code for compatibility with ActiveSupport loggers.
44
+
45
+ ### Changed
46
+
47
+ - Default date/time format for log entries is now ISO-8601 with microsecond precision.
48
+ - Tags that are set to hash values will now be flattened into dot-separated keys in templates.
49
+
50
+ ### Removed
51
+
52
+ - Removed support for Ruby versions < 2.5.
53
+
54
+ ### Deprecated
55
+
56
+ - All unit of work related functionality from version 1.0 has been officially deprecated and will be removed in version 2.0. Use tags instead to set a global context for log entries.
57
+ - Calling `Lumberjack::Logger#tag` without a block is deprecated. Use `Lumberjack::Logger#tag_globally` instead.
58
+
7
59
  ## 1.2.10
8
60
 
9
61
  ### Added
data/README.md CHANGED
@@ -1,15 +1,14 @@
1
1
  # Lumberjack
2
2
 
3
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
- [![Regression Test](https://github.com/bdurand/lumberjack/actions/workflows/regression_test.yml/badge.svg)](https://github.com/bdurand/lumberjack/actions/workflows/regression_test.yml)
5
4
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
6
5
  [![Gem Version](https://badge.fury.io/rb/lumberjack.svg)](https://badge.fury.io/rb/lumberjack)
7
6
 
8
- 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.
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.
9
8
 
10
9
  ## Usage
11
10
 
12
- 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.
13
12
 
14
13
  ```ruby
15
14
  logger = Lumberjack::Logger.new("logs/application.log") # Open a new log file with INFO level
@@ -24,27 +23,25 @@ This code aims to be extremely simple to use. The core interface is the Lumberja
24
23
  logger.info("End request")
25
24
  ```
26
25
 
27
- This is all you need to know to log messages.
28
-
29
26
  ## Features
30
27
 
31
- ### Meta data
28
+ ### Metadata
32
29
 
33
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.
34
31
 
35
32
  The following information is recorded for each message:
36
33
 
37
- * severity - The severity recorded for the message.
38
- * time - The time at which the message was recorded.
39
- * program name - The name of the program logging the message. This can be either set for all messages or customized with each message.
40
- * process id - The process id (pid) of the process that logged the message.
41
- * 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.
42
39
 
43
40
  ### Tags
44
41
 
45
- 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.
46
43
 
47
- 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:
48
45
 
49
46
  ```ruby
50
47
  logger.info("request completed", duration: elapsed_time, status: response.status)
@@ -89,9 +86,55 @@ logger.info("no requests") # Will not include the `request_id` tag
89
86
 
90
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.
91
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
+
92
135
  #### Compatibility with ActiveSupport::TaggedLogging
93
136
 
94
- `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.
95
138
 
96
139
  ```ruby
97
140
  logger.tagged("foo", "bar=1", "other") do
@@ -103,38 +146,37 @@ end
103
146
 
104
147
  The built in `Lumberjack::Device::Writer` class has built in support for including tags in the output using the `Lumberjack::Template` class.
105
148
 
106
- 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.
107
-
108
- #### Unit Of Work
109
-
110
- 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.
111
150
 
112
151
  ### Pluggable Devices
113
152
 
114
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.
115
154
 
116
- * Lumberjack::Device::Writer - Writes log entries to an IO stream.
117
- * Lumberjack::Device::LogFile - Writes log entries to a file.
118
- * Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
119
- * Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
120
- * Lumberjack::Device::Multi - This device wraps mulitiple other devices and will write log entries to each of them.
121
- * 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.
122
161
 
123
- 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:
124
163
 
125
- * [lumberjack_syslog_device](https://github.com/bdurand/lumberjack_syslog_device) - send your log messages to the system wide syslog service
126
- * [lumberjack_mongo_device](https://github.com/bdurand/lumberjack_mongo_device) - store your log messages to a [MongoDB](http://www.mongodb.org/) NoSQL data store
127
- * [lumberjack-couchdb-driver](https://github.com/narkisr/lumberjack-couchdb-driver) - store your log messages to a [CouchDB](http://couchdb.apache.org/) NoSQL data store
128
- * [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.
129
171
 
130
172
  ### Customize Formatting
131
173
 
132
174
  #### Formatters
133
175
 
134
- 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.
135
177
 
136
178
  ```ruby
137
- # Format all floating point number with three significant digits.
179
+ # Format all floating point numbers with three significant digits.
138
180
  logger.formatter.add(Float) { |value| value.round(3) }
139
181
 
140
182
  # Format all enumerable objects as a comma delimited string.
@@ -148,25 +190,53 @@ There are several built in classes you can add as formatters. You can use a symb
148
190
  logger.formatter.add(Hash, Lumberjack::Formatter::PrettyPrintFormatter.new) # alternative using a formatter instance
149
191
  ```
150
192
 
151
- * `:object` - `Lumberjack::Formatter::ObjectFormatter` - no op conversion that returns the object itself.
152
- * `:string` - `Lumberjack::Formatter::StringFormatter` - calls `to_s` on the object.
153
- * `:strip` - `Lumberjack::Formatter::StripFormatter` - calls `to_s.strip` on the object.
154
- * `:inspect` - `Lumberjack::Formatter::InspectFormatter` - calls `inspect` on the object.
155
- * `:exception` - `Lumberjack::Formatter::ExceptionFormatter` - special formatter for exceptions which logs them as multi line statements with the message and backtrace.
156
- * `:date_time` - `Lumberjack::Formatter::DateTimeFormatter` - special formatter for dates and times to format them using `strftime`.
157
- * `:pretty_print` - `Lumberjack::Formatter::PrettyPrintFormatter` - returns the pretty print format of the object.
158
- * `:id` - `Lumberjack::Formatter::IdFormatter` - returns a hash of the object with keys for the id attribute and class.
159
- * `: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.).
160
202
 
161
203
  To define your own formatter, either provide a block or an object that responds to `call` with a single argument.
162
204
 
205
+ #### Default Formatter
206
+
207
+ The default formatter is applied to all objects being logged. This includes both messages and tags.
208
+
163
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.
164
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
+
165
235
  #### Tag Formatters
166
236
 
167
- 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.
168
238
 
169
- 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.
170
240
 
171
241
  ```ruby
172
242
  # These will all do the same thing formatting all tag values with `inspect`
@@ -177,11 +247,20 @@ logger.tag_formatter.default { |value| value.inspect }
177
247
  # This will register formatters only on specific tag names
178
248
  logger.tag_formatter.add(:thread) { |thread| "Thread(#{thread.name})" }
179
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"}
180
259
  ```
181
260
 
182
261
  #### Templates
183
262
 
184
- 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.
185
264
 
186
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.
187
266
 
@@ -203,7 +282,7 @@ See `Lumberjack::Template` for a complete list of macros you can use in the temp
203
282
 
204
283
  ### Buffered Logging
205
284
 
206
- 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.
207
286
 
208
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.
209
288
 
@@ -223,28 +302,38 @@ The built in devices include two that can automatically roll log files based eit
223
302
 
224
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.
225
304
 
226
- ## 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.
310
+
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
227
314
 
228
- `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.
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.
229
316
 
230
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.
231
318
 
232
- 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.
233
320
 
234
321
  ## Examples
235
322
 
236
- 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.
237
324
 
238
325
  In a Rails application you can replace the default production logger by adding this to your config/environments/production.rb file:
239
326
 
240
327
  ```ruby
241
- # Use the ActionDispatch request id as the unit of work id. This will use just the first chunk of the request id.
242
- # If you want to use an abbreviated request id for terseness, change the last argument to `true`
243
- config.middleware.insert_after ActionDispatch::RequestId, Lumberjack::Rack::RequestId, false
244
- # Use a custom unit of work id to each request
245
- # 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
+ )
246
334
  # Change the logger to use Lumberjack
247
- 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
248
337
  config.logger = Lumberjack::Logger.new(log_file, :level => :warn)
249
338
  ```
250
339
 
@@ -283,12 +372,12 @@ To send log messages to syslog instead of to a file, you could use this (require
283
372
  Add this line to your application's Gemfile:
284
373
 
285
374
  ```ruby
286
- gem 'lumberjack'
375
+ gem "lumberjack"
287
376
  ```
288
377
 
289
378
  And then execute:
290
379
  ```bash
291
- $ bundle
380
+ $ bundle install
292
381
  ```
293
382
 
294
383
  Or install it yourself as:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.10
1
+ 1.3.2