lumberjack 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{README.rdoc → README.md} +42 -15
- data/VERSION +1 -1
- data/lib/lumberjack/rack.rb +1 -0
- data/lib/lumberjack/rack/request_id.rb +25 -0
- data/spec/rack/request_id_spec.rb +48 -0
- metadata +7 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9c87ea941460683699c83e03f4b9b0fda5bcf92
|
4
|
+
data.tar.gz: c4a9af57b579144b798d47ef6ff19a632050dc25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aab631be08c9ef9a55c755c42268ba4cc68e0f0aa27d625d43f534c649e9c9c6ce5d270a336984484993c6c9211f7e32f35438d9f01201c6024d142cbbf838af
|
7
|
+
data.tar.gz: 53db84229562c9d90931e9feda302887e13d930d746bb92d2d18a7e4d7029ad7e21205fa82ae35e2e2f8a0247557cb0e06a87f58ea3d7c573e665c13395a2eae
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,11 +1,12 @@
|
|
1
|
-
|
1
|
+
# Lumberjack
|
2
2
|
|
3
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.
|
4
4
|
|
5
|
-
|
5
|
+
## Usage
|
6
6
|
|
7
7
|
This code aims to be extremely simple to use. The core interface it 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.
|
8
8
|
|
9
|
+
```ruby
|
9
10
|
logger = Lumberjack::Logger.new("logs/application.log") # Open a new log file with INFO level
|
10
11
|
logger.info("Begin request")
|
11
12
|
logger.debug(request.params) # Message not written unless the level is set to DEBUG
|
@@ -16,12 +17,13 @@ This code aims to be extremely simple to use. The core interface it the Lumberja
|
|
16
17
|
raise
|
17
18
|
end
|
18
19
|
logger.info("End request")
|
20
|
+
```
|
19
21
|
|
20
22
|
This is all you need to know to log messages.
|
21
23
|
|
22
|
-
|
24
|
+
## Features
|
23
25
|
|
24
|
-
|
26
|
+
### Meta data
|
25
27
|
|
26
28
|
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.
|
27
29
|
|
@@ -33,18 +35,20 @@ The following information is recorded for each message:
|
|
33
35
|
* process id - The process id (pid) of the process that logged the message.
|
34
36
|
* unit of work id - The unique 12 byte hexadecimal number generated for a unit of work.
|
35
37
|
|
36
|
-
|
38
|
+
### Units Of Work
|
37
39
|
|
38
40
|
A unit of work can be used to tie together all log messages within a block. This can be very useful to isolate a group of messages that represent one path through the system. For instance, in a web application, a single request represents a natural unit of work, and when you are looking through a log file, it is useful to see the entire set of log entries as a unit instead of interspersed with messages from other concurrent requests.
|
39
41
|
|
42
|
+
```ruby
|
40
43
|
# All log entries in this block will get a common unit of work id.
|
41
44
|
Lumberjack.unit_of_work do
|
42
45
|
logger.info("Begin request")
|
43
46
|
yield
|
44
47
|
logger.info("End request")
|
45
48
|
end
|
49
|
+
```
|
46
50
|
|
47
|
-
|
51
|
+
### Pluggable Devices
|
48
52
|
|
49
53
|
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.
|
50
54
|
|
@@ -56,19 +60,24 @@ When a Logger logs a LogEntry, it sends it to a Lumberjack::Device. Lumberjack c
|
|
56
60
|
|
57
61
|
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:
|
58
62
|
|
59
|
-
* lumberjack_syslog_device
|
60
|
-
*
|
61
|
-
*
|
63
|
+
* [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
|
+
* [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
|
+
* [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
|
+
* [lumberjack_heroku_device](https://github.com/tonycoco/lumberjack_heroku_device) - log to Heroku's logging system
|
62
68
|
|
63
|
-
|
69
|
+
### Customize Formatting
|
64
70
|
|
65
71
|
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.
|
66
72
|
|
73
|
+
```ruby
|
67
74
|
logger.formatter.add(Hash, :pretty_print) # use the Formatter::PrettyPrintFormatter for all Hashes
|
68
75
|
logger.formatter.add(MyClass){|obj| "#{obj.class}@#{obj.id}"} # use a block to provide a custom format
|
76
|
+
```
|
69
77
|
|
70
78
|
If you use the built in devices, you can also customize the Template used to format the LogEntry.
|
71
79
|
|
80
|
+
```ruby
|
72
81
|
# Change the format of the time in the log
|
73
82
|
Lumberjack::Logger.new("application.log", :time_format => "%m/%d/%Y %H:%M:%S")
|
74
83
|
|
@@ -78,8 +87,9 @@ If you use the built in devices, you can also customize the Template used to for
|
|
78
87
|
# Use a custom template as a block that only includes the first character of the severity
|
79
88
|
template = lambda{|e| "#{e.severity_label[0, 1]} #{e.time} - #{e.message}"}
|
80
89
|
Lumberjack::Logger.new("application.log", :template => template)
|
90
|
+
```
|
81
91
|
|
82
|
-
|
92
|
+
### Buffered Performance
|
83
93
|
|
84
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.
|
85
95
|
|
@@ -87,46 +97,63 @@ You can use the <tt>:flush_seconds</tt> option on the logger to periodically flu
|
|
87
97
|
|
88
98
|
The built in stream based logging devices use an internal buffer. The size of the buffer (in bytes) can be set with the <tt>:buffer_size</tt> options when initializing a logger. The default behavior is to not to buffer.
|
89
99
|
|
100
|
+
```ruby
|
90
101
|
# Set buffer to flush after 8K has been written to the log.
|
91
102
|
logger = Lumberjack::Logger.new("application.log", :buffer_size => 8192)
|
92
103
|
|
93
104
|
# Turn off buffering so entries are immediately written to disk.
|
94
105
|
logger = Lumberjack::Logger.new("application.log", :buffer_size => 0)
|
106
|
+
```
|
95
107
|
|
96
|
-
|
108
|
+
### Automatic Log Rolling
|
97
109
|
|
98
110
|
The built in devices include two that can automatically roll log files based either on date or on file size. When a log file is rolled, it will be renamed with a suffix and a new file will be created to receive new log entries. This can keep your log files from growing to unusable sizes and removes the need to schedule an external process to roll the files.
|
99
111
|
|
100
112
|
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.
|
101
113
|
|
102
|
-
|
114
|
+
## Examples
|
103
115
|
|
104
116
|
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.
|
105
117
|
|
106
118
|
In a Rails application you can replace the default production logger by adding this to your config/environments/production.rb file:
|
107
119
|
|
108
|
-
|
109
|
-
|
120
|
+
```ruby
|
121
|
+
# Use the ActionDispatch request id as the unit of work id. This will use just the first chunk of the request id.
|
122
|
+
# If you want to use an abbreviated request id for terseness, change the last argument to `true`
|
123
|
+
config.middleware.insert_after ActionDispatch::RequestId, Lumberjack::Rack::RequestId, false
|
124
|
+
# Use a custom unit of work id to each request
|
125
|
+
# config.middleware.insert(0, Lumberjack::Rack::UnitOfWork)
|
110
126
|
# Change the logger to use Lumberjack
|
111
127
|
log_file_path = Rails.root + "log" + "#{Rails.env}.log"
|
112
128
|
config.logger = Lumberjack::Logger.new(log_file, :level => :warn)
|
129
|
+
```
|
113
130
|
|
114
131
|
To set up a logger to roll every day at midnight, you could use this code (you can also specify :weekly or :monthly):
|
115
132
|
|
133
|
+
```ruby
|
116
134
|
config.logger = Lumberjack::Logger.new(log_file_path, :roll => :daily)
|
135
|
+
```
|
117
136
|
|
118
137
|
To set up a logger to roll log files when they get to 100Mb, you could use this:
|
119
138
|
|
139
|
+
```ruby
|
120
140
|
config.logger = Lumberjack::Logger.new(log_file_path, :max_size => 100.megabytes)
|
141
|
+
```
|
121
142
|
|
122
143
|
To change the log message format, you could use this code:
|
123
144
|
|
145
|
+
```ruby
|
124
146
|
config.logger = Lumberjack::Logger.new(log_file_path, :template => ":time - :message")
|
147
|
+
```
|
125
148
|
|
126
149
|
To change the log message format to output JSON, you could use this code (this example requires the multi-json gem):
|
127
150
|
|
151
|
+
```ruby
|
128
152
|
config.logger = Lumberjack::Logger.new(log_file_path, :template => lambda{|e| MultiJson.dump(e)})
|
153
|
+
```
|
129
154
|
|
130
155
|
To send log messages to syslog instead of to a file, you could use this (require the lumberjack_syslog_device gem):
|
131
156
|
|
157
|
+
```ruby
|
132
158
|
config.logger = Lumberjack::Logger.new(Lumberjack::SyslogDevice.new)
|
159
|
+
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.12
|
data/lib/lumberjack/rack.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Lumberjack
|
2
|
+
module Rack
|
3
|
+
# Support for using the Rails ActionDispatch request id in the log.
|
4
|
+
# The format is expected to be a random UUID and only the first chunk is used for terseness
|
5
|
+
# if the abbreviated argument is true.
|
6
|
+
class RequestId
|
7
|
+
REQUEST_ID = "action_dispatch.request_id".freeze
|
8
|
+
|
9
|
+
def initialize(app, abbreviated = false)
|
10
|
+
@app = app
|
11
|
+
@abbreviated = abbreviated
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
request_id = env[REQUEST_ID]
|
16
|
+
if request_id && @abbreviated
|
17
|
+
request_id = request_id.split('-'.freeze, 2).first
|
18
|
+
end
|
19
|
+
Lumberjack.unit_of_work(request_id) do
|
20
|
+
@app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lumberjack::Rack::RequestId do
|
4
|
+
|
5
|
+
it "should use the action dispatch request id if it exists" do
|
6
|
+
app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]}
|
7
|
+
handler = Lumberjack::Rack::RequestId.new(app)
|
8
|
+
|
9
|
+
response = handler.call("Content-Type" => "text/plain", "action_dispatch.request_id" => "0123-4567-89ab-cdef")
|
10
|
+
response[0].should == 200
|
11
|
+
response[1]["Content-Type"].should == "text/plain"
|
12
|
+
response[1]["Unit-Of-Work"].should == "0123-4567-89ab-cdef"
|
13
|
+
response[2].should == ["OK"]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should use an abbreviated action dispatch request id if abbreviated is true" do
|
17
|
+
app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]}
|
18
|
+
handler = Lumberjack::Rack::RequestId.new(app, true)
|
19
|
+
|
20
|
+
response = handler.call("Content-Type" => "text/plain", "action_dispatch.request_id" => "0123-4567-89ab-cdef")
|
21
|
+
response[0].should == 200
|
22
|
+
response[1]["Content-Type"].should == "text/plain"
|
23
|
+
response[1]["Unit-Of-Work"].should == "0123"
|
24
|
+
response[2].should == ["OK"]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should create a unit of work in a middleware stack if the request id doesn't exist" do
|
28
|
+
app = lambda{|env| [200, {"Content-Type" => env["Content-Type"], "Unit-Of-Work" => Lumberjack.unit_of_work_id.to_s}, ["OK"]]}
|
29
|
+
handler = Lumberjack::Rack::RequestId.new(app)
|
30
|
+
|
31
|
+
response = handler.call("Content-Type" => "text/plain")
|
32
|
+
response[0].should == 200
|
33
|
+
response[1]["Content-Type"].should == "text/plain"
|
34
|
+
unit_of_work_1 = response[1]["Unit-Of-Work"]
|
35
|
+
response[2].should == ["OK"]
|
36
|
+
|
37
|
+
response = handler.call("Content-Type" => "text/html")
|
38
|
+
response[0].should == 200
|
39
|
+
response[1]["Content-Type"].should == "text/html"
|
40
|
+
unit_of_work_2 = response[1]["Unit-Of-Work"]
|
41
|
+
response[2].should == ["OK"]
|
42
|
+
|
43
|
+
unit_of_work_1.should_not == nil
|
44
|
+
unit_of_work_2.should_not == nil
|
45
|
+
unit_of_work_1.should_not == unit_of_work_2
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lumberjack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -46,11 +46,10 @@ email:
|
|
46
46
|
- bbdurand@gmail.com
|
47
47
|
executables: []
|
48
48
|
extensions: []
|
49
|
-
extra_rdoc_files:
|
50
|
-
- README.rdoc
|
49
|
+
extra_rdoc_files: []
|
51
50
|
files:
|
52
51
|
- MIT_LICENSE
|
53
|
-
- README.
|
52
|
+
- README.md
|
54
53
|
- Rakefile
|
55
54
|
- VERSION
|
56
55
|
- lib/lumberjack.rb
|
@@ -69,6 +68,7 @@ files:
|
|
69
68
|
- lib/lumberjack/log_entry.rb
|
70
69
|
- lib/lumberjack/logger.rb
|
71
70
|
- lib/lumberjack/rack.rb
|
71
|
+
- lib/lumberjack/rack/request_id.rb
|
72
72
|
- lib/lumberjack/rack/unit_of_work.rb
|
73
73
|
- lib/lumberjack/severity.rb
|
74
74
|
- lib/lumberjack/template.rb
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- spec/log_entry_spec.rb
|
87
87
|
- spec/logger_spec.rb
|
88
88
|
- spec/lumberjack_spec.rb
|
89
|
+
- spec/rack/request_id_spec.rb
|
89
90
|
- spec/rack/unit_of_work_spec.rb
|
90
91
|
- spec/severity_spec.rb
|
91
92
|
- spec/spec_helper.rb
|
@@ -95,10 +96,7 @@ licenses:
|
|
95
96
|
- MIT
|
96
97
|
metadata: {}
|
97
98
|
post_install_message:
|
98
|
-
rdoc_options:
|
99
|
-
- "--charset=UTF-8"
|
100
|
-
- "--main"
|
101
|
-
- README.rdoc
|
99
|
+
rdoc_options: []
|
102
100
|
require_paths:
|
103
101
|
- lib
|
104
102
|
required_ruby_version: !ruby/object:Gem::Requirement
|