lumberjack 1.0.13 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +74 -0
- data/README.md +92 -20
- data/VERSION +1 -1
- data/lib/lumberjack.rb +51 -16
- data/lib/lumberjack/context.rb +35 -0
- data/lib/lumberjack/device.rb +20 -6
- data/lib/lumberjack/device/date_rolling_log_file.rb +2 -2
- data/lib/lumberjack/device/log_file.rb +12 -1
- data/lib/lumberjack/device/multi.rb +46 -0
- data/lib/lumberjack/device/null.rb +0 -2
- data/lib/lumberjack/device/rolling_log_file.rb +2 -2
- data/lib/lumberjack/device/size_rolling_log_file.rb +1 -1
- data/lib/lumberjack/device/writer.rb +86 -55
- data/lib/lumberjack/formatter.rb +54 -18
- data/lib/lumberjack/formatter/date_time_formatter.rb +26 -0
- data/lib/lumberjack/formatter/id_formatter.rb +23 -0
- data/lib/lumberjack/formatter/object_formatter.rb +12 -0
- data/lib/lumberjack/formatter/structured_formatter.rb +31 -0
- data/lib/lumberjack/log_entry.rb +41 -16
- data/lib/lumberjack/logger.rb +168 -63
- data/lib/lumberjack/rack.rb +3 -2
- data/lib/lumberjack/rack/context.rb +18 -0
- data/lib/lumberjack/rack/request_id.rb +4 -4
- data/lib/lumberjack/severity.rb +11 -9
- data/lib/lumberjack/tags.rb +24 -0
- data/lib/lumberjack/template.rb +74 -32
- data/lumberjack.gemspec +35 -0
- metadata +48 -37
- data/Rakefile +0 -40
- data/spec/device/date_rolling_log_file_spec.rb +0 -73
- data/spec/device/log_file_spec.rb +0 -48
- data/spec/device/null_spec.rb +0 -12
- data/spec/device/rolling_log_file_spec.rb +0 -151
- data/spec/device/size_rolling_log_file_spec.rb +0 -58
- data/spec/device/writer_spec.rb +0 -118
- data/spec/formatter/exception_formatter_spec.rb +0 -20
- data/spec/formatter/inspect_formatter_spec.rb +0 -13
- data/spec/formatter/pretty_print_formatter_spec.rb +0 -14
- data/spec/formatter/string_formatter_spec.rb +0 -12
- data/spec/formatter_spec.rb +0 -45
- data/spec/log_entry_spec.rb +0 -69
- data/spec/logger_spec.rb +0 -411
- data/spec/lumberjack_spec.rb +0 -29
- data/spec/rack/request_id_spec.rb +0 -48
- data/spec/rack/unit_of_work_spec.rb +0 -26
- data/spec/severity_spec.rb +0 -23
- data/spec/spec_helper.rb +0 -32
- data/spec/template_spec.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 937acfbe9675acf0eecd3fa034e0818952967b00471e365d2ea3f0ab7fafa4d1
|
4
|
+
data.tar.gz: 0b82a010a7c4265c92cec3e7d5b0ffcb5f4ac5e151c7e1e8ee28ac8d0b3adf9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42d94b2e4c35ded1291262acf02e3d886c9b5acc2d26bd40d5d6884ebced0658d8b7baeb83aa402f700a294cbdba05491aaabff4d9f7df9bb03c5edd3954f2a4
|
7
|
+
data.tar.gz: 2ddcf64ed40c77e57cc8152aba8241c6f92c2668d3a6f6aa58c674e46e35cb0e1a78491d24588d67fbdc13d1ce9904c5a200426948367fe8d9ef4b50bd00b23e
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
## 1.1.0
|
2
|
+
|
3
|
+
* Change Lumberjack::Logger to inherit from ::Logger
|
4
|
+
* Add support for tags on log messages
|
5
|
+
* Add global tag context for all loggers
|
6
|
+
* Add per logger tags and tag contexts
|
7
|
+
* Reimplement unit of work id as a tag on log entries
|
8
|
+
* Add support for setting datetime format on log devices
|
9
|
+
* Performance optimizations
|
10
|
+
* Add Multi device to output to multiple devices
|
11
|
+
* Add DateTimeFormatter, IdFormatter, ObjectFormatter, and StructuredFormatter
|
12
|
+
* Add rack Context middleware for setting thread global context
|
13
|
+
* End support for ruby versions < 2.3
|
14
|
+
|
15
|
+
## 1.0.13
|
16
|
+
|
17
|
+
* Reduce amount of code executed inside a mutex lock when writing to the logger stream.
|
18
|
+
* Added `:min_roll_check` option to `Lumberjack::Device::RollingLogFile` to reduce file system checks. Default is now to only check if a file needs to be rolled at most once per second.
|
19
|
+
* Force immutable strings for Ruby versions that support them.
|
20
|
+
|
21
|
+
## 1.0.12
|
22
|
+
|
23
|
+
* Add support for ActionDispatch request id for better Rails compatibility.
|
24
|
+
|
25
|
+
## 1.0.11
|
26
|
+
|
27
|
+
* Fix Ruby 2.4 deprecation warning on Fixnum (thanks koic).
|
28
|
+
* Fix gemspec files to be flat array (thanks e2).
|
29
|
+
|
30
|
+
## 1.0.10
|
31
|
+
|
32
|
+
* Expose option to manually roll log files.
|
33
|
+
* Minor code cleanup.
|
34
|
+
|
35
|
+
## 1.0.9
|
36
|
+
|
37
|
+
* Add method so Formatter is compatible with ActiveSupport logging extensions.
|
38
|
+
|
39
|
+
## 1.0.8
|
40
|
+
|
41
|
+
* Fix another internal variable name conflict with ActiveSupport logging extensions.
|
42
|
+
|
43
|
+
## 1.0.7
|
44
|
+
|
45
|
+
* Fix broken formatter attribute method.
|
46
|
+
|
47
|
+
## 1.0.6
|
48
|
+
|
49
|
+
* Fix internal variable name conflict with ActiveSupport logging extensions.
|
50
|
+
|
51
|
+
## 1.0.5
|
52
|
+
|
53
|
+
* Update docs.
|
54
|
+
* Remove autoload calls to make thread safe.
|
55
|
+
* Make compatible with Ruby 2.1.1 Pathname.
|
56
|
+
* Make compatible with standard library Logger's use of progname as default message.
|
57
|
+
|
58
|
+
## 1.0.4
|
59
|
+
|
60
|
+
* Add ability to supply a unit of work id for a block instead of having one generated every time.
|
61
|
+
|
62
|
+
## 1.0.3
|
63
|
+
|
64
|
+
* Change log file output format to binary to avoid encoding warnings.
|
65
|
+
* Fixed bug in log file rolling that left the file locked.
|
66
|
+
|
67
|
+
## 1.0.2
|
68
|
+
|
69
|
+
* Remove deprecation warnings under ruby 1.9.3.
|
70
|
+
* Add more error checking around file rolling.
|
71
|
+
|
72
|
+
## 1.0.1
|
73
|
+
|
74
|
+
* Writes are no longer buffered by default.
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Lumberjack
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/bdurand/lumberjack.svg?branch=master)](https://travis-ci.org/bdurand/lumberjack)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/a0abc03721fff9b0cde1/maintainability)](https://codeclimate.com/github/bdurand/lumberjack/maintainability)
|
5
|
+
|
3
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.
|
4
7
|
|
5
8
|
## Usage
|
@@ -33,21 +36,55 @@ The following information is recorded for each message:
|
|
33
36
|
* time - The time at which the message was recorded.
|
34
37
|
* program name - The name of the program logging the message. This can be either set for all messages or customized with each message.
|
35
38
|
* process id - The process id (pid) of the process that logged the message.
|
36
|
-
*
|
39
|
+
* tags - An map of name value pairs for addition information about the log context.
|
40
|
+
|
41
|
+
### Tags
|
37
42
|
|
38
|
-
|
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 you log messages clean. You can avoid string interoplation to add additional meta data.
|
39
44
|
|
40
|
-
|
45
|
+
Each of the logger methods includes an additional argument that can be used to specify tags on a messsage:
|
41
46
|
|
42
47
|
```ruby
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
logger.info("request completed", duration: elapsed_time, status: response.status)
|
49
|
+
```
|
50
|
+
|
51
|
+
You can also specify tags on a logger that will be included with every log message.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
logger.tag(host: Socket.gethostname)
|
55
|
+
```
|
56
|
+
|
57
|
+
You can specify tags that will only be applied to the logger in a block as well.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
logger.tag(thread_id: Thread.current.object_id) do
|
61
|
+
logger.info("here") # Will include the `thread_id` tag
|
62
|
+
end
|
63
|
+
logger.info("there") # Will not include the `thread_id` tag
|
64
|
+
```
|
65
|
+
|
66
|
+
Finally, you can specify a logging context with tags that apply within a block to all loggers.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Lumberjack.context do
|
70
|
+
Lumberjack.tag(request_id: SecureRandom.hex)
|
71
|
+
logger.info("begin request") # Will include the `request_id` tag
|
72
|
+
end
|
73
|
+
logger.info("no requests") # Will not include the `request_id` tag
|
49
74
|
```
|
50
75
|
|
76
|
+
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.
|
77
|
+
|
78
|
+
#### Templates
|
79
|
+
|
80
|
+
The built in `Lumberjack::Device::Writer` class has built in support for including tags in the output using the `Lumberjack::Template` class.
|
81
|
+
|
82
|
+
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.
|
83
|
+
|
84
|
+
#### Unit Of Work
|
85
|
+
|
86
|
+
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.
|
87
|
+
|
51
88
|
### Pluggable Devices
|
52
89
|
|
53
90
|
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.
|
@@ -56,27 +93,50 @@ When a Logger logs a LogEntry, it sends it to a Lumberjack::Device. Lumberjack c
|
|
56
93
|
* Lumberjack::Device::LogFile - Writes log entries to a file.
|
57
94
|
* Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
|
58
95
|
* Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
|
96
|
+
* Lumberjack::Device::Multi - This device wraps mulitiple other devices and will write log entries to each of them.
|
59
97
|
* Lumberjack::Device::Null - This device produces no output and is intended for testing environments.
|
60
98
|
|
61
99
|
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:
|
62
100
|
|
63
101
|
* [lumberjack_syslog_device](https://github.com/bdurand/lumberjack_syslog_device) - send your log messages to the system wide syslog service
|
64
|
-
* [lumberjack_multi-device](https://github.com/astevens/lumberjack_multi-device) - send log messages to multiple devices
|
65
102
|
* [lumberjack_mongo_device](https://github.com/bdurand/lumberjack_mongo_device) - store your log messages to a [MongoDB](http://www.mongodb.org/) NoSQL data store
|
66
103
|
* [lumberjack-couchdb-driver](https://github.com/narkisr/lumberjack-couchdb-driver) - store your log messages to a [CouchDB](http://couchdb.apache.org/) NoSQL data store
|
67
104
|
* [lumberjack_heroku_device](https://github.com/tonycoco/lumberjack_heroku_device) - log to Heroku's logging system
|
68
105
|
|
69
106
|
### Customize Formatting
|
70
107
|
|
108
|
+
#### Formatters
|
109
|
+
|
71
110
|
When a message is logged, it is first converted into a string. You can customize how it is converted by adding mappings to a Formatter.
|
72
111
|
|
112
|
+
There are several built in classes you can add as formatters
|
113
|
+
|
73
114
|
```ruby
|
74
115
|
logger.formatter.add(Hash, :pretty_print) # use the Formatter::PrettyPrintFormatter for all Hashes
|
116
|
+
logger.formatter.add(Hash, Lumberjack::Formatter::PrettyPrintFormatter.new) # alternative using a formatter instance
|
117
|
+
```
|
118
|
+
|
119
|
+
* `Lumberjack::Formatter::ObjectFormatter` - no op conversion that returns the object itself.
|
120
|
+
* `Lumberjack::Formatter::StringFormatter` - calls `to_s` on the object.
|
121
|
+
* `Lumberjack::Formatter::InspectFormatter` - calls `inspect` on the object.
|
122
|
+
* `Lumberjack::Formatter::ExceptionFormatter` - special formatter for exceptions which logs them as multi line statements with the message and backtrace.
|
123
|
+
* `Lumberjack::Formatter::DateTimeFormatter` - special formatter for dates and times to format them using `strftime`.
|
124
|
+
* `Lumberjack::Formatter::PrettyPrintFormatter` - returns the pretty print format of the object.
|
125
|
+
* `Lumberjack::Formatter::IdFormatter` - returns a hash of the object with keys for the id attribute and class.
|
126
|
+
* `Lumberjack::Formatter::StructuredFormatter` - crawls the object and applies the formatter recursively to Enumerable objects found in it (arrays, hashes, etc.).
|
127
|
+
|
128
|
+
You can also specify a block to use as a formatter:
|
129
|
+
|
130
|
+
```ruby
|
75
131
|
logger.formatter.add(MyClass){|obj| "#{obj.class}@#{obj.id}"} # use a block to provide a custom format
|
76
132
|
```
|
77
133
|
|
134
|
+
#### Templates
|
135
|
+
|
78
136
|
If you use the built in devices, you can also customize the Template used to format the LogEntry.
|
79
|
-
|
137
|
+
|
138
|
+
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.
|
139
|
+
|
80
140
|
```ruby
|
81
141
|
# Change the format of the time in the log
|
82
142
|
Lumberjack::Logger.new("application.log", :time_format => "%m/%d/%Y %H:%M:%S")
|
@@ -84,23 +144,27 @@ If you use the built in devices, you can also customize the Template used to for
|
|
84
144
|
# Use a simple template that only includes the time and the message
|
85
145
|
Lumberjack::Logger.new("application.log", :template => ":time - :message")
|
86
146
|
|
147
|
+
# Use a simple template that includes tags, but handles the `duration` tag separately.
|
148
|
+
# All tags will appear at the end of the message except for `duration` which will be at the beginning.
|
149
|
+
Lumberjack::Logger.new("application.log", :template => ":time (:duration) - :message - :tags")
|
150
|
+
|
87
151
|
# Use a custom template as a block that only includes the first character of the severity
|
88
152
|
template = lambda{|e| "#{e.severity_label[0, 1]} #{e.time} - #{e.message}"}
|
89
153
|
Lumberjack::Logger.new("application.log", :template => template)
|
90
154
|
```
|
91
155
|
|
92
|
-
### Buffered
|
156
|
+
### Buffered Logging
|
93
157
|
|
94
|
-
The logger has hooks for devices that support buffering to increase performance by batching physical writes. Log entries are not guaranteed to be written until the Lumberjack::Logger#flush method is called.
|
158
|
+
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.
|
95
159
|
|
96
|
-
You can use the
|
160
|
+
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.
|
97
161
|
|
98
|
-
The built in stream based logging devices use an internal buffer. The size of the buffer (in bytes) can be set with the
|
162
|
+
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 to buffer.
|
99
163
|
|
100
164
|
```ruby
|
101
165
|
# Set buffer to flush after 8K has been written to the log.
|
102
166
|
logger = Lumberjack::Logger.new("application.log", :buffer_size => 8192)
|
103
|
-
|
167
|
+
|
104
168
|
# Turn off buffering so entries are immediately written to disk.
|
105
169
|
logger = Lumberjack::Logger.new("application.log", :buffer_size => 0)
|
106
170
|
```
|
@@ -111,6 +175,14 @@ The built in devices include two that can automatically roll log files based eit
|
|
111
175
|
|
112
176
|
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.
|
113
177
|
|
178
|
+
## Difference Standard Library Logger
|
179
|
+
|
180
|
+
`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.
|
181
|
+
|
182
|
+
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.
|
183
|
+
|
184
|
+
The logging methods (`debug`, 'info', 'warn', 'error', 'fatal') are overloaded with an additional argument for setting tags on the log entry.
|
185
|
+
|
114
186
|
## Examples
|
115
187
|
|
116
188
|
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.
|
@@ -139,17 +211,17 @@ To set up a logger to roll log files when they get to 100Mb, you could use this:
|
|
139
211
|
```ruby
|
140
212
|
config.logger = Lumberjack::Logger.new(log_file_path, :max_size => 100.megabytes)
|
141
213
|
```
|
142
|
-
|
214
|
+
|
143
215
|
To change the log message format, you could use this code:
|
144
216
|
|
145
217
|
```ruby
|
146
218
|
config.logger = Lumberjack::Logger.new(log_file_path, :template => ":time - :message")
|
147
219
|
```
|
148
220
|
|
149
|
-
To change the log message format to output JSON, you could use this code
|
150
|
-
|
221
|
+
To change the log message format to output JSON, you could use this code:
|
222
|
+
|
151
223
|
```ruby
|
152
|
-
config.logger = Lumberjack::Logger.new(log_file_path, :template => lambda{|e|
|
224
|
+
config.logger = Lumberjack::Logger.new(log_file_path, :template => lambda{|e| JSON.dump(time: e.time, level: e.severity_label, message: e.message)})
|
153
225
|
```
|
154
226
|
|
155
227
|
To send log messages to syslog instead of to a file, you could use this (require the lumberjack_syslog_device gem):
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/lumberjack.rb
CHANGED
@@ -4,18 +4,21 @@ require 'rbconfig'
|
|
4
4
|
require 'time'
|
5
5
|
require 'thread'
|
6
6
|
require 'securerandom'
|
7
|
+
require 'logger'
|
7
8
|
|
8
9
|
module Lumberjack
|
9
10
|
LINE_SEPARATOR = (RbConfig::CONFIG['host_os'].match(/mswin/i) ? "\r\n" : "\n")
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
require_relative "lumberjack/severity.rb"
|
13
|
+
require_relative "lumberjack/context.rb"
|
14
|
+
require_relative "lumberjack/log_entry.rb"
|
15
|
+
require_relative "lumberjack/formatter.rb"
|
16
|
+
require_relative "lumberjack/device.rb"
|
17
|
+
require_relative "lumberjack/logger.rb"
|
18
|
+
require_relative "lumberjack/tags.rb"
|
19
|
+
require_relative "lumberjack/template.rb"
|
20
|
+
require_relative "lumberjack/rack.rb"
|
21
|
+
|
19
22
|
class << self
|
20
23
|
# Define a unit of work within a block. Within the block supplied to this
|
21
24
|
# method, calling +unit_of_work_id+ will return the same value that can
|
@@ -27,19 +30,51 @@ module Lumberjack
|
|
27
30
|
# For the common use case of treating a single web request as a unit of work, see the
|
28
31
|
# Lumberjack::Rack::UnitOfWork class.
|
29
32
|
def unit_of_work(id = nil)
|
30
|
-
save_val = Thread.current[:lumberjack_logger_unit_of_work_id]
|
31
33
|
id ||= SecureRandom.hex(6)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
ensure
|
36
|
-
Thread.current[:lumberjack_logger_unit_of_work_id] = save_val
|
34
|
+
context do
|
35
|
+
context[:unit_of_work_id] = id
|
36
|
+
yield
|
37
37
|
end
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
# Get the UniqueIdentifier for the current unit of work.
|
41
41
|
def unit_of_work_id
|
42
|
-
|
42
|
+
context[:unit_of_work_id]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Contexts can be used to store tags that will be attached to all log entries in the block.
|
46
|
+
#
|
47
|
+
# If this method is called with a block, it will set a logging context for the scope of a block.
|
48
|
+
# If there is already a context in scope, a new one will be created that inherits
|
49
|
+
# all the tags of the parent context.
|
50
|
+
#
|
51
|
+
# Otherwise, it will return the current context. If one doesn't exist, it will return a new one
|
52
|
+
# but that context will not be in any scope.
|
53
|
+
def context
|
54
|
+
current_context = Thread.current[:lumberjack_context]
|
55
|
+
if block_given?
|
56
|
+
Thread.current[:lumberjack_context] = Context.new(current_context)
|
57
|
+
begin
|
58
|
+
yield
|
59
|
+
ensure
|
60
|
+
Thread.current[:lumberjack_context] = current_context
|
61
|
+
end
|
62
|
+
else
|
63
|
+
current_context || Context.new
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Return the tags from the current context or nil if there are no tags.
|
68
|
+
def context_tags
|
69
|
+
context = Thread.current[:lumberjack_context]
|
70
|
+
context.tags if context
|
43
71
|
end
|
72
|
+
|
73
|
+
# Set tags on the current context
|
74
|
+
def tag(tags)
|
75
|
+
context = Thread.current[:lumberjack_context]
|
76
|
+
context.tag(tags) if context
|
77
|
+
end
|
78
|
+
|
44
79
|
end
|
45
80
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lumberjack
|
4
|
+
# A context is used to store tags that are then added to all log entries within a block.
|
5
|
+
class Context
|
6
|
+
attr_reader :tags
|
7
|
+
|
8
|
+
def initialize(parent_context = nil)
|
9
|
+
@tags = {}
|
10
|
+
@tags.merge!(parent_context.tags) if parent_context
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set tags on the context.
|
14
|
+
def tag(tags)
|
15
|
+
tags.each do |key, value|
|
16
|
+
@tags[key.to_s] = value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get a context tag.
|
21
|
+
def [](key)
|
22
|
+
@tags[key.to_s]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set a context tag.
|
26
|
+
def []=(key, value)
|
27
|
+
@tags[key.to_s] = value
|
28
|
+
end
|
29
|
+
|
30
|
+
# Clear all the context data.
|
31
|
+
def reset
|
32
|
+
@tags.clear
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/lumberjack/device.rb
CHANGED
@@ -4,12 +4,13 @@ module Lumberjack
|
|
4
4
|
# This is an abstract class for logging devices. Subclasses must implement the +write+ method and
|
5
5
|
# may implement the +close+ and +flush+ methods if applicable.
|
6
6
|
class Device
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
require_relative "device/writer.rb"
|
8
|
+
require_relative "device/log_file.rb"
|
9
|
+
require_relative "device/rolling_log_file.rb"
|
10
|
+
require_relative "device/date_rolling_log_file.rb"
|
11
|
+
require_relative "device/size_rolling_log_file.rb"
|
12
|
+
require_relative "device/multi.rb"
|
13
|
+
require_relative "device/null.rb"
|
13
14
|
|
14
15
|
# Subclasses must implement this method to write a LogEntry.
|
15
16
|
def write(entry)
|
@@ -21,8 +22,21 @@ module Lumberjack
|
|
21
22
|
flush
|
22
23
|
end
|
23
24
|
|
25
|
+
# Subclasses may implement this method to reopen the device.
|
26
|
+
def reopen(logdev = nil)
|
27
|
+
flush
|
28
|
+
end
|
29
|
+
|
24
30
|
# Subclasses may implement this method to flush any buffers used by the device.
|
25
31
|
def flush
|
26
32
|
end
|
33
|
+
|
34
|
+
# Subclasses may implement this method to get the format for log timestamps.
|
35
|
+
def datetime_format
|
36
|
+
end
|
37
|
+
|
38
|
+
# Subclasses may implement this method to set a format for log timestamps.
|
39
|
+
def datetime_format=(format)
|
40
|
+
end
|
27
41
|
end
|
28
42
|
end
|