lumberjack 1.0.13 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +129 -0
- data/{MIT_LICENSE → MIT_LICENSE.txt} +0 -0
- data/README.md +142 -23
- data/VERSION +1 -1
- data/lib/lumberjack.rb +70 -22
- data/lib/lumberjack/context.rb +35 -0
- data/lib/lumberjack/device.rb +22 -8
- data/lib/lumberjack/device/date_rolling_log_file.rb +9 -9
- data/lib/lumberjack/device/log_file.rb +14 -3
- data/lib/lumberjack/device/multi.rb +46 -0
- data/lib/lumberjack/device/null.rb +1 -3
- data/lib/lumberjack/device/rolling_log_file.rb +45 -21
- data/lib/lumberjack/device/size_rolling_log_file.rb +10 -10
- data/lib/lumberjack/device/writer.rb +92 -61
- data/lib/lumberjack/formatter.rb +97 -28
- data/lib/lumberjack/formatter/date_time_formatter.rb +25 -0
- data/lib/lumberjack/formatter/exception_formatter.rb +25 -2
- data/lib/lumberjack/formatter/id_formatter.rb +23 -0
- data/lib/lumberjack/formatter/object_formatter.rb +12 -0
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +4 -4
- data/lib/lumberjack/formatter/string_formatter.rb +1 -1
- data/lib/lumberjack/formatter/strip_formatter.rb +12 -0
- data/lib/lumberjack/formatter/structured_formatter.rb +63 -0
- data/lib/lumberjack/log_entry.rb +44 -16
- data/lib/lumberjack/logger.rb +275 -69
- 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/rack/unit_of_work.rb +1 -1
- data/lib/lumberjack/severity.rb +11 -10
- data/lib/lumberjack/tag_formatter.rb +96 -0
- data/lib/lumberjack/tagged_logger_support.rb +66 -0
- data/lib/lumberjack/tagged_logging.rb +29 -0
- data/lib/lumberjack/tags.rb +42 -0
- data/lib/lumberjack/template.rb +81 -33
- data/lumberjack.gemspec +31 -0
- metadata +26 -53
- 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
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Device::SizeRollingLogFile do
|
4
|
-
|
5
|
-
before :all do
|
6
|
-
create_tmp_dir
|
7
|
-
end
|
8
|
-
|
9
|
-
after :all do
|
10
|
-
delete_tmp_dir
|
11
|
-
end
|
12
|
-
|
13
|
-
before :each do
|
14
|
-
delete_tmp_files
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should roll a file when it gets to a specified size" do
|
18
|
-
log_file = File.join(tmp_dir, "a#{rand(1000000000)}.log")
|
19
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => 40, :template => ":message", :min_roll_check => 0)
|
20
|
-
logger = Lumberjack::Logger.new(device, :buffer_size => 2)
|
21
|
-
4.times do |i|
|
22
|
-
logger.error("test message #{i + 1}")
|
23
|
-
logger.flush
|
24
|
-
end
|
25
|
-
logger.close
|
26
|
-
|
27
|
-
File.read("#{log_file}.1").split(Lumberjack::LINE_SEPARATOR).should == ["test message 1", "test message 2", "test message 3"]
|
28
|
-
File.read(log_file).should == "test message 4#{Lumberjack::LINE_SEPARATOR}"
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should be able to specify the max size in kilobytes" do
|
32
|
-
log_file = File.join(tmp_dir, "b#{rand(1000000000)}.log")
|
33
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "32K", :min_roll_check => 0)
|
34
|
-
device.max_size.should == 32768
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should be able to specify the max size in megabytes" do
|
38
|
-
log_file = File.join(tmp_dir, "c#{rand(1000000000)}.log")
|
39
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "100M", :min_roll_check => 0)
|
40
|
-
device.max_size.should == 104_857_600
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should be able to specify the max size in gigabytes" do
|
44
|
-
log_file = File.join(tmp_dir, "d#{rand(1000000000)}.log")
|
45
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "1G", :min_roll_check => 0)
|
46
|
-
device.max_size.should == 1_073_741_824
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should figure out the next archive file name available" do
|
50
|
-
log_file = File.join(tmp_dir, "filename.log")
|
51
|
-
(3..11).each do |i|
|
52
|
-
File.open("#{log_file}.#{i}", 'w'){|f| f.write(i.to_s)}
|
53
|
-
end
|
54
|
-
device = Lumberjack::Device::SizeRollingLogFile.new(log_file, :max_size => "100M", :min_roll_check => 0)
|
55
|
-
device.archive_file_suffix.should == "12"
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
data/spec/device/writer_spec.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Device::Writer do
|
4
|
-
|
5
|
-
let(:time_string){ "2011-01-15T14:23:45.123" }
|
6
|
-
let(:time){ Time.parse(time_string) }
|
7
|
-
let(:stream){ StringIO.new }
|
8
|
-
let(:entry){ Lumberjack::LogEntry.new(time, Lumberjack::Severity::INFO, "test message", "app", 12345, "ABCD") }
|
9
|
-
|
10
|
-
it "should buffer output and not write directly to the stream" do
|
11
|
-
device = Lumberjack::Device::Writer.new(stream, :template => ":message", :buffer_size => 32767)
|
12
|
-
device.write(entry)
|
13
|
-
stream.string.should == ""
|
14
|
-
device.flush
|
15
|
-
stream.string.should == "test message#{Lumberjack::LINE_SEPARATOR}"
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should flush the buffer when it gets to the specified size" do
|
19
|
-
device = Lumberjack::Device::Writer.new(stream, :buffer_size => 15, :template => ":message")
|
20
|
-
device.write(entry)
|
21
|
-
stream.string.should == ""
|
22
|
-
device.write(entry)
|
23
|
-
stream.string.should == "test message#{Lumberjack::LINE_SEPARATOR}test message#{Lumberjack::LINE_SEPARATOR}"
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should sync the stream and flush it when the device is flushed" do
|
27
|
-
# Create an IO like object that require flush to be called
|
28
|
-
io = Object.new
|
29
|
-
def io.init; @string = ""; @buffer = ""; @sync = false; end
|
30
|
-
def io.write(string); @buffer << string; end
|
31
|
-
def io.flush; @string << @buffer; @buffer = ""; end
|
32
|
-
def io.string; @string; end
|
33
|
-
def io.sync=(val); @sync = val; end
|
34
|
-
def io.sync; @sync; end
|
35
|
-
io.init
|
36
|
-
|
37
|
-
device = Lumberjack::Device::Writer.new(io, :template => ":message", :buffer_size => 32767)
|
38
|
-
device.write(entry)
|
39
|
-
io.string.should == ""
|
40
|
-
device.flush
|
41
|
-
io.string.should == "test message#{Lumberjack::LINE_SEPARATOR}"
|
42
|
-
io.sync.should == true
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should be able to set the buffer size" do
|
46
|
-
device = Lumberjack::Device::Writer.new(stream, :buffer_size => 15)
|
47
|
-
device.buffer_size.should == 15
|
48
|
-
device.buffer_size = 100
|
49
|
-
device.buffer_size.should == 100
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should have a default buffer size of 0" do
|
53
|
-
device = Lumberjack::Device::Writer.new(stream)
|
54
|
-
device.buffer_size.should == 0
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should write entries out to the stream with a default template" do
|
58
|
-
device = Lumberjack::Device::Writer.new(stream)
|
59
|
-
device.write(entry)
|
60
|
-
device.flush
|
61
|
-
stream.string.should == "[2011-01-15T14:23:45.123 INFO app(12345) #ABCD] test message#{Lumberjack::LINE_SEPARATOR}"
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should write entries out to the stream with a custom template" do
|
65
|
-
device = Lumberjack::Device::Writer.new(stream, :template => ":message")
|
66
|
-
device.write(entry)
|
67
|
-
device.flush
|
68
|
-
stream.string.should == "test message#{Lumberjack::LINE_SEPARATOR}"
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should be able to specify the time format for the template" do
|
72
|
-
device = Lumberjack::Device::Writer.new(stream, :time_format => :microseconds)
|
73
|
-
device.write(entry)
|
74
|
-
device.flush
|
75
|
-
stream.string.should == "[2011-01-15T14:23:45.123000 INFO app(12345) #ABCD] test message#{Lumberjack::LINE_SEPARATOR}"
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should be able to specify a block template for log entries" do
|
79
|
-
device = Lumberjack::Device::Writer.new(stream, :template => lambda{|e| e.message.upcase})
|
80
|
-
device.write(entry)
|
81
|
-
device.flush
|
82
|
-
stream.string.should == "TEST MESSAGE#{Lumberjack::LINE_SEPARATOR}"
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should write to STDERR if an error is raised when flushing to the stream" do
|
86
|
-
stderr = $stderr
|
87
|
-
$stderr = StringIO.new
|
88
|
-
begin
|
89
|
-
device = Lumberjack::Device::Writer.new(stream, :template => ":message")
|
90
|
-
stream.should_receive(:write).and_raise(StandardError.new("Cannot write to stream"))
|
91
|
-
device.write(entry)
|
92
|
-
device.flush
|
93
|
-
$stderr.string.should include("test message#{Lumberjack::LINE_SEPARATOR}")
|
94
|
-
$stderr.string.should include("StandardError: Cannot write to stream")
|
95
|
-
ensure
|
96
|
-
$stderr = stderr
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context "multi line messages" do
|
101
|
-
let(:message){ "line 1#{Lumberjack::LINE_SEPARATOR}line 2#{Lumberjack::LINE_SEPARATOR}line 3" }
|
102
|
-
let(:entry){ Lumberjack::LogEntry.new(time, Lumberjack::Severity::INFO, message, "app", 12345, "ABCD") }
|
103
|
-
|
104
|
-
it "should have a default template for multiline messages" do
|
105
|
-
device = Lumberjack::Device::Writer.new(stream)
|
106
|
-
device.write(entry)
|
107
|
-
device.flush
|
108
|
-
stream.string.split(Lumberjack::LINE_SEPARATOR).should == ["[2011-01-15T14:23:45.123 INFO app(12345) #ABCD] line 1", "> [#ABCD] line 2", "> [#ABCD] line 3"]
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should be able to specify a template for multiple line messages" do
|
112
|
-
device = Lumberjack::Device::Writer.new(stream, :additional_lines => " // :message")
|
113
|
-
device.write(entry)
|
114
|
-
device.flush
|
115
|
-
stream.string.should == "[2011-01-15T14:23:45.123 INFO app(12345) #ABCD] line 1 // line 2 // line 3#{Lumberjack::LINE_SEPARATOR}"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Formatter::ExceptionFormatter do
|
4
|
-
|
5
|
-
it "should convert an exception without a backtrace to a string" do
|
6
|
-
e = ArgumentError.new("not expected")
|
7
|
-
formatter = Lumberjack::Formatter::ExceptionFormatter.new
|
8
|
-
formatter.call(e).should == "ArgumentError: not expected"
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should convert an exception with a backtrace to a string" do
|
12
|
-
begin
|
13
|
-
raise ArgumentError.new("not expected")
|
14
|
-
rescue => e
|
15
|
-
formatter = Lumberjack::Formatter::ExceptionFormatter.new
|
16
|
-
formatter.call(e).should == "ArgumentError: not expected#{Lumberjack::LINE_SEPARATOR} #{e.backtrace.join(Lumberjack::LINE_SEPARATOR + ' ')}"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Formatter::InspectFormatter do
|
4
|
-
|
5
|
-
it "should format objects as string by calling their inspect method" do
|
6
|
-
formatter = Lumberjack::Formatter::InspectFormatter.new
|
7
|
-
formatter.call("abc").should == "\"abc\""
|
8
|
-
formatter.call(:test).should == ":test"
|
9
|
-
formatter.call(1).should == "1"
|
10
|
-
formatter.call([:a, 1, "b"]).should == [:a, 1, "b"].inspect
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Formatter::PrettyPrintFormatter do
|
4
|
-
|
5
|
-
it "should convert an object to a string using pretty print" do
|
6
|
-
object = Object.new
|
7
|
-
def object.pretty_print(q)
|
8
|
-
q.text "woot!"
|
9
|
-
end
|
10
|
-
formatter = Lumberjack::Formatter::PrettyPrintFormatter.new
|
11
|
-
formatter.call(object).should == "woot!"
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Formatter::StringFormatter do
|
4
|
-
|
5
|
-
it "should format objects as string by calling their to_s method" do
|
6
|
-
formatter = Lumberjack::Formatter::StringFormatter.new
|
7
|
-
formatter.call("abc").should == "abc"
|
8
|
-
formatter.call(:test).should == "test"
|
9
|
-
formatter.call(1).should == "1"
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
data/spec/formatter_spec.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::Formatter do
|
4
|
-
|
5
|
-
let(:formatter){ Lumberjack::Formatter.new }
|
6
|
-
|
7
|
-
it "should have a default set of formatters" do
|
8
|
-
formatter.format("abc").should == "abc"
|
9
|
-
formatter.format([1, 2, 3]).should == "[1, 2, 3]"
|
10
|
-
formatter.format(ArgumentError.new("boom")).should == "ArgumentError: boom"
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to add a formatter object for a class" do
|
14
|
-
formatter.add(Numeric, lambda{|obj| "number: #{obj}"})
|
15
|
-
formatter.format(10).should == "number: 10"
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should be able to add a formatter block for a class" do
|
19
|
-
formatter.add(Numeric){|obj| "number: #{obj}"}
|
20
|
-
formatter.format(10).should == "number: 10"
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should be able to remove a formatter for a class" do
|
24
|
-
formatter.remove(String)
|
25
|
-
formatter.format("abc").should == "\"abc\""
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should be able to chain add and remove calls" do
|
29
|
-
formatter.remove(String).should == formatter
|
30
|
-
formatter.add(String, Lumberjack::Formatter::StringFormatter.new).should == formatter
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should format an object based on the class hierarchy" do
|
34
|
-
formatter.add(Numeric){|obj| "number: #{obj}"}
|
35
|
-
formatter.add(Integer){|obj| "fixed number: #{obj}"}
|
36
|
-
formatter.format(10).should == "fixed number: 10"
|
37
|
-
formatter.format(10.1).should == "number: 10.1"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should have a default formatter" do
|
41
|
-
formatter.remove(Object)
|
42
|
-
formatter.format(:test).should == ":test"
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
data/spec/log_entry_spec.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Lumberjack::LogEntry do
|
4
|
-
|
5
|
-
it "should have a time" do
|
6
|
-
t = Time.now
|
7
|
-
entry = Lumberjack::LogEntry.new(t, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
8
|
-
entry.time.should == t
|
9
|
-
entry.time = t + 1
|
10
|
-
entry.time.should == t + 1
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should have a severity" do
|
14
|
-
entry = Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
15
|
-
entry.severity.should == Lumberjack::Severity::INFO
|
16
|
-
entry.severity = Lumberjack::Severity::WARN
|
17
|
-
entry.severity.should == Lumberjack::Severity::WARN
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should convert a severity label to a numeric level" do
|
21
|
-
entry = Lumberjack::LogEntry.new(Time.now, "INFO", "test", "app", 1500, "ABCD")
|
22
|
-
entry.severity.should == Lumberjack::Severity::INFO
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should get the severity as a string" do
|
26
|
-
Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::DEBUG, "test", "app", 1500, nil).severity_label.should == "DEBUG"
|
27
|
-
Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, nil).severity_label.should == "INFO"
|
28
|
-
Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::WARN, "test", "app", 1500, nil).severity_label.should == "WARN"
|
29
|
-
Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::ERROR, "test", "app", 1500, nil).severity_label.should == "ERROR"
|
30
|
-
Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::FATAL, "test", "app", 1500, nil).severity_label.should == "FATAL"
|
31
|
-
Lumberjack::LogEntry.new(Time.now, -1, "test", "app", 1500, nil).severity_label.should == "UNKNOWN"
|
32
|
-
Lumberjack::LogEntry.new(Time.now, 1000, "test", "app", 1500, nil).severity_label.should == "UNKNOWN"
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should have a message" do
|
36
|
-
entry = Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
37
|
-
entry.message.should == "test"
|
38
|
-
entry.message = "new message"
|
39
|
-
entry.message.should == "new message"
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should have a progname" do
|
43
|
-
entry = Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
44
|
-
entry.progname.should == "app"
|
45
|
-
entry.progname = "prog"
|
46
|
-
entry.progname.should == "prog"
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should have a pid" do
|
50
|
-
entry = Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
51
|
-
entry.pid.should == 1500
|
52
|
-
entry.pid = 150
|
53
|
-
entry.pid.should == 150
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should have a unit_of_work_id" do
|
57
|
-
entry = Lumberjack::LogEntry.new(Time.now, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
58
|
-
entry.unit_of_work_id.should == "ABCD"
|
59
|
-
entry.unit_of_work_id = "1234"
|
60
|
-
entry.unit_of_work_id.should == "1234"
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should be converted to a string" do
|
64
|
-
t = Time.parse("2011-01-29T12:15:32.001")
|
65
|
-
entry = Lumberjack::LogEntry.new(t, Lumberjack::Severity::INFO, "test", "app", 1500, "ABCD")
|
66
|
-
entry.to_s.should == "[2011-01-29T12:15:32.001 INFO app(1500) #ABCD] test"
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
data/spec/logger_spec.rb
DELETED
@@ -1,411 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'pathname'
|
3
|
-
|
4
|
-
describe Lumberjack::Logger do
|
5
|
-
|
6
|
-
context "initialization" do
|
7
|
-
|
8
|
-
before :all do
|
9
|
-
create_tmp_dir
|
10
|
-
end
|
11
|
-
|
12
|
-
after :all do
|
13
|
-
delete_tmp_dir
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should wrap an IO stream in a device" do
|
17
|
-
output = StringIO.new
|
18
|
-
logger = Lumberjack::Logger.new(output)
|
19
|
-
logger.device.class.should == Lumberjack::Device::Writer
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should have a formatter" do
|
23
|
-
output = StringIO.new
|
24
|
-
logger = Lumberjack::Logger.new(output)
|
25
|
-
logger.formatter.should be
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should open a file path in a device" do
|
29
|
-
logger = Lumberjack::Logger.new(File.join(tmp_dir, "log_file_1.log"))
|
30
|
-
logger.device.class.should == Lumberjack::Device::LogFile
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should open a pathname in a device" do
|
34
|
-
logger = Lumberjack::Logger.new(Pathname.new(File.join(tmp_dir, "log_file_1.log")))
|
35
|
-
logger.device.class.should == Lumberjack::Device::LogFile
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should use the null device if the stream is :null" do
|
39
|
-
logger = Lumberjack::Logger.new(:null)
|
40
|
-
logger.device.class.should == Lumberjack::Device::Null
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should set the level with a numeric" do
|
44
|
-
logger = Lumberjack::Logger.new(:null, :level => Lumberjack::Severity::WARN)
|
45
|
-
logger.level.should == Lumberjack::Severity::WARN
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should set the level with a level" do
|
49
|
-
logger = Lumberjack::Logger.new(:null, :level => :warn)
|
50
|
-
logger.level.should == Lumberjack::Severity::WARN
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should default the level to INFO" do
|
54
|
-
logger = Lumberjack::Logger.new(:null)
|
55
|
-
logger.level.should == Lumberjack::Severity::INFO
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should set the progname"do
|
59
|
-
logger = Lumberjack::Logger.new(:null, :progname => "app")
|
60
|
-
logger.progname.should == "app"
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should create a thread to flush the device" do
|
64
|
-
Thread.should_receive(:new)
|
65
|
-
logger = Lumberjack::Logger.new(:null, :flush_seconds => 10)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "attributes" do
|
70
|
-
it "should have a level" do
|
71
|
-
logger = Lumberjack::Logger.new
|
72
|
-
logger.level = Lumberjack::Severity::DEBUG
|
73
|
-
logger.level.should == Lumberjack::Severity::DEBUG
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should have a progname" do
|
77
|
-
logger = Lumberjack::Logger.new
|
78
|
-
logger.progname = "app"
|
79
|
-
logger.progname.should == "app"
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should be able to silence the log in a block" do
|
83
|
-
output = StringIO.new
|
84
|
-
logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
|
85
|
-
logger.info("one")
|
86
|
-
logger.silence do
|
87
|
-
logger.level.should == Lumberjack::Severity::ERROR
|
88
|
-
logger.info("two")
|
89
|
-
logger.error("three")
|
90
|
-
end
|
91
|
-
logger.info("four")
|
92
|
-
output.string.split.should == ["one", "three", "four"]
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should be able to customize the level of silence in a block" do
|
96
|
-
output = StringIO.new
|
97
|
-
logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
|
98
|
-
logger.info("one")
|
99
|
-
logger.silence(Lumberjack::Severity::FATAL) do
|
100
|
-
logger.level.should == Lumberjack::Severity::FATAL
|
101
|
-
logger.info("two")
|
102
|
-
logger.error("three")
|
103
|
-
logger.fatal("woof")
|
104
|
-
end
|
105
|
-
logger.info("four")
|
106
|
-
output.string.split.should == ["one", "woof", "four"]
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should not be able to silence the logger if silencing is disabled" do
|
110
|
-
output = StringIO.new
|
111
|
-
logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
|
112
|
-
logger.silencer = false
|
113
|
-
logger.info("one")
|
114
|
-
logger.silence do
|
115
|
-
logger.level.should == Lumberjack::Severity::INFO
|
116
|
-
logger.info("two")
|
117
|
-
logger.error("three")
|
118
|
-
end
|
119
|
-
logger.info("four")
|
120
|
-
output.string.split.should == ["one", "two", "three", "four"]
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should be able to set the progname in a block" do
|
124
|
-
logger = Lumberjack::Logger.new
|
125
|
-
logger.set_progname("app")
|
126
|
-
logger.progname.should == "app"
|
127
|
-
block_executed = false
|
128
|
-
logger.set_progname("xxx") do
|
129
|
-
block_executed = true
|
130
|
-
logger.progname.should == "xxx"
|
131
|
-
end
|
132
|
-
block_executed.should == true
|
133
|
-
logger.progname.should == "app"
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should only affect the current thread when silencing the logger" do
|
137
|
-
output = StringIO.new
|
138
|
-
logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
|
139
|
-
# status is used to make sure the two threads are executing at the same time
|
140
|
-
status = 0
|
141
|
-
begin
|
142
|
-
Thread.new do
|
143
|
-
logger.silence do
|
144
|
-
logger.info("inner")
|
145
|
-
status = 1
|
146
|
-
loop{ sleep(0.001); break if status == 2}
|
147
|
-
end
|
148
|
-
end
|
149
|
-
loop{ sleep(0.001); break if status == 1}
|
150
|
-
logger.info("outer")
|
151
|
-
status = 2
|
152
|
-
logger.close
|
153
|
-
output.string.should include("outer")
|
154
|
-
output.string.should_not include("inner")
|
155
|
-
ensure
|
156
|
-
status = 2
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
it "should only affect the current thread when changing the progname in a block" do
|
161
|
-
output = StringIO.new
|
162
|
-
logger = Lumberjack::Logger.new(output, :progname => "thread1", :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":progname :message")
|
163
|
-
# status is used to make sure the two threads are executing at the same time
|
164
|
-
status = 0
|
165
|
-
begin
|
166
|
-
Thread.new do
|
167
|
-
logger.set_progname("thread2") do
|
168
|
-
logger.info("inner")
|
169
|
-
status = 1
|
170
|
-
loop{ sleep(0.001); break if status == 2}
|
171
|
-
end
|
172
|
-
end
|
173
|
-
loop{ sleep(0.001); break if status == 1}
|
174
|
-
logger.info("outer")
|
175
|
-
status = 2
|
176
|
-
logger.close
|
177
|
-
output.string.should include("thread1")
|
178
|
-
output.string.should include("thread2")
|
179
|
-
ensure
|
180
|
-
status = 2
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
context "flushing" do
|
186
|
-
it "should autoflush the buffer if it hasn't been flushed in a specified number of seconds" do
|
187
|
-
output = StringIO.new
|
188
|
-
logger = Lumberjack::Logger.new(output, :flush_seconds => 0.1, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
189
|
-
logger.info("message 1")
|
190
|
-
logger.info("message 2")
|
191
|
-
output.string.should == ""
|
192
|
-
sleep(0.15)
|
193
|
-
output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1", "message 2"]
|
194
|
-
logger.info("message 3")
|
195
|
-
output.string.should_not include("message 3")
|
196
|
-
sleep(0.15)
|
197
|
-
output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1", "message 2", "message 3"]
|
198
|
-
end
|
199
|
-
|
200
|
-
it "should write the log entries to the device on flush and update the last flushed time" do
|
201
|
-
output = StringIO.new
|
202
|
-
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
203
|
-
logger.info("message 1")
|
204
|
-
output.string.should == ""
|
205
|
-
last_flushed_at = logger.last_flushed_at
|
206
|
-
logger.flush
|
207
|
-
output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1"]
|
208
|
-
logger.last_flushed_at.should >= last_flushed_at
|
209
|
-
end
|
210
|
-
|
211
|
-
it "should flush the buffer and close the devices" do
|
212
|
-
output = StringIO.new
|
213
|
-
logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
|
214
|
-
logger.info("message 1")
|
215
|
-
output.string.should == ""
|
216
|
-
logger.close
|
217
|
-
output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1"]
|
218
|
-
output.should be_closed
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
context "logging" do
|
223
|
-
let(:output){ StringIO.new }
|
224
|
-
let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0) }
|
225
|
-
let(:logger){ Lumberjack::Logger.new(device, :level => Lumberjack::Severity::INFO, :progname => "test") }
|
226
|
-
let(:n){ Lumberjack::LINE_SEPARATOR }
|
227
|
-
|
228
|
-
it "should add entries with a numeric severity and a message" do
|
229
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
230
|
-
Time.stub(:now => time)
|
231
|
-
logger.add(Lumberjack::Severity::INFO, "test")
|
232
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should add entries with a severity label" do
|
236
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
237
|
-
Time.stub(:now => time)
|
238
|
-
logger.add(:info, "test")
|
239
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should add entries with a custom progname and message" do
|
243
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
244
|
-
Time.stub(:now => time)
|
245
|
-
logger.add(Lumberjack::Severity::INFO, "test", "app")
|
246
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO app(#{$$}) #] test#{n}"
|
247
|
-
end
|
248
|
-
|
249
|
-
it "should add entries with a local progname and message" do
|
250
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
251
|
-
Time.stub(:now => time)
|
252
|
-
logger.set_progname("block") do
|
253
|
-
logger.add(Lumberjack::Severity::INFO, "test")
|
254
|
-
end
|
255
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO block(#{$$}) #] test#{n}"
|
256
|
-
end
|
257
|
-
|
258
|
-
it "should add entries with a progname but no message or block" do
|
259
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
260
|
-
Time.stub(:now => time)
|
261
|
-
logger.set_progname("default") do
|
262
|
-
logger.add(Lumberjack::Severity::INFO, nil, "message")
|
263
|
-
end
|
264
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO default(#{$$}) #] message#{n}"
|
265
|
-
end
|
266
|
-
|
267
|
-
it "should add entries with a block" do
|
268
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
269
|
-
Time.stub(:now => time)
|
270
|
-
logger.add(Lumberjack::Severity::INFO){"test"}
|
271
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
|
272
|
-
end
|
273
|
-
|
274
|
-
it "should log entries (::Logger compatibility)" do
|
275
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
276
|
-
Time.stub(:now => time)
|
277
|
-
logger.log(Lumberjack::Severity::INFO, "test")
|
278
|
-
output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
|
279
|
-
end
|
280
|
-
|
281
|
-
it "should append messages with unknown severity to the log" do
|
282
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
283
|
-
Time.stub(:now => time)
|
284
|
-
logger << "test"
|
285
|
-
output.string.should == "[2011-01-30T12:31:56.123 UNKNOWN test(#{$$}) #] test#{n}"
|
286
|
-
end
|
287
|
-
|
288
|
-
it "should ouput entries to STDERR if they can't be written the the device" do
|
289
|
-
stderr = $stderr
|
290
|
-
$stderr = StringIO.new
|
291
|
-
begin
|
292
|
-
time = Time.parse("2011-01-30T12:31:56.123")
|
293
|
-
Time.stub(:now => time)
|
294
|
-
device.should_receive(:write).and_raise(StandardError.new("Cannot write to device"))
|
295
|
-
logger.add(Lumberjack::Severity::INFO, "test")
|
296
|
-
$stderr.string.should include("[2011-01-30T12:31:56.123 INFO test(#{$$})] test")
|
297
|
-
$stderr.string.should include("StandardError: Cannot write to device")
|
298
|
-
ensure
|
299
|
-
$stderr = stderr
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
context "log helper methods" do
|
304
|
-
let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, :template => ":message") }
|
305
|
-
|
306
|
-
it "should only add messages whose severity is greater or equal to the logger level" do
|
307
|
-
logger.add(Lumberjack::Severity::DEBUG, "debug")
|
308
|
-
logger.add(Lumberjack::Severity::INFO, "info")
|
309
|
-
logger.add(Lumberjack::Severity::ERROR, "error")
|
310
|
-
output.string.should == "info#{n}error#{n}"
|
311
|
-
end
|
312
|
-
|
313
|
-
it "should only log fatal messages when the level is set to fatal" do
|
314
|
-
logger.level = Lumberjack::Severity::FATAL
|
315
|
-
logger.fatal("fatal")
|
316
|
-
logger.fatal?.should == true
|
317
|
-
logger.error("error")
|
318
|
-
logger.error?.should == false
|
319
|
-
logger.warn("warn")
|
320
|
-
logger.warn?.should == false
|
321
|
-
logger.info("info")
|
322
|
-
logger.info?.should == false
|
323
|
-
logger.debug("debug")
|
324
|
-
logger.debug?.should == false
|
325
|
-
logger.unknown("unknown")
|
326
|
-
output.string.should == "fatal#{n}unknown#{n}"
|
327
|
-
end
|
328
|
-
|
329
|
-
it "should only log error messages and higher when the level is set to error" do
|
330
|
-
logger.level = Lumberjack::Severity::ERROR
|
331
|
-
logger.fatal("fatal")
|
332
|
-
logger.fatal?.should == true
|
333
|
-
logger.error("error")
|
334
|
-
logger.error?.should == true
|
335
|
-
logger.warn("warn")
|
336
|
-
logger.warn?.should == false
|
337
|
-
logger.info("info")
|
338
|
-
logger.info?.should == false
|
339
|
-
logger.debug("debug")
|
340
|
-
logger.debug?.should == false
|
341
|
-
logger.unknown("unknown")
|
342
|
-
output.string.should == "fatal#{n}error#{n}unknown#{n}"
|
343
|
-
end
|
344
|
-
|
345
|
-
it "should only log warn messages and higher when the level is set to warn" do
|
346
|
-
logger.level = Lumberjack::Severity::WARN
|
347
|
-
logger.fatal("fatal")
|
348
|
-
logger.fatal?.should == true
|
349
|
-
logger.error("error")
|
350
|
-
logger.error?.should == true
|
351
|
-
logger.warn("warn")
|
352
|
-
logger.warn?.should == true
|
353
|
-
logger.info("info")
|
354
|
-
logger.info?.should == false
|
355
|
-
logger.debug("debug")
|
356
|
-
logger.debug?.should == false
|
357
|
-
logger.unknown("unknown")
|
358
|
-
output.string.should == "fatal#{n}error#{n}warn#{n}unknown#{n}"
|
359
|
-
end
|
360
|
-
|
361
|
-
it "should only log info messages and higher when the level is set to info" do
|
362
|
-
logger.level = Lumberjack::Severity::INFO
|
363
|
-
logger.fatal("fatal")
|
364
|
-
logger.fatal?.should == true
|
365
|
-
logger.error("error")
|
366
|
-
logger.error?.should == true
|
367
|
-
logger.warn("warn")
|
368
|
-
logger.warn?.should == true
|
369
|
-
logger.info("info")
|
370
|
-
logger.info?.should == true
|
371
|
-
logger.debug("debug")
|
372
|
-
logger.debug?.should == false
|
373
|
-
logger.unknown("unknown")
|
374
|
-
output.string.should == "fatal#{n}error#{n}warn#{n}info#{n}unknown#{n}"
|
375
|
-
end
|
376
|
-
|
377
|
-
it "should log all messages when the level is set to debug" do
|
378
|
-
logger.level = Lumberjack::Severity::DEBUG
|
379
|
-
logger.fatal("fatal")
|
380
|
-
logger.fatal?.should == true
|
381
|
-
logger.error("error")
|
382
|
-
logger.error?.should == true
|
383
|
-
logger.warn("warn")
|
384
|
-
logger.warn?.should == true
|
385
|
-
logger.info("info")
|
386
|
-
logger.info?.should == true
|
387
|
-
logger.debug("debug")
|
388
|
-
logger.debug?.should == true
|
389
|
-
logger.unknown("unknown")
|
390
|
-
output.string.should == "fatal#{n}error#{n}warn#{n}info#{n}debug#{n}unknown#{n}"
|
391
|
-
end
|
392
|
-
|
393
|
-
it "should only log unkown messages when the level is set above fatal" do
|
394
|
-
logger.level = Lumberjack::Severity::FATAL + 1
|
395
|
-
logger.fatal("fatal")
|
396
|
-
logger.fatal?.should == false
|
397
|
-
logger.error("error")
|
398
|
-
logger.error?.should == false
|
399
|
-
logger.warn("warn")
|
400
|
-
logger.warn?.should == false
|
401
|
-
logger.info("info")
|
402
|
-
logger.info?.should == false
|
403
|
-
logger.debug("debug")
|
404
|
-
logger.debug?.should == false
|
405
|
-
logger.unknown("unknown")
|
406
|
-
output.string.should == "unknown#{n}"
|
407
|
-
end
|
408
|
-
end
|
409
|
-
end
|
410
|
-
|
411
|
-
end
|