lumberjack 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -2
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/lib/lumberjack/device/writer.rb +13 -3
- data/lib/lumberjack/logger.rb +1 -1
- data/spec/device/rolling_log_file_spec.rb +4 -4
- data/spec/device/writer_spec.rb +4 -4
- data/spec/logger_spec.rb +3 -3
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -48,7 +48,7 @@ A unit of work can be used to tie together all log messages within a block. This
|
|
48
48
|
|
49
49
|
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
50
|
|
51
|
-
* Lumberjack::Device::
|
51
|
+
* Lumberjack::Device::Writer - Writes log entries to an IO stream.
|
52
52
|
* Lumberjack::Device::LogFile - Writes log entries to a file.
|
53
53
|
* Lumberjack::Device::DateRollingLogFile - Writes log entries to a file that will automatically roll itself based on date.
|
54
54
|
* Lumberjack::Device::SizeRollingLogFile - Writes log entries to a file that will automatically roll itself based on size.
|
@@ -77,7 +77,17 @@ If you use the built in devices, you can also customize the Template used to for
|
|
77
77
|
|
78
78
|
=== Buffered Performance
|
79
79
|
|
80
|
-
The
|
80
|
+
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.
|
81
|
+
|
82
|
+
You can use the <tt>:flush_seconds</tt> 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.
|
83
|
+
|
84
|
+
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.
|
85
|
+
|
86
|
+
# Set buffer to flush after 8K has been written to the log.
|
87
|
+
logger = Lumberjack::Logger.new("application.log", :buffer_size => 8192)
|
88
|
+
|
89
|
+
# Turn off buffering so entries are immediately written to disk.
|
90
|
+
logger = Lumberjack::Logger.new("application.log", :buffer_size => 0)
|
81
91
|
|
82
92
|
=== Automatic Log Rolling
|
83
93
|
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
-
require '
|
4
|
-
require '
|
3
|
+
require 'rubygems/package_task'
|
4
|
+
require 'rdoc/task'
|
5
5
|
|
6
6
|
desc 'Default: run unit tests.'
|
7
7
|
task :default => :test
|
@@ -42,7 +42,7 @@ spec_file = File.expand_path('../lumberjack.gemspec', __FILE__)
|
|
42
42
|
if File.exist?(spec_file)
|
43
43
|
spec = eval(File.read(spec_file))
|
44
44
|
|
45
|
-
|
45
|
+
Gem::PackageTask.new(spec) do |p|
|
46
46
|
p.gem_spec = spec
|
47
47
|
end
|
48
48
|
Rake.application["package"].prerequisites.unshift("rbx:delete_rbc_files")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
@@ -1,12 +1,13 @@
|
|
1
1
|
module Lumberjack
|
2
2
|
class Device
|
3
|
-
# This logging device writes log entries as strings to an IO stream.
|
3
|
+
# This logging device writes log entries as strings to an IO stream. By default, messages will be buffered
|
4
|
+
# and written to the stream in a batch when the buffer is full or when +flush+ is called.
|
4
5
|
class Writer < Device
|
5
6
|
DEFAULT_FIRST_LINE_TEMPLATE = "[:time :severity :progname(:pid) #:unit_of_work_id] :message".freeze
|
6
7
|
DEFAULT_ADDITIONAL_LINES_TEMPLATE = "#{Lumberjack::LINE_SEPARATOR}> [#:unit_of_work_id] :message".freeze
|
7
8
|
|
8
9
|
# The size of the internal buffer. Defaults to 32K.
|
9
|
-
|
10
|
+
attr_reader :buffer_size
|
10
11
|
|
11
12
|
# Internal buffer to batch writes to the stream.
|
12
13
|
class Buffer # :nodoc:
|
@@ -49,12 +50,14 @@ module Lumberjack
|
|
49
50
|
# The default template is <tt>"[:time :severity :progname(:pid) #:unit_of_work_id] :message"</tt>
|
50
51
|
# with additional lines formatted as <tt>"\n [#:unit_of_work_id] :message"</tt>. The unit of
|
51
52
|
# work id will only appear if it is present.
|
53
|
+
#
|
54
|
+
# The size of the internal buffer in bytes can be set by providing <tt>:buffer_size</tt> (defaults to 32K).
|
52
55
|
def initialize(stream, options = {})
|
53
56
|
@lock = Mutex.new
|
54
57
|
@stream = stream
|
55
58
|
@stream.sync = true if @stream.respond_to?(:sync=)
|
56
59
|
@buffer = Buffer.new
|
57
|
-
@buffer_size = (options[:buffer_size] ||
|
60
|
+
@buffer_size = (options[:buffer_size] || 0)
|
58
61
|
template = (options[:template] || DEFAULT_FIRST_LINE_TEMPLATE)
|
59
62
|
if template.respond_to?(:call)
|
60
63
|
@template = template
|
@@ -64,6 +67,13 @@ module Lumberjack
|
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
70
|
+
# Set the buffer size in bytes. The device will only be physically written to when the buffer size
|
71
|
+
# is exceeded.
|
72
|
+
def buffer_size=(value)
|
73
|
+
@buffer_size = value
|
74
|
+
flush
|
75
|
+
end
|
76
|
+
|
67
77
|
# Write an entry to the stream. The entry will be converted into a string using the defined template.
|
68
78
|
def write(entry)
|
69
79
|
string = @template.call(entry)
|
data/lib/lumberjack/logger.rb
CHANGED
@@ -13,7 +13,7 @@ describe Lumberjack::Device::RollingLogFile do
|
|
13
13
|
let(:entry){ Lumberjack::LogEntry.new(Time.now, 1, "New log entry", nil, $$, nil) }
|
14
14
|
|
15
15
|
it "should check for rolling the log file on flush" do
|
16
|
-
device = Lumberjack::Device::RollingLogFile.new(File.join(tmp_dir, "test.log"))
|
16
|
+
device = Lumberjack::Device::RollingLogFile.new(File.join(tmp_dir, "test.log"), :buffer_size => 32767)
|
17
17
|
device.write(entry)
|
18
18
|
device.should_receive(:roll_file?).twice.and_return(false)
|
19
19
|
device.flush
|
@@ -22,7 +22,7 @@ describe Lumberjack::Device::RollingLogFile do
|
|
22
22
|
|
23
23
|
it "should roll the file by archiving the existing file and opening a new stream and calling after_roll" do
|
24
24
|
log_file = File.join(tmp_dir, "test_2.log")
|
25
|
-
device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message")
|
25
|
+
device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :buffer_size => 32767)
|
26
26
|
device.should_receive(:roll_file?).and_return(false, true)
|
27
27
|
device.should_receive(:after_roll)
|
28
28
|
device.stub!(:archive_file_suffix).and_return("rolled")
|
@@ -59,7 +59,7 @@ describe Lumberjack::Device::RollingLogFile do
|
|
59
59
|
|
60
60
|
process_count.times do
|
61
61
|
Process.fork do
|
62
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => max_size, :template => ":message")
|
62
|
+
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => max_size, :template => ":message", :buffer_size => 32767)
|
63
63
|
threads = []
|
64
64
|
thread_count.times do
|
65
65
|
threads << Thread.new do
|
@@ -95,7 +95,7 @@ describe Lumberjack::Device::RollingLogFile do
|
|
95
95
|
|
96
96
|
it "should only keep a specified number of archived log files" do
|
97
97
|
log_file = File.join(tmp_dir, "test_5.log")
|
98
|
-
device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :keep => 2)
|
98
|
+
device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :keep => 2, :buffer_size => 32767)
|
99
99
|
device.should_receive(:roll_file?).and_return(false, true, true, true)
|
100
100
|
device.stub!(:archive_file_suffix).and_return("delete", "another", "keep")
|
101
101
|
t = Time.now
|
data/spec/device/writer_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe Lumberjack::Device::Writer do
|
|
8
8
|
let(:entry){ Lumberjack::LogEntry.new(time, Lumberjack::Severity::INFO, "test message", "app", 12345, "ABCD") }
|
9
9
|
|
10
10
|
it "should buffer output and not write directly to the stream" do
|
11
|
-
device = Lumberjack::Device::Writer.new(stream, :template => ":message")
|
11
|
+
device = Lumberjack::Device::Writer.new(stream, :template => ":message", :buffer_size => 32767)
|
12
12
|
device.write(entry)
|
13
13
|
stream.string.should == ""
|
14
14
|
device.flush
|
@@ -34,7 +34,7 @@ describe Lumberjack::Device::Writer do
|
|
34
34
|
def io.sync; @sync; end
|
35
35
|
io.init
|
36
36
|
|
37
|
-
device = Lumberjack::Device::Writer.new(io, :template => ":message")
|
37
|
+
device = Lumberjack::Device::Writer.new(io, :template => ":message", :buffer_size => 32767)
|
38
38
|
device.write(entry)
|
39
39
|
io.string.should == ""
|
40
40
|
device.flush
|
@@ -49,9 +49,9 @@ describe Lumberjack::Device::Writer do
|
|
49
49
|
device.buffer_size.should == 100
|
50
50
|
end
|
51
51
|
|
52
|
-
it "should have a default buffer size of
|
52
|
+
it "should have a default buffer size of 0" do
|
53
53
|
device = Lumberjack::Device::Writer.new(stream)
|
54
|
-
device.buffer_size.should ==
|
54
|
+
device.buffer_size.should == 0
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should write entries out to the stream with a default template" do
|
data/spec/logger_spec.rb
CHANGED
@@ -173,7 +173,7 @@ describe Lumberjack::Logger do
|
|
173
173
|
context "flushing" do
|
174
174
|
it "should autoflush the buffer if it hasn't been flushed in a specified number of seconds" do
|
175
175
|
output = StringIO.new
|
176
|
-
logger = Lumberjack::Logger.new(output, :flush_seconds => 0.1, :level => Lumberjack::Severity::INFO, :template => ":message")
|
176
|
+
logger = Lumberjack::Logger.new(output, :flush_seconds => 0.1, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
177
177
|
logger.info("message 1")
|
178
178
|
logger.info("message 2")
|
179
179
|
output.string.should == ""
|
@@ -187,7 +187,7 @@ describe Lumberjack::Logger do
|
|
187
187
|
|
188
188
|
it "should write the log entries to the device on flush and update the last flushed time" do
|
189
189
|
output = StringIO.new
|
190
|
-
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message")
|
190
|
+
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
191
191
|
logger.info("message 1")
|
192
192
|
output.string.should == ""
|
193
193
|
last_flushed_at = logger.last_flushed_at
|
@@ -198,7 +198,7 @@ describe Lumberjack::Logger do
|
|
198
198
|
|
199
199
|
it "should flush the buffer and close the devices" do
|
200
200
|
output = StringIO.new
|
201
|
-
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message")
|
201
|
+
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
202
202
|
logger.info("message 1")
|
203
203
|
output.string.should == ""
|
204
204
|
logger.close
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lumberjack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Durand
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-23 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
120
|
requirements: []
|
121
121
|
|
122
122
|
rubyforge_project:
|
123
|
-
rubygems_version: 1.5.
|
123
|
+
rubygems_version: 1.5.2
|
124
124
|
signing_key:
|
125
125
|
specification_version: 3
|
126
126
|
summary: A simple, powerful, and very fast logging utility that can be a drop in replacement for Logger or ActiveSupport::BufferedLogger.
|