logging 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/logging.yaml +63 -0
- data/lib/logging.rb +23 -2
- data/lib/logging/appender.rb +9 -7
- data/lib/logging/appenders/console.rb +13 -9
- data/lib/logging/appenders/file.rb +25 -23
- data/lib/logging/appenders/io.rb +4 -8
- data/lib/logging/appenders/rolling_file.rb +189 -0
- data/lib/logging/config/yaml_configurator.rb +190 -0
- data/lib/logging/layout.rb +10 -8
- data/lib/logging/layouts/pattern.rb +7 -9
- data/lib/logging/logger.rb +37 -7
- data/test/appenders/test_console.rb +7 -7
- data/test/appenders/test_file.rb +20 -10
- data/test/appenders/test_rolling_file.rb +141 -0
- data/test/config/test_yaml_configurator.rb +47 -0
- data/test/layouts/test_pattern.rb +2 -2
- data/test/setup.rb +3 -1
- data/test/test_layout.rb +7 -7
- data/test/test_log_event.rb +3 -3
- data/test/test_logger.rb +37 -1
- data/test/test_logging.rb +82 -2
- data/test/test_root_logger.rb +17 -1
- metadata +7 -2
@@ -0,0 +1,141 @@
|
|
1
|
+
# $Id: test_rolling_file.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
|
+
|
3
|
+
require 'test/setup.rb'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
module TestLogging
|
7
|
+
module TestAppenders
|
8
|
+
|
9
|
+
class TestRollingFile < Test::Unit::TestCase
|
10
|
+
include LoggingTestCase
|
11
|
+
|
12
|
+
TMP = 'tmp'
|
13
|
+
|
14
|
+
def setup
|
15
|
+
super
|
16
|
+
FileUtils.rm_rf TMP if File.exist?(TMP)
|
17
|
+
FileUtils.mkdir(TMP)
|
18
|
+
@fn = File.join(TMP, 'test.log')
|
19
|
+
@fn_fmt = File.join(TMP, 'test.%d.log')
|
20
|
+
@glob = File.join(TMP, '*.log')
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
FileUtils.rm_rf TMP
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_initialize
|
28
|
+
assert_equal [], Dir.glob(@glob)
|
29
|
+
|
30
|
+
# create a new appender
|
31
|
+
ap = ::Logging::Appenders::RollingFile.new('test', :filename => @fn)
|
32
|
+
assert File.exist?(@fn)
|
33
|
+
assert_equal 0, File.size(@fn)
|
34
|
+
|
35
|
+
ap << "Just a line of text\n" # 20 bytes
|
36
|
+
ap.flush
|
37
|
+
assert_equal 20, File.size(@fn)
|
38
|
+
ap.close
|
39
|
+
|
40
|
+
# make sure we append to the current file (not truncate)
|
41
|
+
ap = ::Logging::Appenders::RollingFile.new('test', :filename => @fn)
|
42
|
+
assert_equal [@fn], Dir.glob(@glob)
|
43
|
+
assert_equal 20, File.size(@fn)
|
44
|
+
|
45
|
+
ap << "Just another line of text\n" # 26 bytes
|
46
|
+
ap.flush
|
47
|
+
assert_equal 46, File.size(@fn)
|
48
|
+
ap.close
|
49
|
+
|
50
|
+
# setting the truncate option to true should roll the current log file
|
51
|
+
# and create a new one
|
52
|
+
ap = ::Logging::Appenders::RollingFile.new('test',
|
53
|
+
:filename => @fn, :truncate => true)
|
54
|
+
|
55
|
+
log1 = sprintf(@fn_fmt, 1)
|
56
|
+
assert_equal [log1, @fn], Dir.glob(@glob).sort
|
57
|
+
assert_equal 0, File.size(@fn)
|
58
|
+
assert_equal 46, File.size(log1)
|
59
|
+
|
60
|
+
ap << "Some more text in the new file\n" # 31 bytes
|
61
|
+
ap.flush
|
62
|
+
assert_equal 31, File.size(@fn)
|
63
|
+
ap.close
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_keep
|
67
|
+
assert_equal [], Dir.glob(@glob)
|
68
|
+
|
69
|
+
(1..12).each do |cnt|
|
70
|
+
name = sprintf(@fn_fmt, cnt)
|
71
|
+
File.open(name,'w') {|fd| fd.write 'X'*cnt}
|
72
|
+
end
|
73
|
+
FileUtils.touch(@fn)
|
74
|
+
|
75
|
+
# keep only five files
|
76
|
+
ap = ::Logging::Appenders::RollingFile.new('test',
|
77
|
+
:filename => @fn, :keep => 5)
|
78
|
+
|
79
|
+
# we still have 13 files because we did not truncate the log file,
|
80
|
+
# and hence, we did not roll all the log files
|
81
|
+
assert_equal 13, Dir.glob(@glob).length
|
82
|
+
|
83
|
+
# force the appender to roll the files
|
84
|
+
ap.send :roll
|
85
|
+
assert_equal 6, Dir.glob(@glob).length
|
86
|
+
|
87
|
+
(1..5).each do |cnt|
|
88
|
+
name = sprintf(@fn_fmt, cnt)
|
89
|
+
assert_equal cnt-1, File.size(name)
|
90
|
+
end
|
91
|
+
ap.close
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_max_age
|
95
|
+
assert_equal [], Dir.glob(@glob)
|
96
|
+
|
97
|
+
ap = ::Logging::Appenders::RollingFile.new('test',
|
98
|
+
:filename => @fn, :max_age => 1)
|
99
|
+
|
100
|
+
ap << "random message\n"
|
101
|
+
assert_equal 1, Dir.glob(@glob).length
|
102
|
+
|
103
|
+
sleep 1
|
104
|
+
ap << "another random message\n"
|
105
|
+
assert_equal 2, Dir.glob(@glob).length
|
106
|
+
|
107
|
+
ap.close
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_max_size
|
111
|
+
assert_equal [], Dir.glob(@glob)
|
112
|
+
|
113
|
+
ap = ::Logging::Appenders::RollingFile.new('test',
|
114
|
+
:filename => @fn, :max_size => 100)
|
115
|
+
|
116
|
+
ap << 'X' * 100; ap.flush
|
117
|
+
assert_equal 1, Dir.glob(@glob).length
|
118
|
+
assert_equal 100, File.size(@fn)
|
119
|
+
|
120
|
+
# this character is appended to the log file (bringing its size to 101)
|
121
|
+
# and THEN the file is rolled resulting in a new, empty log file
|
122
|
+
ap << 'X'
|
123
|
+
assert_equal 2, Dir.glob(@glob).length
|
124
|
+
assert_equal 0, File.size(@fn)
|
125
|
+
|
126
|
+
ap << 'X' * 100; ap.flush
|
127
|
+
assert_equal 2, Dir.glob(@glob).length
|
128
|
+
assert_equal 100, File.size(@fn)
|
129
|
+
|
130
|
+
ap << 'X'
|
131
|
+
assert_equal 3, Dir.glob(@glob).length
|
132
|
+
assert_equal 0, File.size(@fn)
|
133
|
+
|
134
|
+
ap.close
|
135
|
+
end
|
136
|
+
|
137
|
+
end # class TestRollingFile
|
138
|
+
end # module TestAppenders
|
139
|
+
end # module TestLogging
|
140
|
+
|
141
|
+
# EOF
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# $Id: test_yaml_configurator.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
require 'yaml'
|
5
|
+
require 'test/setup.rb'
|
6
|
+
|
7
|
+
module TestLogging
|
8
|
+
module TestConfig
|
9
|
+
|
10
|
+
class TestYamlConfigurator < Test::Unit::TestCase
|
11
|
+
include LoggingTestCase
|
12
|
+
|
13
|
+
def setup
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_class_load
|
18
|
+
assert_raise(::Logging::Config::YamlConfigurator::Error) {
|
19
|
+
::Logging::Config::YamlConfigurator.load(Object.new)
|
20
|
+
}
|
21
|
+
|
22
|
+
begin
|
23
|
+
fd = File.open('examples/logging.yaml','r')
|
24
|
+
assert_nothing_raised {
|
25
|
+
::Logging::Config::YamlConfigurator.load(fd)
|
26
|
+
}
|
27
|
+
ensure
|
28
|
+
fd.close
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_initialize
|
33
|
+
io = StringIO.new
|
34
|
+
io << YAML.dump({:one => 1, :two => 2, :three => 3})
|
35
|
+
io.seek 0
|
36
|
+
|
37
|
+
assert_raise(::Logging::Config::YamlConfigurator::Error) {
|
38
|
+
::Logging::Config::YamlConfigurator.new(io)
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
end # class TestYamlConfigurator
|
43
|
+
|
44
|
+
end # module TestConfig
|
45
|
+
end # module TestLogging
|
46
|
+
|
47
|
+
# EOF
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: test_pattern.rb
|
1
|
+
# $Id: test_pattern.rb 17 2007-01-20 18:47:43Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/setup.rb'
|
4
4
|
|
@@ -138,7 +138,7 @@ module TestLayouts
|
|
138
138
|
assert_match %r/\A\d+\z/, @layout.format(event)
|
139
139
|
|
140
140
|
@layout.pattern = '%t'
|
141
|
-
assert_match %r/\A
|
141
|
+
assert_match %r/\A-?\d+\z/, @layout.format(event)
|
142
142
|
|
143
143
|
@layout.pattern = '%%'
|
144
144
|
assert_equal '%', @layout.format(event)
|
data/test/setup.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: setup.rb
|
1
|
+
# $Id: setup.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/unit'
|
4
4
|
|
@@ -37,6 +37,8 @@ module TestLogging
|
|
37
37
|
define_method(:instance, nonce::FirstInstanceCall)
|
38
38
|
end
|
39
39
|
end
|
40
|
+
|
41
|
+
::Logging::Appender.instance_variable_get(:@appenders).clear
|
40
42
|
end
|
41
43
|
|
42
44
|
end # module LoggingTestCase
|
data/test/test_layout.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: test_layout.rb
|
1
|
+
# $Id: test_layout.rb 17 2007-01-20 18:47:43Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/setup.rb'
|
4
4
|
|
@@ -21,13 +21,13 @@ module TestLogging
|
|
21
21
|
|
22
22
|
assert_equal :string, obj_format[@layout]
|
23
23
|
|
24
|
-
@layout = ::Logging::Layout.new 'blah'
|
24
|
+
@layout = ::Logging::Layout.new 'format_as' => 'blah'
|
25
25
|
assert_equal :string, obj_format[@layout]
|
26
26
|
|
27
|
-
@layout = ::Logging::Layout.new :inspect
|
27
|
+
@layout = ::Logging::Layout.new :format_as => :inspect
|
28
28
|
assert_equal :inspect, obj_format[@layout]
|
29
29
|
|
30
|
-
@layout = ::Logging::Layout.new :yaml
|
30
|
+
@layout = ::Logging::Layout.new 'format_as' => :yaml
|
31
31
|
assert_equal :yaml, obj_format[@layout]
|
32
32
|
|
33
33
|
@layout = ::Logging::Layout.new
|
@@ -70,16 +70,16 @@ module TestLogging
|
|
70
70
|
assert_equal '<Array> 1234', r
|
71
71
|
|
72
72
|
obj = %w( one two three four )
|
73
|
-
@layout = ::Logging::Layout.new :inspect
|
73
|
+
@layout = ::Logging::Layout.new :format_as => :inspect
|
74
74
|
r = @layout.send :format_obj, obj
|
75
75
|
assert_equal '<Array> ["one", "two", "three", "four"]', r
|
76
76
|
|
77
|
-
@layout = ::Logging::Layout.new :yaml
|
77
|
+
@layout = ::Logging::Layout.new :format_as => :yaml
|
78
78
|
r = @layout.send :format_obj, obj
|
79
79
|
assert_equal "<Array> \n--- \n- one\n- two\n- three\n- four\n", r
|
80
80
|
end
|
81
81
|
|
82
|
-
end # class
|
82
|
+
end # class TestLayout
|
83
83
|
end # module TestLogging
|
84
84
|
|
85
85
|
# EOF
|
data/test/test_log_event.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# $Id: test_log_event.rb
|
1
|
+
# $Id: test_log_event.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/setup.rb'
|
4
4
|
|
5
5
|
module TestLogging
|
6
6
|
|
7
|
-
class
|
7
|
+
class TestLogEvent < Test::Unit::TestCase
|
8
8
|
include LoggingTestCase
|
9
9
|
|
10
10
|
def setup
|
@@ -69,7 +69,7 @@ module TestLogging
|
|
69
69
|
assert_equal 'test_method', @appender.event.method
|
70
70
|
end
|
71
71
|
|
72
|
-
end # class
|
72
|
+
end # class TestLogEvent
|
73
73
|
|
74
74
|
class EventAppender < ::Logging::Appender
|
75
75
|
attr_reader :event
|
data/test/test_logger.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: test_logger.rb
|
1
|
+
# $Id: test_logger.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/setup.rb'
|
4
4
|
require 'stringio'
|
@@ -69,6 +69,20 @@ module TestLogging
|
|
69
69
|
|
70
70
|
log.additive = false
|
71
71
|
assert_equal false, log.additive
|
72
|
+
|
73
|
+
log.additive = true
|
74
|
+
assert_equal true, log.additive
|
75
|
+
|
76
|
+
log.additive = 'false'
|
77
|
+
assert_equal false, log.additive
|
78
|
+
|
79
|
+
log.additive = 'true'
|
80
|
+
assert_equal true, log.additive
|
81
|
+
|
82
|
+
log.additive = nil
|
83
|
+
assert_equal true, log.additive
|
84
|
+
|
85
|
+
assert_raise(ArgumentError) {log.additive = Object}
|
72
86
|
end
|
73
87
|
|
74
88
|
def test_appenders_eq
|
@@ -444,6 +458,28 @@ module TestLogging
|
|
444
458
|
assert_equal true, log.trace
|
445
459
|
end
|
446
460
|
|
461
|
+
def test_trace_eq
|
462
|
+
log = ::Logging::Logger.new 'A'
|
463
|
+
assert_equal false, log.trace
|
464
|
+
|
465
|
+
log.trace = true
|
466
|
+
assert_equal true, log.trace
|
467
|
+
|
468
|
+
log.trace = false
|
469
|
+
assert_equal false, log.trace
|
470
|
+
|
471
|
+
log.trace = 'true'
|
472
|
+
assert_equal true, log.trace
|
473
|
+
|
474
|
+
log.trace = 'false'
|
475
|
+
assert_equal false, log.trace
|
476
|
+
|
477
|
+
log.trace = nil
|
478
|
+
assert_equal false, log.trace
|
479
|
+
|
480
|
+
assert_raise(ArgumentError) {log.trace = Object}
|
481
|
+
end
|
482
|
+
|
447
483
|
end # class TestLogger
|
448
484
|
|
449
485
|
class SioAppender < ::Logging::Appenders::IO
|
data/test/test_logging.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: test_logging.rb
|
1
|
+
# $Id: test_logging.rb 22 2007-01-29 16:20:54Z tim_pease $
|
2
2
|
|
3
3
|
require 'test/setup.rb'
|
4
4
|
|
@@ -13,6 +13,71 @@ module TestLogging
|
|
13
13
|
@lnames = ::Logging::LNAMES
|
14
14
|
end
|
15
15
|
|
16
|
+
def test_configure
|
17
|
+
assert_raise(ArgumentError) {::Logging.configure 'blah.txt'}
|
18
|
+
|
19
|
+
::Logging.configure 'examples/logging.yaml'
|
20
|
+
|
21
|
+
names = {
|
22
|
+
0 => 'DEB', 1 => 'INF', 2 => 'PRT',
|
23
|
+
3 => 'WRN', 4 => 'ERR', 5 => 'FAT'
|
24
|
+
}
|
25
|
+
assert_equal names, ::Logging::LNAMES
|
26
|
+
assert_equal :inspect, ::Logging::OBJ_FORMAT
|
27
|
+
assert_equal 3, ::Logging::Logger.root.level
|
28
|
+
|
29
|
+
# verify the appenders
|
30
|
+
h = ::Logging::Appender.instance_variable_get :@appenders
|
31
|
+
assert_equal ['logfile', 'stderr'], h.keys.sort
|
32
|
+
|
33
|
+
# start with the File appender
|
34
|
+
logfile = ::Logging::Appender['logfile']
|
35
|
+
assert_instance_of ::Logging::Appenders::File, logfile
|
36
|
+
assert_equal 0, logfile.level
|
37
|
+
assert_equal 'temp.log', logfile.instance_variable_get(:@fn)
|
38
|
+
|
39
|
+
layout = logfile.layout
|
40
|
+
assert_instance_of ::Logging::Layouts::Pattern, layout
|
41
|
+
assert_equal '[%d] %l %c : %m\\n', layout.pattern
|
42
|
+
assert_equal 'to_s', layout.date_method
|
43
|
+
assert_nil layout.date_pattern
|
44
|
+
|
45
|
+
# and now the Stderr appender
|
46
|
+
stderr = ::Logging::Appender['stderr']
|
47
|
+
assert_instance_of ::Logging::Appenders::Stderr, stderr
|
48
|
+
assert_equal 0, stderr.level
|
49
|
+
|
50
|
+
layout = stderr.layout
|
51
|
+
assert_instance_of ::Logging::Layouts::Basic, layout
|
52
|
+
|
53
|
+
# verify the loggers
|
54
|
+
h = ::Logging::Repository.instance.instance_variable_get :@h
|
55
|
+
assert_equal 3, h.length
|
56
|
+
|
57
|
+
# mylogger
|
58
|
+
mylogger = ::Logging::Logger['mylogger']
|
59
|
+
assert_equal 0, mylogger.level
|
60
|
+
assert_equal false, mylogger.additive
|
61
|
+
assert_equal false, mylogger.trace
|
62
|
+
|
63
|
+
appenders = mylogger.instance_variable_get :@appenders
|
64
|
+
assert_equal 2, appenders.length
|
65
|
+
assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort
|
66
|
+
|
67
|
+
# yourlogger
|
68
|
+
yourlogger = ::Logging::Logger['yourlogger']
|
69
|
+
assert_equal 1, yourlogger.level
|
70
|
+
assert_equal true, yourlogger.additive
|
71
|
+
assert_equal false, yourlogger.trace
|
72
|
+
|
73
|
+
appenders = yourlogger.instance_variable_get :@appenders
|
74
|
+
assert_equal 2, appenders.length
|
75
|
+
assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort
|
76
|
+
|
77
|
+
# cleanup
|
78
|
+
File.delete('temp.log') if File.exist?('temp.log')
|
79
|
+
end
|
80
|
+
|
16
81
|
def test_define_levels_default
|
17
82
|
empty = {}
|
18
83
|
assert_equal empty, @levels
|
@@ -91,7 +156,7 @@ module TestLogging
|
|
91
156
|
def test_format_as
|
92
157
|
assert_equal false, ::Logging.const_defined?('OBJ_FORMAT')
|
93
158
|
|
94
|
-
assert_raises(ArgumentError) {::Logging.format_as '
|
159
|
+
assert_raises(ArgumentError) {::Logging.format_as 'bob'}
|
95
160
|
assert_raises(ArgumentError) {::Logging.format_as String}
|
96
161
|
assert_raises(ArgumentError) {::Logging.format_as :what?}
|
97
162
|
|
@@ -113,6 +178,21 @@ module TestLogging
|
|
113
178
|
assert ::Logging.const_defined?('OBJ_FORMAT')
|
114
179
|
assert_equal :yaml, ::Logging::OBJ_FORMAT
|
115
180
|
remove_const[:OBJ_FORMAT]
|
181
|
+
|
182
|
+
::Logging.format_as 'string'
|
183
|
+
assert ::Logging.const_defined?('OBJ_FORMAT')
|
184
|
+
assert_equal :string, ::Logging::OBJ_FORMAT
|
185
|
+
remove_const[:OBJ_FORMAT]
|
186
|
+
|
187
|
+
::Logging.format_as 'inspect'
|
188
|
+
assert ::Logging.const_defined?('OBJ_FORMAT')
|
189
|
+
assert_equal :inspect, ::Logging::OBJ_FORMAT
|
190
|
+
remove_const[:OBJ_FORMAT]
|
191
|
+
|
192
|
+
::Logging.format_as 'yaml'
|
193
|
+
assert ::Logging.const_defined?('OBJ_FORMAT')
|
194
|
+
assert_equal :yaml, ::Logging::OBJ_FORMAT
|
195
|
+
remove_const[:OBJ_FORMAT]
|
116
196
|
end
|
117
197
|
|
118
198
|
end # class TestLogging
|