lumberjack 1.2.7 → 1.4.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 +4 -4
- data/ARCHITECTURE.md +244 -0
- data/CHANGELOG.md +251 -56
- data/README.md +197 -62
- data/VERSION +1 -1
- data/lib/lumberjack/context.rb +25 -5
- data/lib/lumberjack/device/date_rolling_log_file.rb +17 -8
- data/lib/lumberjack/device/log_file.rb +14 -7
- data/lib/lumberjack/device/multi.rb +8 -7
- data/lib/lumberjack/device/null.rb +2 -2
- data/lib/lumberjack/device/rolling_log_file.rb +46 -22
- data/lib/lumberjack/device/size_rolling_log_file.rb +10 -10
- data/lib/lumberjack/device/writer.rb +45 -21
- data/lib/lumberjack/device.rb +28 -13
- data/lib/lumberjack/formatter/date_time_formatter.rb +5 -5
- data/lib/lumberjack/formatter/exception_formatter.rb +4 -4
- data/lib/lumberjack/formatter/id_formatter.rb +4 -3
- data/lib/lumberjack/formatter/inspect_formatter.rb +1 -1
- data/lib/lumberjack/formatter/multiply_formatter.rb +25 -0
- data/lib/lumberjack/formatter/object_formatter.rb +1 -1
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +7 -5
- data/lib/lumberjack/formatter/redact_formatter.rb +23 -0
- data/lib/lumberjack/formatter/round_formatter.rb +21 -0
- data/lib/lumberjack/formatter/string_formatter.rb +1 -1
- data/lib/lumberjack/formatter/strip_formatter.rb +1 -1
- data/lib/lumberjack/formatter/structured_formatter.rb +3 -1
- data/lib/lumberjack/formatter/tagged_message.rb +39 -0
- data/lib/lumberjack/formatter/truncate_formatter.rb +27 -0
- data/lib/lumberjack/formatter.rb +96 -28
- data/lib/lumberjack/log_entry.rb +90 -19
- data/lib/lumberjack/logger.rb +318 -86
- data/lib/lumberjack/rack/context.rb +21 -2
- data/lib/lumberjack/rack/request_id.rb +8 -4
- data/lib/lumberjack/rack/unit_of_work.rb +7 -3
- data/lib/lumberjack/rack.rb +4 -4
- data/lib/lumberjack/severity.rb +22 -3
- data/lib/lumberjack/tag_context.rb +78 -0
- data/lib/lumberjack/tag_formatter.rb +124 -25
- data/lib/lumberjack/tagged_logger_support.rb +26 -12
- data/lib/lumberjack/tagged_logging.rb +1 -1
- data/lib/lumberjack/tags.rb +8 -8
- data/lib/lumberjack/template.rb +17 -5
- data/lib/lumberjack/utils.rb +182 -0
- data/lib/lumberjack.rb +64 -35
- data/lumberjack.gemspec +18 -15
- metadata +23 -54
data/README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
# Lumberjack
|
|
2
2
|
|
|
3
|
-
[](https://github.com/bdurand/lumberjack/actions/workflows/continuous_integration.yml)
|
|
4
|
+
[](https://github.com/testdouble/standard)
|
|
5
|
+
[](https://badge.fury.io/rb/lumberjack)
|
|
5
6
|
|
|
6
|
-
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.
|
|
7
8
|
|
|
8
9
|
## Usage
|
|
9
10
|
|
|
10
|
-
This code aims to be extremely simple to use. The core interface
|
|
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.
|
|
11
12
|
|
|
12
13
|
```ruby
|
|
13
14
|
logger = Lumberjack::Logger.new("logs/application.log") # Open a new log file with INFO level
|
|
@@ -22,27 +23,25 @@ This code aims to be extremely simple to use. The core interface it the Lumberja
|
|
|
22
23
|
logger.info("End request")
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
This is all you need to know to log messages.
|
|
26
|
-
|
|
27
26
|
## Features
|
|
28
27
|
|
|
29
|
-
###
|
|
28
|
+
### Metadata
|
|
30
29
|
|
|
31
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.
|
|
32
31
|
|
|
33
32
|
The following information is recorded for each message:
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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.
|
|
40
39
|
|
|
41
40
|
### Tags
|
|
42
41
|
|
|
43
|
-
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
|
|
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.
|
|
44
43
|
|
|
45
|
-
Each of the logger methods includes an additional argument that can be used to specify tags on a
|
|
44
|
+
Each of the logger methods includes an additional argument that can be used to specify tags on a message:
|
|
46
45
|
|
|
47
46
|
```ruby
|
|
48
47
|
logger.info("request completed", duration: elapsed_time, status: response.status)
|
|
@@ -51,7 +50,7 @@ logger.info("request completed", duration: elapsed_time, status: response.status
|
|
|
51
50
|
You can also specify tags on a logger that will be included with every log message.
|
|
52
51
|
|
|
53
52
|
```ruby
|
|
54
|
-
logger.
|
|
53
|
+
logger.tag_globally(host: Socket.gethostname.force_encoding("UTF-8"))
|
|
55
54
|
```
|
|
56
55
|
|
|
57
56
|
You can specify tags that will only be applied to the logger in a block as well.
|
|
@@ -65,31 +64,92 @@ end
|
|
|
65
64
|
logger.info("there") # Will not include the `thread_id` or `count` tag
|
|
66
65
|
```
|
|
67
66
|
|
|
67
|
+
The block opens up a new tag context. The context applies only within a block and only for the currently executing thread. You can also open up a new context block without specifying any tags and then add tags to the context within the block.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
logger.context do # When a block is given to `context`, it opens a new empty tag context.
|
|
71
|
+
# `context` can be called without a block, to get an TagContext object for manipulating tags.
|
|
72
|
+
logger.context.tag(user_id: current_user.id, username: current_user.username)
|
|
73
|
+
logger.context["user_role"] = current_user.role # You can also use hash syntax to set tags.
|
|
74
|
+
logger.info("user logged in") # Will include the `user_id`, `username`, and `user_role` tags.
|
|
75
|
+
end
|
|
76
|
+
logger.info("no user") # Will not include the tags
|
|
77
|
+
```
|
|
78
|
+
|
|
68
79
|
You can also set tags to `Proc` objects that will be evaluated when creating a log entry.
|
|
69
80
|
|
|
70
81
|
```ruby
|
|
71
|
-
logger.
|
|
82
|
+
logger.tag_globally(thread_id: lambda { Thread.current.object_id })
|
|
83
|
+
|
|
72
84
|
Thread.new do
|
|
73
85
|
logger.info("inside thread") # Will include the `thread_id` tag with id of the spawned thread
|
|
74
86
|
end
|
|
87
|
+
|
|
75
88
|
logger.info("outside thread") # Will include the `thread_id` tag with id of the main thread
|
|
76
89
|
```
|
|
77
90
|
|
|
78
|
-
Finally, you can specify a logging context
|
|
91
|
+
Finally, you can specify a global logging context that applies to all loggers.
|
|
79
92
|
|
|
80
93
|
```ruby
|
|
81
94
|
Lumberjack.context do
|
|
82
|
-
Lumberjack.tag(request_id: SecureRandom.hex)
|
|
95
|
+
Lumberjack.tag(request_id: SecureRandom.hex) # add a global context tag
|
|
83
96
|
logger.info("begin request") # Will include the `request_id` tag
|
|
97
|
+
event_logger.info("http.request") # Will also include the `request_id` tag
|
|
84
98
|
end
|
|
85
99
|
logger.info("no requests") # Will not include the `request_id` tag
|
|
86
100
|
```
|
|
87
101
|
|
|
88
102
|
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.
|
|
89
103
|
|
|
104
|
+
#### Structured Logging with Tags
|
|
105
|
+
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
# Instead of this (harder to parse)
|
|
110
|
+
logger.info("User john_doe logged in from IP 192.168.1.100 in 0.25 seconds")
|
|
111
|
+
|
|
112
|
+
# Do this (structured and parseable)
|
|
113
|
+
logger.info("User logged in", {
|
|
114
|
+
user_id: "john_doe",
|
|
115
|
+
ip_address: "192.168.1.100",
|
|
116
|
+
duration: 0.25,
|
|
117
|
+
action: "login"
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
This approach provides several benefits:
|
|
122
|
+
|
|
123
|
+
- **Consistent message format** - The base message stays the same while data varies
|
|
124
|
+
- **Easy filtering and searching** - You can search by specific tag values
|
|
125
|
+
- **Better analytics** - Aggregate data by tag values (e.g., average login duration)
|
|
126
|
+
- **Machine processing** - Automated systems can easily extract and process tag data
|
|
127
|
+
|
|
128
|
+
You can also use nested structures in tags for complex data:
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
logger.info("API request completed", {
|
|
132
|
+
request: {
|
|
133
|
+
method: "POST",
|
|
134
|
+
path: "/api/users",
|
|
135
|
+
user_agent: request.user_agent
|
|
136
|
+
},
|
|
137
|
+
response: {
|
|
138
|
+
status: 201,
|
|
139
|
+
duration_ms: 150
|
|
140
|
+
},
|
|
141
|
+
user: {
|
|
142
|
+
id: current_user.id,
|
|
143
|
+
role: current_user.role
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
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.
|
|
149
|
+
|
|
90
150
|
#### Compatibility with ActiveSupport::TaggedLogging
|
|
91
151
|
|
|
92
|
-
`Lumberjack::Logger`
|
|
152
|
+
`Lumberjack::Logger` 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.
|
|
93
153
|
|
|
94
154
|
```ruby
|
|
95
155
|
logger.tagged("foo", "bar=1", "other") do
|
|
@@ -101,38 +161,37 @@ end
|
|
|
101
161
|
|
|
102
162
|
The built in `Lumberjack::Device::Writer` class has built in support for including tags in the output using the `Lumberjack::Template` class.
|
|
103
163
|
|
|
104
|
-
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
|
|
105
|
-
|
|
106
|
-
#### Unit Of Work
|
|
107
|
-
|
|
108
|
-
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.
|
|
164
|
+
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.
|
|
109
165
|
|
|
110
166
|
### Pluggable Devices
|
|
111
167
|
|
|
112
168
|
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.
|
|
113
169
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
170
|
+
- Lumberjack::Device::Writer - Writes log entries to an IO stream.
|
|
171
|
+
- Lumberjack::Device::LogFile - Writes log entries to a file.
|
|
172
|
+
- Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
|
|
173
|
+
- Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
|
|
174
|
+
- Lumberjack::Device::Multi - This device wraps multiple other devices and will write log entries to each of them.
|
|
175
|
+
- Lumberjack::Device::Null - This device produces no output and is intended for testing environments.
|
|
120
176
|
|
|
121
|
-
If you'd like to send
|
|
177
|
+
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:
|
|
122
178
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
179
|
+
- [lumberjack_json_device](https://github.com/bdurand/lumberjack_json_device) - output your log messages as stream of JSON objects for structured logging.
|
|
180
|
+
- [lumberjack_syslog_device](https://github.com/bdurand/lumberjack_syslog_device) - send your log messages to the system wide syslog service
|
|
181
|
+
- [lumberjack_mongo_device](https://github.com/bdurand/lumberjack_mongo_device) - store your log messages to a [MongoDB](http://www.mongodb.org/) NoSQL data store
|
|
182
|
+
- [lumberjack_redis_device](https://github.com/bdurand/lumberjack_redis_device) - store your log messages in a [Redis](https://redis.io/) data store
|
|
183
|
+
- [lumberjack-couchdb-driver](https://github.com/narkisr/lumberjack-couchdb-driver) - store your log messages to a [CouchDB](http://couchdb.apache.org/) NoSQL data store
|
|
184
|
+
- [lumberjack_heroku_device](https://github.com/tonycoco/lumberjack_heroku_device) - log to Heroku's logging system
|
|
185
|
+
- [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.
|
|
127
186
|
|
|
128
187
|
### Customize Formatting
|
|
129
188
|
|
|
130
189
|
#### Formatters
|
|
131
190
|
|
|
132
|
-
The message you send to the logger can be any object type and does not need to be a string. You can specify
|
|
191
|
+
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.
|
|
133
192
|
|
|
134
193
|
```ruby
|
|
135
|
-
# Format all floating point
|
|
194
|
+
# Format all floating point numbers with three significant digits.
|
|
136
195
|
logger.formatter.add(Float) { |value| value.round(3) }
|
|
137
196
|
|
|
138
197
|
# Format all enumerable objects as a comma delimited string.
|
|
@@ -146,25 +205,54 @@ There are several built in classes you can add as formatters. You can use a symb
|
|
|
146
205
|
logger.formatter.add(Hash, Lumberjack::Formatter::PrettyPrintFormatter.new) # alternative using a formatter instance
|
|
147
206
|
```
|
|
148
207
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
208
|
+
- `:object` - `Lumberjack::Formatter::ObjectFormatter` - no op conversion that returns the object itself.
|
|
209
|
+
- `:string` - `Lumberjack::Formatter::StringFormatter` - calls `to_s` on the object.
|
|
210
|
+
- `:strip` - `Lumberjack::Formatter::StripFormatter` - calls `to_s.strip` on the object.
|
|
211
|
+
- `:inspect` - `Lumberjack::Formatter::InspectFormatter` - calls `inspect` on the object.
|
|
212
|
+
- `:exception` - `Lumberjack::Formatter::ExceptionFormatter` - special formatter for exceptions which logs them as multi-line statements with the message and backtrace.
|
|
213
|
+
- `:date_time` - `Lumberjack::Formatter::DateTimeFormatter` - special formatter for dates and times to format them using `strftime`.
|
|
214
|
+
- `:pretty_print` - `Lumberjack::Formatter::PrettyPrintFormatter` - returns the pretty print format of the object.
|
|
215
|
+
- `:id` - `Lumberjack::Formatter::IdFormatter` - returns a hash of the object with keys for the id attribute and class.
|
|
216
|
+
- `:structured` - `Lumberjack::Formatter::StructuredFormatter` - crawls the object and applies the formatter recursively to Enumerable objects found in it (arrays, hashes, etc.).
|
|
217
|
+
- `:truncate` - `Lumberjack::Formatter::TruncateFormatter` - truncates long strings to a specified length.
|
|
158
218
|
|
|
159
219
|
To define your own formatter, either provide a block or an object that responds to `call` with a single argument.
|
|
160
220
|
|
|
221
|
+
#### Default Formatter
|
|
222
|
+
|
|
223
|
+
The default formatter is applied to all objects being logged. This includes both messages and tags.
|
|
224
|
+
|
|
161
225
|
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.
|
|
162
226
|
|
|
227
|
+
#### Message Formatter
|
|
228
|
+
|
|
229
|
+
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.
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
logger.message_formatter.add(String, :truncate, 1000) # Will truncate all string messages to 1000 characters
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
##### Extracting Tags from Messages
|
|
236
|
+
|
|
237
|
+
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:
|
|
238
|
+
|
|
239
|
+
```ruby
|
|
240
|
+
logger.message_formatter.add(Exception, ->(e) {
|
|
241
|
+
Lumberjack::Formatter::TaggedMessage.new(e.inspect, {
|
|
242
|
+
"error.message": e.message,
|
|
243
|
+
"error.class": e.class.name,
|
|
244
|
+
"error.trace": e.backtrace
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
logger.error(exception) # Will log the exception and add tags for the message, class, and trace.
|
|
249
|
+
```
|
|
250
|
+
|
|
163
251
|
#### Tag Formatters
|
|
164
252
|
|
|
165
|
-
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
|
|
253
|
+
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.
|
|
166
254
|
|
|
167
|
-
The
|
|
255
|
+
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.
|
|
168
256
|
|
|
169
257
|
```ruby
|
|
170
258
|
# These will all do the same thing formatting all tag values with `inspect`
|
|
@@ -175,11 +263,20 @@ logger.tag_formatter.default { |value| value.inspect }
|
|
|
175
263
|
# This will register formatters only on specific tag names
|
|
176
264
|
logger.tag_formatter.add(:thread) { |thread| "Thread(#{thread.name})" }
|
|
177
265
|
logger.tag_formatter.add(:current_user, Lumberjack::Formatter::IdFormatter.new)
|
|
266
|
+
|
|
267
|
+
# You can also register formatters for tag values by class
|
|
268
|
+
logger.tag_formatter.add(Numeric, &:round)
|
|
269
|
+
|
|
270
|
+
# Tag formatters will be applied to nested hashes and arrays as well.
|
|
271
|
+
|
|
272
|
+
# Name formatters use dot syntax to apply to nested hashes.
|
|
273
|
+
logger.tag_formatter.add("user.username", &:upcase)
|
|
274
|
+
# logger.tag(user: {username: "john_doe"}) # Will log the tag as {"user" => "username" => "JOHN_DOE"}
|
|
178
275
|
```
|
|
179
276
|
|
|
180
277
|
#### Templates
|
|
181
278
|
|
|
182
|
-
If you use the built
|
|
279
|
+
If you use the built-in `Lumberjack::Device::Writer` derived devices, you can also customize the Template used to format the LogEntry.
|
|
183
280
|
|
|
184
281
|
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.
|
|
185
282
|
|
|
@@ -201,11 +298,11 @@ See `Lumberjack::Template` for a complete list of macros you can use in the temp
|
|
|
201
298
|
|
|
202
299
|
### Buffered Logging
|
|
203
300
|
|
|
204
|
-
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.
|
|
301
|
+
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.
|
|
205
302
|
|
|
206
303
|
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.
|
|
207
304
|
|
|
208
|
-
The built in stream based logging devices use an internal buffer. The size of the buffer (in bytes) can be set with the `:buffer_size` options when initializing a logger. The default behavior is to not
|
|
305
|
+
The built in stream based logging devices use an internal buffer. The size of the buffer (in bytes) can be set with the `:buffer_size` options when initializing a logger. The default behavior is to not buffer.
|
|
209
306
|
|
|
210
307
|
```ruby
|
|
211
308
|
# Set buffer to flush after 8K has been written to the log.
|
|
@@ -221,28 +318,38 @@ The built in devices include two that can automatically roll log files based eit
|
|
|
221
318
|
|
|
222
319
|
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.
|
|
223
320
|
|
|
224
|
-
##
|
|
321
|
+
## Integrations
|
|
322
|
+
|
|
323
|
+
Lumberjack has built in support for logging extensions in Rails.
|
|
324
|
+
|
|
325
|
+
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.
|
|
225
326
|
|
|
226
|
-
|
|
327
|
+
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.
|
|
328
|
+
|
|
329
|
+
## Differences from Standard Library Logger
|
|
330
|
+
|
|
331
|
+
`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.
|
|
227
332
|
|
|
228
333
|
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.
|
|
229
334
|
|
|
230
|
-
The logging methods (`debug`,
|
|
335
|
+
The logging methods (`debug`, `info`, `warn`, `error`, `fatal`) are overloaded with an additional argument for setting tags on the log entry.
|
|
231
336
|
|
|
232
337
|
## Examples
|
|
233
338
|
|
|
234
|
-
These
|
|
339
|
+
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.
|
|
235
340
|
|
|
236
341
|
In a Rails application you can replace the default production logger by adding this to your config/environments/production.rb file:
|
|
237
342
|
|
|
238
343
|
```ruby
|
|
239
|
-
#
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
344
|
+
# Add the ActionDispatch request id as a global tag on all log entries.
|
|
345
|
+
config.middleware.insert_after(
|
|
346
|
+
ActionDispatch::RequestId,
|
|
347
|
+
Lumberjack::Rack::Context,
|
|
348
|
+
request_id: ->(env) { env["action_dispatch.request_id"] }
|
|
349
|
+
)
|
|
244
350
|
# Change the logger to use Lumberjack
|
|
245
|
-
|
|
351
|
+
log_file = Rails.root + "log" + "#{Rails.env}.log"
|
|
352
|
+
# log_file = $stdout # or write to stdout instead of a file
|
|
246
353
|
config.logger = Lumberjack::Logger.new(log_file, :level => :warn)
|
|
247
354
|
```
|
|
248
355
|
|
|
@@ -270,8 +377,36 @@ To change the log message format to output JSON, you could use this code:
|
|
|
270
377
|
config.logger = Lumberjack::Logger.new(log_file_path, :template => lambda{|e| JSON.dump(time: e.time, level: e.severity_label, message: e.message)})
|
|
271
378
|
```
|
|
272
379
|
|
|
273
|
-
To send log messages to syslog instead of to a file, you could use this (
|
|
380
|
+
To send log messages to syslog instead of to a file, you could use this (requires the lumberjack_syslog_device gem):
|
|
274
381
|
|
|
275
382
|
```ruby
|
|
276
383
|
config.logger = Lumberjack::Logger.new(Lumberjack::SyslogDevice.new)
|
|
277
384
|
```
|
|
385
|
+
|
|
386
|
+
## Installation
|
|
387
|
+
|
|
388
|
+
Add this line to your application's Gemfile:
|
|
389
|
+
|
|
390
|
+
```ruby
|
|
391
|
+
gem "lumberjack"
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
And then execute:
|
|
395
|
+
```bash
|
|
396
|
+
$ bundle install
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
Or install it yourself as:
|
|
400
|
+
```bash
|
|
401
|
+
$ gem install lumberjack
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## Contributing
|
|
405
|
+
|
|
406
|
+
Open a pull request on GitHub.
|
|
407
|
+
|
|
408
|
+
Please use the [standardrb](https://github.com/testdouble/standard) syntax and lint your code with `standardrb --fix` before submitting.
|
|
409
|
+
|
|
410
|
+
## License
|
|
411
|
+
|
|
412
|
+
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
|
|
1
|
+
1.4.2
|
data/lib/lumberjack/context.rb
CHANGED
|
@@ -5,29 +5,49 @@ module Lumberjack
|
|
|
5
5
|
class Context
|
|
6
6
|
attr_reader :tags
|
|
7
7
|
|
|
8
|
+
# @param parent_context [Context] A parent context to inherit tags from.
|
|
8
9
|
def initialize(parent_context = nil)
|
|
9
10
|
@tags = {}
|
|
10
11
|
@tags.merge!(parent_context.tags) if parent_context
|
|
12
|
+
@tag_context = TagContext.new(@tags)
|
|
11
13
|
end
|
|
12
14
|
|
|
13
15
|
# Set tags on the context.
|
|
16
|
+
#
|
|
17
|
+
# @param tags [Hash] The tags to set.
|
|
18
|
+
# @return [void]
|
|
14
19
|
def tag(tags)
|
|
15
|
-
tags
|
|
16
|
-
@tags[key.to_s] = value
|
|
17
|
-
end
|
|
20
|
+
@tag_context.tag(tags)
|
|
18
21
|
end
|
|
19
22
|
|
|
20
23
|
# Get a context tag.
|
|
24
|
+
#
|
|
25
|
+
# @param key [String, Symbol] The tag key.
|
|
26
|
+
# @return [Object] The tag value.
|
|
21
27
|
def [](key)
|
|
22
|
-
@
|
|
28
|
+
@tag_context[key]
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
# Set a context tag.
|
|
32
|
+
#
|
|
33
|
+
# @param key [String, Symbol] The tag key.
|
|
34
|
+
# @param value [Object] The tag value.
|
|
35
|
+
# @return [void]
|
|
26
36
|
def []=(key, value)
|
|
27
|
-
@
|
|
37
|
+
@tag_context[key] = value
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Remove tags from the context.
|
|
41
|
+
#
|
|
42
|
+
# @param keys [Array<String, Symbol>] The tag keys to remove.
|
|
43
|
+
# @return [void]
|
|
44
|
+
def delete(*keys)
|
|
45
|
+
@tag_context.delete(*keys)
|
|
28
46
|
end
|
|
29
47
|
|
|
30
48
|
# Clear all the context data.
|
|
49
|
+
#
|
|
50
|
+
# @return [void]
|
|
31
51
|
def reset
|
|
32
52
|
@tags.clear
|
|
33
53
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require "date"
|
|
4
4
|
|
|
5
5
|
module Lumberjack
|
|
6
6
|
class Device
|
|
@@ -13,10 +13,13 @@ module Lumberjack
|
|
|
13
13
|
# Create a new logging device to the specified file. The period to roll the file is specified
|
|
14
14
|
# with the :roll option which may contain a value of :daily, :weekly,
|
|
15
15
|
# or :monthly.
|
|
16
|
+
#
|
|
17
|
+
# @param [String, Pathname] path The path to the log file.
|
|
18
|
+
# @param [Hash] options The options for the device.
|
|
16
19
|
def initialize(path, options = {})
|
|
17
20
|
@manual = options[:manual]
|
|
18
21
|
@file_date = Date.today
|
|
19
|
-
if options[:roll]
|
|
22
|
+
if options[:roll]&.to_s&.match(/(daily)|(weekly)|(monthly)/i)
|
|
20
23
|
@roll_period = $~[0].downcase.to_sym
|
|
21
24
|
options.delete(:roll)
|
|
22
25
|
else
|
|
@@ -25,17 +28,23 @@ module Lumberjack
|
|
|
25
28
|
super
|
|
26
29
|
end
|
|
27
30
|
|
|
31
|
+
# The date based suffix for file.
|
|
32
|
+
#
|
|
33
|
+
# @return [String]
|
|
28
34
|
def archive_file_suffix
|
|
29
35
|
case @roll_period
|
|
30
36
|
when :weekly
|
|
31
|
-
|
|
37
|
+
@file_date.strftime("week-of-%Y-%m-%d").to_s
|
|
32
38
|
when :monthly
|
|
33
|
-
|
|
39
|
+
@file_date.strftime("%Y-%m").to_s
|
|
34
40
|
else
|
|
35
|
-
|
|
41
|
+
@file_date.strftime("%Y-%m-%d").to_s
|
|
36
42
|
end
|
|
37
43
|
end
|
|
38
44
|
|
|
45
|
+
# Check if the file should be rolled.
|
|
46
|
+
#
|
|
47
|
+
# @return [Boolean]
|
|
39
48
|
def roll_file?
|
|
40
49
|
if @manual
|
|
41
50
|
true
|
|
@@ -54,9 +63,9 @@ module Lumberjack
|
|
|
54
63
|
end
|
|
55
64
|
end
|
|
56
65
|
end
|
|
57
|
-
|
|
66
|
+
|
|
58
67
|
protected
|
|
59
|
-
|
|
68
|
+
|
|
60
69
|
def after_roll
|
|
61
70
|
@file_date = Date.today
|
|
62
71
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require "fileutils"
|
|
4
4
|
|
|
5
5
|
module Lumberjack
|
|
6
6
|
class Device
|
|
@@ -10,23 +10,30 @@ module Lumberjack
|
|
|
10
10
|
|
|
11
11
|
# The absolute path of the file being logged to.
|
|
12
12
|
attr_reader :path
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
# Create a logger to the file at +path+. Options are passed through to the Writer constructor.
|
|
15
|
+
#
|
|
16
|
+
# @param [String, Pathname] path The path to the log file.
|
|
17
|
+
# @param [Hash] options The options for the device.
|
|
15
18
|
def initialize(path, options = {})
|
|
16
19
|
@path = File.expand_path(path)
|
|
17
20
|
FileUtils.mkdir_p(File.dirname(@path))
|
|
18
21
|
super(file_stream, options)
|
|
19
22
|
end
|
|
20
|
-
|
|
23
|
+
|
|
24
|
+
# Reopen the log file.
|
|
25
|
+
#
|
|
26
|
+
# @param [Object] logdev not used
|
|
27
|
+
# @return [void]
|
|
21
28
|
def reopen(logdev = nil)
|
|
22
29
|
close
|
|
23
30
|
@stream = file_stream
|
|
24
31
|
end
|
|
25
|
-
|
|
32
|
+
|
|
26
33
|
private
|
|
27
|
-
|
|
34
|
+
|
|
28
35
|
def file_stream
|
|
29
|
-
File.new(@path,
|
|
36
|
+
File.new(@path, "a", encoding: EXTERNAL_ENCODING)
|
|
30
37
|
end
|
|
31
38
|
end
|
|
32
39
|
end
|
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Lumberjack
|
|
4
4
|
class Device
|
|
5
5
|
# This is a logging device that forward log entries to multiple other devices.
|
|
6
6
|
class Multi < Device
|
|
7
|
+
# @param [Array<Lumberjack::Device>] devices The devices to write to.
|
|
7
8
|
def initialize(*devices)
|
|
8
9
|
@devices = devices.flatten
|
|
9
10
|
end
|
|
10
|
-
|
|
11
|
+
|
|
11
12
|
def write(entry)
|
|
12
13
|
@devices.each do |device|
|
|
13
14
|
device.write(entry)
|
|
14
15
|
end
|
|
15
16
|
end
|
|
16
|
-
|
|
17
|
+
|
|
17
18
|
def flush
|
|
18
19
|
@devices.each do |device|
|
|
19
20
|
device.flush
|
|
20
21
|
end
|
|
21
22
|
end
|
|
22
|
-
|
|
23
|
+
|
|
23
24
|
def close
|
|
24
25
|
@devices.each do |device|
|
|
25
26
|
device.close
|
|
26
27
|
end
|
|
27
28
|
end
|
|
28
|
-
|
|
29
|
+
|
|
29
30
|
def reopen(logdev = nil)
|
|
30
31
|
@devices.each do |device|
|
|
31
32
|
device.reopen(logdev = nil)
|
|
32
33
|
end
|
|
33
34
|
end
|
|
34
|
-
|
|
35
|
+
|
|
35
36
|
def datetime_format
|
|
36
37
|
@devices.detect(&:datetime_format).datetime_format
|
|
37
38
|
end
|
|
38
|
-
|
|
39
|
+
|
|
39
40
|
def datetime_format=(format)
|
|
40
41
|
@devices.each do |device|
|
|
41
42
|
device.datetime_format = format
|