rubysl-logger 1.0.0

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.
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Logger
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/logger/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-logger"
6
+ spec.version = RubySL::Logger::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library logger.}
10
+ spec.summary = %q{Ruby standard library logger.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-logger"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::Application#level=" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
8
+
9
+ end
10
+
11
+ after :each do
12
+ @log_file.close unless @log_file.closed?
13
+ rm_r @file_path
14
+ end
15
+
16
+ it "sets the logging threshold" do
17
+ @app.level = Logger::ERROR
18
+ @app.start
19
+ @app.log(Logger::WARN, "Don't show me")
20
+ @app.log(Logger::ERROR, "Show me")
21
+ @log_file.rewind
22
+ messages = @log_file.readlines
23
+ messages.length.should == 1
24
+ LoggerSpecs::strip_date(messages.first).should == "ERROR -- TestApp: Show me\n"
25
+ end
26
+
27
+ it "can set the threshold to unknown values" do
28
+ @app.level = 10
29
+ @app.start
30
+ @app.log(Logger::INFO, "Info message")
31
+ @app.log(Logger::DEBUG, "Debug message")
32
+ @app.log(Logger::WARN, "Warn message")
33
+ @app.log(Logger::ERROR, "Error message")
34
+ @app.log(Logger::FATAL, "Fatal message")
35
+ @log_file.rewind
36
+ @log_file.readlines.should be_empty
37
+ end
38
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::Application#log" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
8
+ @app.start
9
+ end
10
+
11
+ after :each do
12
+ @log_file.close unless @log_file.closed?
13
+ rm_r @file_path
14
+ end
15
+
16
+ it "logs a message" do
17
+ @app.log(Logger::WARN, "Test message")
18
+ @log_file.rewind
19
+ message = @log_file.readlines.last
20
+ LoggerSpecs::strip_date(message).should == "WARN -- TestApp: Test message\n"
21
+ end
22
+
23
+ it "receives a severity" do
24
+ @app.log(Logger::INFO, "Info message")
25
+ @app.log(Logger::DEBUG, "Debug message")
26
+ @app.log(Logger::WARN, "Warn message")
27
+ @app.log(Logger::ERROR, "Error message")
28
+ @app.log(Logger::FATAL, "Fatal message")
29
+ @log_file.rewind
30
+ messages = @log_file.readlines[3..-1] # remove default messages
31
+
32
+ LoggerSpecs::strip_date(messages[0]).should == "INFO -- TestApp: Info message\n"
33
+ LoggerSpecs::strip_date(messages[1]).should == "DEBUG -- TestApp: Debug message\n"
34
+ LoggerSpecs::strip_date(messages[2]).should == "WARN -- TestApp: Warn message\n"
35
+ LoggerSpecs::strip_date(messages[3]).should == "ERROR -- TestApp: Error message\n"
36
+ LoggerSpecs::strip_date(messages[4]).should == "FATAL -- TestApp: Fatal message\n"
37
+ end
38
+
39
+ it "uses app name for Application Name" do
40
+ @app.log(Logger::INFO, "Info message")
41
+ @log_file.rewind
42
+ test_message = @log_file.readlines.last
43
+ Regexp.new(/TestApp/).should =~ LoggerSpecs::strip_date(test_message)
44
+ end
45
+
46
+ it "receives a block and calls it if message is nil" do
47
+ temp = 0
48
+ @app.log(Logger::INFO, nil) { temp = 1 }
49
+ temp.should == 1
50
+ end
51
+ end
52
+
53
+ describe "Logger::Application#log=" do
54
+ before :each do
55
+ @file_path = tmp("test_log.log")
56
+ @log_file = File.open(@file_path, "w+")
57
+ @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
58
+ @app.start
59
+ end
60
+
61
+ after :all do
62
+ @log_file.close
63
+ rm_r @file_path
64
+ end
65
+
66
+ it "sets the log device" do
67
+ regex = /STDERR Message/
68
+ @app.log = STDERR
69
+ lambda { @app.log(Logger::WARN, "STDERR Message") }.should output_to_fd(regex, STDERR)
70
+ end
71
+ end
72
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::Application.new" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ end
8
+
9
+ after :each do
10
+ @log_file.close unless @log_file.closed?
11
+ rm_r @file_path
12
+ end
13
+
14
+ it "starts the logger on a new application" do
15
+ LoggerSpecs::TestApp.new("TestApp", @log_file).start
16
+ @log_file.rewind # go back to the beginning to read the contents
17
+
18
+ first, second, third = @log_file.readlines
19
+ LoggerSpecs::strip_date(first).should == "INFO -- TestApp: Start of TestApp.\n"
20
+ LoggerSpecs::strip_date(second).should == "WARN -- TestApp: Test log message\n"
21
+ LoggerSpecs::strip_date(third).should == "INFO -- TestApp: End of TestApp. (status: true)\n"
22
+ end
23
+
24
+ it "defaults application name to ''" do
25
+ LoggerSpecs::TestApp.new(nil, @log_file).start
26
+ @log_file.rewind
27
+
28
+ first, second, third = @log_file.readlines
29
+ LoggerSpecs::strip_date(first).should == "INFO -- : Start of .\n"
30
+ LoggerSpecs::strip_date(second).should == "WARN -- : Test log message\n"
31
+ LoggerSpecs::strip_date(third).should == "INFO -- : End of . (status: true)\n"
32
+ end
33
+
34
+ it "defaults logs to STDERR" do
35
+ regex = /INFO.*WARN.*INFO.*/m
36
+ lambda { LoggerSpecs::TestApp.new(nil, nil).start }.should output_to_fd(regex, STDERR)
37
+ @log_file.rewind
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::Application#set_log"do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
8
+ end
9
+
10
+ after :each do
11
+ @log_file.close unless @log_file.closed?
12
+ rm_r @file_path
13
+ end
14
+
15
+ it "sets the log device for the logger" do
16
+ regex = /STDERR Message/
17
+ @app.set_log(STDERR)
18
+ lambda { @app.log(Logger::WARN, "STDERR Message") }.should output_to_fd(regex, STDERR)
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::Application#start" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
8
+ end
9
+
10
+ after :each do
11
+ @log_file.close unless @log_file.closed?
12
+ rm_r @file_path
13
+ end
14
+
15
+
16
+ it "starts the application logging start/end messages" do
17
+ @app.start
18
+ @log_file.rewind
19
+ app_start, discard, app_end = @log_file.readlines
20
+ LoggerSpecs::strip_date(app_start).should == "INFO -- TestApp: Start of TestApp.\n"
21
+ LoggerSpecs::strip_date(app_end).should == "INFO -- TestApp: End of TestApp. (status: true)\n"
22
+ end
23
+
24
+ it "returns the status code" do
25
+ code = @app.start
26
+ @log_file.rewind
27
+ app_end = @log_file.readlines.last
28
+ /true/.should =~ LoggerSpecs::strip_date(app_end)
29
+ code.should == true
30
+ end
31
+
32
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::LogDevice#close" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+
8
+ # Avoid testing this with STDERR, we don't want to be closing that.
9
+ @device = Logger::LogDevice.new(@log_file)
10
+ end
11
+
12
+ after :each do
13
+ @log_file.close unless @log_file.closed?
14
+ rm_r @file_path
15
+ end
16
+
17
+ ruby_version_is "" ... "1.9" do
18
+ it "closes the LogDevice's stream" do
19
+ @device.close
20
+ lambda { @device.write("Test") }.should raise_error
21
+ end
22
+
23
+ it "raises an error if it's already closed" do
24
+ @device.close
25
+ lambda { @device.close }.should raise_error
26
+ end
27
+ end
28
+
29
+ ruby_version_is "1.9" do
30
+ it "closes the LogDevice's stream" do
31
+ @device.close
32
+ lambda { @device.write("Test") }.should complain(/\Alog writing failed\./)
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,46 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::LogDevice#new" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ end
8
+
9
+ after :each do
10
+ @log_file.close unless @log_file.closed?
11
+ rm_r @file_path
12
+ end
13
+
14
+ it "creates a new log device" do
15
+ l = Logger::LogDevice.new(@log_file)
16
+ l.dev.should be_kind_of(File)
17
+ end
18
+
19
+ it "receives an IO object to log there as first argument" do
20
+ @log_file.should be_kind_of(IO)
21
+ l = Logger::LogDevice.new(@log_file)
22
+ l.write("foo")
23
+ @log_file.rewind
24
+ @log_file.readlines.first.should == "foo"
25
+ end
26
+
27
+ it "creates a File if the IO object does not exist" do
28
+ path = tmp("test_logger_file")
29
+ l = Logger::LogDevice.new(path)
30
+ l.write("Test message")
31
+ l.close
32
+
33
+ File.exist?(path).should be_true
34
+ File.open(path) do |f|
35
+ f.readlines.should_not be_empty
36
+ end
37
+
38
+ rm_r path
39
+ end
40
+
41
+ it "receives options via a hash as second argument" do
42
+ lambda { Logger::LogDevice.new(STDERR,
43
+ { :shift_age => 8, :shift_size => 10
44
+ })}.should_not raise_error
45
+ end
46
+ end
@@ -0,0 +1,50 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger::LogDevice#write" do
4
+ before :each do
5
+ @file_path = tmp("test_log.log")
6
+ @log_file = File.open(@file_path, "w+")
7
+ # Avoid testing this with STDERR, we don't want to be closing that.
8
+ @device = Logger::LogDevice.new(@log_file)
9
+ end
10
+
11
+ after :each do
12
+ @log_file.close unless @log_file.closed?
13
+ rm_r @file_path
14
+ end
15
+
16
+ it "writes a message to the device" do
17
+ @device.write "This is a test message"
18
+ @log_file.rewind
19
+ @log_file.readlines.first.should == "This is a test message"
20
+ end
21
+
22
+ it "can create a file and writes empty message" do
23
+ path = tmp("you_should_not_see_me")
24
+ logdevice = Logger::LogDevice.new(path)
25
+ logdevice.write("")
26
+ logdevice.close
27
+
28
+ File.open(path) do |f|
29
+ messages = f.readlines
30
+ messages.size.should == 1
31
+ messages.first.should =~ /#.*/ # only a comment
32
+ end
33
+
34
+ rm_r path
35
+ end
36
+
37
+ ruby_version_is "" ... "1.9" do
38
+ it "fails if the device is already closed" do
39
+ @device.close
40
+ lambda { @device.write "foo" }.should raise_error
41
+ end
42
+ end
43
+
44
+ ruby_version_is "1.9" do
45
+ it "fails if the device is already closed" do
46
+ @device.close
47
+ lambda { @device.write "foo" }.should complain(/\Alog writing failed\./)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ require 'logger'
2
+
3
+ module LoggerSpecs
4
+
5
+ def self.strip_date(str)
6
+ str.gsub(/[A-Z].*\[.*\]/, "").lstrip
7
+ end
8
+ class TestApp < Logger::Application
9
+ def initialize(appname, log_file=nil)
10
+ super(appname)
11
+ self.set_log(log_file) if log_file
12
+ end
13
+
14
+ def run
15
+ log(WARN, "Test log message")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#add" do
4
+ before :each do
5
+ @path = tmp("test_log.log")
6
+ @log_file = File.open(@path, "w+")
7
+ @logger = Logger.new(@path)
8
+ end
9
+
10
+ after :each do
11
+ @logger.close
12
+ @log_file.close unless @log_file.closed?
13
+ rm_r @path
14
+ end
15
+
16
+ it "writes a new message to the logger" do
17
+ @logger.add(Logger::WARN, "Test")
18
+ @log_file.rewind
19
+ message = @log_file.readlines.last
20
+ LoggerSpecs::strip_date(message).should == "WARN -- : Test\n"
21
+ end
22
+
23
+ it "receives a severity" do
24
+ @logger.log(Logger::INFO, "Info message")
25
+ @logger.log(Logger::DEBUG, "Debug message")
26
+ @logger.log(Logger::WARN, "Warn message")
27
+ @logger.log(Logger::ERROR, "Error message")
28
+ @logger.log(Logger::FATAL, "Fatal message")
29
+
30
+ @log_file.rewind
31
+
32
+ info, debug, warn, error, fatal = @log_file.readlines
33
+
34
+ LoggerSpecs::strip_date(info).should == "INFO -- : Info message\n"
35
+ LoggerSpecs::strip_date(debug).should == "DEBUG -- : Debug message\n"
36
+ LoggerSpecs::strip_date(warn).should == "WARN -- : Warn message\n"
37
+ LoggerSpecs::strip_date(error).should == "ERROR -- : Error message\n"
38
+ LoggerSpecs::strip_date(fatal).should == "FATAL -- : Fatal message\n"
39
+ end
40
+
41
+ it "receives a message" do
42
+ @logger.log(nil, "test")
43
+ @log_file.rewind
44
+ LoggerSpecs::strip_date(@log_file.readline).should == "ANY -- : test\n"
45
+ end
46
+
47
+ it "receives a program name" do
48
+ @logger.log(nil, "test", "TestApp")
49
+ @log_file.rewind
50
+ LoggerSpecs::strip_date(@log_file.readline).should == "ANY -- TestApp: test\n"
51
+ end
52
+
53
+ it "receives a block" do
54
+ lambda {
55
+ @logger.log(nil, "test", "TestApp") do
56
+ 1+1
57
+ end
58
+ }.should_not raise_error
59
+ end
60
+
61
+ it "calls the block if message is nil" do
62
+ temp = 0
63
+ lambda {
64
+ @logger.log(nil, nil, "TestApp") do
65
+ temp = 1+1
66
+ end
67
+ }.should_not raise_error
68
+ temp.should == 2
69
+ end
70
+
71
+ it "ignores the block if the message is not nil" do
72
+ temp = 0
73
+ lambda {
74
+ @logger.log(nil, "not nil", "TestApp") do
75
+ temp = 1+1
76
+ end
77
+ }.should_not raise_error
78
+ temp.should == 0
79
+ end
80
+ end