rubysl-logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#close" 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
+ @log_file.close unless @log_file.closed?
12
+ rm_r @path
13
+ end
14
+
15
+ ruby_version_is "" ... "1.9" do
16
+ it "closes the logging device" do
17
+ @logger.close
18
+ lambda { @logger.add(nil, "Foo") }.should raise_error(IOError)
19
+ end
20
+
21
+ it "fails when called on a closed device" do
22
+ @logger.close
23
+ lambda { @logger.close }.should raise_error(IOError)
24
+ end
25
+ end
26
+
27
+ ruby_version_is "1.9" do
28
+ it "closes the logging device" do
29
+ @logger.close
30
+ lambda { @logger.add(nil, "Foo") }.should complain(/\Alog writing failed\./)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#datetime_format" 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 "returns the date format used for the logs" do
17
+ format = "%Y-%d"
18
+ @logger.datetime_format = format
19
+ @logger.datetime_format.should == format
20
+ end
21
+
22
+ it "returns nil logger is using the default date format" do
23
+ @logger.datetime_format.should == nil
24
+ end
25
+ end
26
+
27
+ describe "Logger#datetime_format=" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "sets the date format for the logs" do
41
+ format = "%Y"
42
+ @logger.datetime_format = "%Y"
43
+ @logger.datetime_format.should == "%Y"
44
+ @logger.add(Logger::WARN, "Test message")
45
+ @log_file.rewind
46
+
47
+ regex = /2[0-9]{3}.*Test message/
48
+ @log_file.readlines.first.should =~ regex
49
+ end
50
+
51
+ it "follows the Time#strftime format" do
52
+ lambda { @logger.datetime_format = "%Y-%m" }.should_not raise_error
53
+
54
+ regex = /\d{4}-\d{2}-\d{2}oo-\w+ar/
55
+ @logger.datetime_format = "%Foo-%Bar"
56
+ @logger.add(nil, "Test message")
57
+ @log_file.rewind
58
+ @log_file.readlines.first.should =~ regex
59
+ end
60
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#debug?" 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 "returns true if severity level allows debug messages" do
17
+ @logger.level = Logger::DEBUG
18
+ @logger.debug?.should == true
19
+ end
20
+
21
+ it "returns false if severity level does not allow debug messages" do
22
+ @logger.level = Logger::WARN
23
+ @logger.debug?.should == false
24
+ end
25
+ end
26
+
27
+ describe "Logger#debug" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "logs a DEBUG message" do
41
+ @logger.debug("test")
42
+ @log_file.rewind
43
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "DEBUG -- : test\n"
44
+ end
45
+
46
+ it "accepts an application name with a block" do
47
+ @logger.debug("MyApp") { "Test message" }
48
+ @log_file.rewind
49
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "DEBUG -- MyApp: Test message\n"
50
+ end
51
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#error?" 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 "returns true if severity level allows printing errors" do
17
+ @logger.level = Logger::INFO
18
+ @logger.error?.should == true
19
+ end
20
+
21
+ it "returns false if severity level does not allow errors" do
22
+ @logger.level = Logger::FATAL
23
+ @logger.error?.should == false
24
+ end
25
+ end
26
+
27
+ describe "Logger#error" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "logs a ERROR message" do
41
+ @logger.error("test")
42
+ @log_file.rewind
43
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "ERROR -- : test\n"
44
+ end
45
+
46
+ it "accepts an application name with a block" do
47
+ @logger.error("MyApp") { "Test message" }
48
+ @log_file.rewind
49
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "ERROR -- MyApp: Test message\n"
50
+ end
51
+
52
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#fatal?" 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 "returns true if severity level allows fatal messages" do
17
+ @logger.level = Logger::FATAL
18
+ @logger.fatal?.should == true
19
+ end
20
+
21
+ it "returns false if severity level does not allow fatal messages" do
22
+ @logger.level = Logger::UNKNOWN
23
+ @logger.fatal?.should == false
24
+ end
25
+ end
26
+
27
+ describe "Logger#fatal" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "logs a FATAL message" do
41
+ @logger.fatal("test")
42
+ @log_file.rewind
43
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "FATAL -- : test\n"
44
+ end
45
+
46
+ it "accepts an application name with a block" do
47
+ @logger.fatal("MyApp") { "Test message" }
48
+ @log_file.rewind
49
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "FATAL -- MyApp: Test message\n"
50
+ end
51
+
52
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#info?" 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 "returns true if severity level allows info messages" do
17
+ @logger.level = Logger::INFO
18
+ @logger.info?.should == true
19
+ end
20
+
21
+ it "returns false if severity level does not allow info messages" do
22
+ @logger.level = Logger::FATAL
23
+ @logger.info?.should == false
24
+ end
25
+ end
26
+
27
+ describe "Logger#info" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "logs a INFO message" do
41
+ @logger.info("test")
42
+ @log_file.rewind
43
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "INFO -- : test\n"
44
+ end
45
+
46
+ it "accepts an application name with a block" do
47
+ @logger.info("MyApp") { "Test message" }
48
+ @log_file.rewind
49
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "INFO -- MyApp: Test message\n"
50
+ end
51
+
52
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#new" do
4
+
5
+ before :each do
6
+ @file_path = tmp("test_log.log")
7
+ @log_file = File.open(@file_path, "w+")
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 "creates a new logger object" do
16
+ l = Logger.new(STDERR)
17
+ lambda { l.add(Logger::WARN, "Foo") }.should output_to_fd(/Foo/, STDERR)
18
+ end
19
+
20
+ it "receives a logging device as first argument" do
21
+ l = Logger.new(@log_file)
22
+ l.add(Logger::WARN, "Test message")
23
+
24
+ @log_file.rewind
25
+ LoggerSpecs::strip_date(@log_file.readline).should == "WARN -- : Test message\n"
26
+ l.close
27
+ end
28
+
29
+ it "receives a frequency rotation as second argument" do
30
+ lambda { Logger.new(@log_file, "daily") }.should_not raise_error
31
+ lambda { Logger.new(@log_file, "weekly") }.should_not raise_error
32
+ lambda { Logger.new(@log_file, "monthly") }.should_not raise_error
33
+ end
34
+
35
+ it "also receives a number of log files to keep as second argument" do
36
+ lambda { Logger.new(@log_file, 1).close }.should_not raise_error
37
+ end
38
+
39
+ it "receivs a maximum logfile size as third argument" do
40
+ # This should create 2 small log files, logfile_test and logfile_test.0
41
+ # in /tmp, each one with a different message.
42
+ path = tmp("logfile_test.log")
43
+
44
+ l = Logger.new(path, 2, 5)
45
+ l.add Logger::WARN, "foo"
46
+ l.add Logger::WARN, "bar"
47
+
48
+ File.exists?(path).should be_true
49
+ File.exists?(path + ".0").should be_true
50
+
51
+ # first line will be a comment so we'll have to skip it.
52
+ f = File.open(path)
53
+ f1 = File.open("#{path}.0")
54
+ LoggerSpecs::strip_date(f1.readlines.last).should == "WARN -- : foo\n"
55
+ LoggerSpecs::strip_date(f.readlines.last).should == "WARN -- : bar\n"
56
+
57
+ l.close
58
+ f.close
59
+ f1.close
60
+ rm_r path, "#{path}.0"
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#unknown" 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 "logs a message with unknown severity" do
17
+ @logger.unknown "Test"
18
+ @log_file.rewind
19
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "ANY -- : Test\n"
20
+ end
21
+
22
+ it "defaults the priority value to 5 and text value to ANY" do
23
+ @logger.unknown "Test"
24
+ @log_file.rewind
25
+ message = LoggerSpecs::strip_date(@log_file.readlines.first)[0..2]
26
+ message.should == "ANY"
27
+ Logger::UNKNOWN.should == 5
28
+ end
29
+
30
+ it "receives empty messages" do
31
+ lambda { @logger.unknown("") }.should_not raise_error
32
+ @log_file.rewind
33
+ message = LoggerSpecs::strip_date(@log_file.readlines.first).should == "ANY -- : \n"
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../../fixtures/common', __FILE__)
2
+
3
+ describe "Logger#warn?" 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 "returns true if severity level allows printing warn messages" do
17
+ @logger.level = Logger::WARN
18
+ @logger.warn?.should == true
19
+ end
20
+
21
+ it "returns false if severity level does not allow printing warn messages" do
22
+ @logger.level = Logger::FATAL
23
+ @logger.warn?.should == false
24
+ end
25
+ end
26
+
27
+ describe "Logger#warn" do
28
+ before :each do
29
+ @path = tmp("test_log.log")
30
+ @log_file = File.open(@path, "w+")
31
+ @logger = Logger.new(@path)
32
+ end
33
+
34
+ after :each do
35
+ @logger.close
36
+ @log_file.close unless @log_file.closed?
37
+ rm_r @path
38
+ end
39
+
40
+ it "logs a WARN message" do
41
+ @logger.warn("test")
42
+ @log_file.rewind
43
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "WARN -- : test\n"
44
+ end
45
+
46
+ it "accepts an application name with a block" do
47
+ @logger.warn("MyApp") { "Test message" }
48
+ @log_file.rewind
49
+ LoggerSpecs::strip_date(@log_file.readlines.first).should == "WARN -- MyApp: Test message\n"
50
+ end
51
+
52
+ end