logging 0.1.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,69 @@
1
+ # $Id: root_logger.rb 13 2007-01-15 17:19:37Z tim_pease $
2
+
3
+ require 'logging'
4
+ require 'logging/logger'
5
+
6
+ module Logging
7
+
8
+ #
9
+ # The root logger exists to ensure that all loggers have a parent and a
10
+ # defined logging level. If a logger is additive, eventually its log
11
+ # events will propogate up to the root logger.
12
+ #
13
+ class RootLogger < Logger
14
+
15
+ # undefine the methods that the root logger does not need
16
+ %w(additive additive= parent parent=).each do |m|
17
+ undef_method m.intern
18
+ end
19
+
20
+ #
21
+ # call-seq:
22
+ # RootLogger.new
23
+ #
24
+ # Returns a new root logger instance. This method will be called only
25
+ # once when the +Repository+ singleton instance is created.
26
+ #
27
+ def initialize( )
28
+ unless ::Logging.const_defined? 'MAX_LEVEL_LENGTH'
29
+ ::Logging.define_levels %w(debug info warn error fatal)
30
+ end
31
+
32
+ @name = 'root'
33
+ @appenders = []
34
+ @additive = false
35
+ @trace = false
36
+ self.level = 0
37
+ end
38
+
39
+ #
40
+ # call-seq:
41
+ # log <=> other
42
+ #
43
+ # Compares this logger by name to another logger. The normal return codes
44
+ # for +String+ objects apply.
45
+ #
46
+ def <=>( other )
47
+ case other
48
+ when self: 0
49
+ when ::Logging::Logger: -1
50
+ else raise ArgumentError, 'expecting a Logger instance' end
51
+ end
52
+
53
+ #
54
+ # call-seq:
55
+ # level = :all
56
+ #
57
+ # Set the level for the root logger. The functionality of this method is
58
+ # the same as +Logger#level=+, but setting the level to +nil+ for the
59
+ # root logger is not allowed. The level is silently set to :all.
60
+ #
61
+ def level=( level )
62
+ level ||= 0
63
+ super
64
+ end
65
+
66
+ end # class RootLogger
67
+ end # module Logging
68
+
69
+ # EOF
@@ -0,0 +1,40 @@
1
+ # $Id: test_console.rb 2 2007-01-09 18:10:50Z tim_pease $
2
+
3
+ require 'test/setup.rb'
4
+
5
+ module TestLogging
6
+ module TestAppenders
7
+
8
+ class TestStdOut < Test::Unit::TestCase
9
+ include LoggingTestCase
10
+
11
+ def test_initialize
12
+ appender = ::Logging::Appenders::StdOut.new
13
+ assert_equal 'stdout', appender.name
14
+ assert_same STDOUT, appender.instance_variable_get(:@io)
15
+
16
+ appender.close
17
+ assert_equal true, appender.closed?
18
+ assert_equal false, STDOUT.closed?
19
+ end
20
+
21
+ end # class TestStdOut
22
+
23
+ class TestStdErr < Test::Unit::TestCase
24
+
25
+ def test_initialize
26
+ appender = ::Logging::Appenders::StdErr.new
27
+ assert_equal 'stderr', appender.name
28
+ assert_same STDERR, appender.instance_variable_get(:@io)
29
+
30
+ appender.close
31
+ assert_equal true, appender.closed?
32
+ assert_equal false, STDERR.closed?
33
+ end
34
+
35
+ end # class TestStdErr
36
+
37
+ end # module TestAppenders
38
+ end # module TestLogging
39
+
40
+ # EOF
@@ -0,0 +1,76 @@
1
+ # $Id: test_file.rb 2 2007-01-09 18:10:50Z tim_pease $
2
+
3
+ require 'test/setup.rb'
4
+ require 'fileutils'
5
+
6
+ module TestLogging
7
+ module TestAppenders
8
+
9
+ class TestFile < 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
+ FileUtils.mkdir [File.join(TMP, 'dir'), File.join(TMP, 'uw_dir')]
19
+ FileUtils.chmod 0555, File.join(TMP, 'uw_dir')
20
+ FileUtils.touch File.join(TMP, 'uw_file')
21
+ FileUtils.chmod 0444, File.join(TMP, 'uw_file')
22
+ end
23
+
24
+ def teardown
25
+ FileUtils.rm_rf TMP
26
+ end
27
+
28
+ def test_initialize
29
+ log = File.join(TMP, 'uw_dir', 'file.log')
30
+ assert_raise(StandardError) {::Logging::Appenders::File.new log}
31
+
32
+ log = File.join(TMP, 'dir')
33
+ assert_raise(StandardError) {::Logging::Appenders::File.new log}
34
+
35
+ log = File.join(TMP, 'uw_file')
36
+ assert_raise(StandardError) {::Logging::Appenders::File.new log}
37
+
38
+ log = File.join(TMP, 'file.log')
39
+ appender = ::Logging::Appenders::File.new log
40
+ assert_equal log, appender.name
41
+ appender << "This will be the first line\n"
42
+ appender << "This will be the second line\n"
43
+ File.open(log, 'r') do |file|
44
+ assert_equal "This will be the first line\n", file.readline
45
+ assert_equal "This will be the second line\n", file.readline
46
+ assert_raise(EOFError) {file.readline}
47
+ end
48
+ appender.close
49
+
50
+ appender = ::Logging::Appenders::File.new log
51
+ assert_equal log, appender.name
52
+ appender << "This will be the third line\n"
53
+ File.open(log, 'r') do |file|
54
+ assert_equal "This will be the first line\n", file.readline
55
+ assert_equal "This will be the second line\n", file.readline
56
+ assert_equal "This will be the third line\n", file.readline
57
+ assert_raise(EOFError) {file.readline}
58
+ end
59
+ appender.close
60
+
61
+ appender = ::Logging::Appenders::File.new log, :truncate => true
62
+ assert_equal log, appender.name
63
+ appender << "The file was truncated\n"
64
+ File.open(log, 'r') do |file|
65
+ assert_equal "The file was truncated\n", file.readline
66
+ assert_raise(EOFError) {file.readline}
67
+ end
68
+ appender.close
69
+ end
70
+
71
+ end # class TestFile
72
+
73
+ end # module TestAppenders
74
+ end # module TestLogging
75
+
76
+ # EOF
@@ -0,0 +1,113 @@
1
+ # $Id: test_io.rb 15 2007-01-15 19:03:45Z tim_pease $
2
+
3
+ require 'test/setup.rb'
4
+ require 'stringio'
5
+
6
+ module TestLogging
7
+ module TestAppenders
8
+
9
+ class TestIO < Test::Unit::TestCase
10
+ include LoggingTestCase
11
+
12
+ def setup
13
+ super
14
+ ::Logging.define_levels %w(debug info warn error fatal)
15
+ @levels = ::Logging::LEVELS
16
+
17
+ @sio = StringIO.new
18
+ @appender = ::Logging::Appenders::IO.new 'test_appender', @sio
19
+ begin readline rescue EOFError end
20
+ end
21
+
22
+ def test_append
23
+ event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
24
+ [[1, 2, 3, 4]], false)
25
+ @appender.append event
26
+ assert_equal " WARN TestLogger : <Array> 1234\n", readline
27
+ assert_raise(EOFError) {readline}
28
+
29
+ event.level = @levels['debug']
30
+ event.data = ['the big log message', [1, 2, 3, 4]]
31
+ @appender.append event
32
+ assert_equal "DEBUG TestLogger : the big log message\n", readline
33
+ assert_equal "DEBUG TestLogger : <Array> 1234\n", readline
34
+ assert_raise(EOFError) {readline}
35
+
36
+ @appender.close
37
+ assert_raise(RuntimeError) {@appender.append event}
38
+ end
39
+
40
+ def test_append_error
41
+ @sio.close
42
+ event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
43
+ [[1, 2, 3, 4]], false)
44
+ assert_raise(IOError) {@appender.append event}
45
+ assert_equal true, @appender.closed?
46
+ end
47
+
48
+ def test_close
49
+ assert_equal false, @sio.closed?
50
+ assert_equal false, @appender.closed?
51
+
52
+ @appender.close
53
+ assert_equal true, @sio.closed?
54
+ assert_equal true, @appender.closed?
55
+
56
+ [STDIN, STDERR, STDOUT].each do |io|
57
+ @appender = ::Logging::Appenders::IO.new 'test', io
58
+ @appender.close
59
+ assert_equal false, io.closed?
60
+ assert_equal true, @appender.closed?
61
+ end
62
+ end
63
+
64
+ def test_concat
65
+ @appender << "this is a test message\n"
66
+ assert_equal "this is a test message\n", readline
67
+ assert_raise(EOFError) {readline}
68
+
69
+ @appender << "this is another message\n"
70
+ @appender << "some other line\n"
71
+ assert_equal "this is another message\n", readline
72
+ assert_equal "some other line\n", readline
73
+ assert_raise(EOFError) {readline}
74
+
75
+ @appender.close
76
+ assert_raise(RuntimeError) {@appender << 'message'}
77
+ end
78
+
79
+ def test_concat_error
80
+ @sio.close
81
+ assert_raise(IOError) {@appender << 'oopsy'}
82
+ assert_equal true, @appender.closed?
83
+ end
84
+
85
+ def test_flush
86
+ ary = []
87
+ @sio.instance_variable_set :@ary, ary
88
+ def @sio.flush() @ary << :flush end
89
+
90
+ @appender.flush
91
+ assert_equal :flush, ary.pop
92
+ end
93
+
94
+ def test_initialize
95
+ assert_raise(EOFError) {@sio.readline}
96
+ assert_raise(TypeError) {::Logging::Appenders::IO.new 'test', []}
97
+ end
98
+
99
+ private
100
+ def readline
101
+ @pos ||= 0
102
+ @sio.seek @pos
103
+ line = @sio.readline
104
+ @pos = @sio.tell
105
+ line
106
+ end
107
+
108
+ end # class TestIO
109
+
110
+ end # module TestAppenders
111
+ end # module TestLogging
112
+
113
+ # EOF
data/test/benchmark.rb ADDED
@@ -0,0 +1,80 @@
1
+ # $Id: benchmark.rb 2 2007-01-09 18:10:50Z tim_pease $
2
+
3
+ begin
4
+ require 'logging'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'logging'
8
+ end
9
+
10
+ begin
11
+ require 'rubygems'
12
+ require 'log4r'
13
+ $log4r = true
14
+ rescue LoadError
15
+ $log4r = false
16
+ end
17
+
18
+ require 'benchmark'
19
+ require 'stringio'
20
+ require 'logger'
21
+
22
+ module Logging
23
+ class Benchmark
24
+
25
+ def run
26
+ this_many = 100_000
27
+
28
+ sio = StringIO.new
29
+
30
+ ::Logging::Logger[:root].level = 'warn'
31
+ logging = ::Logging::Logger[self]
32
+ logging.add ::Logging::Appenders::IO.new('benchmark', sio)
33
+
34
+ logger = ::Logger.new sio
35
+ logger.level = ::Logger::WARN
36
+
37
+ log4r = if $log4r
38
+ x = ::Log4r::Logger.new('benchmark')
39
+ x.level = ::Log4r::WARN
40
+ x.add ::Log4r::IOOutputter.new('benchmark', sio)
41
+ x
42
+ end
43
+
44
+ puts "== Debug (not logged) ==\n"
45
+ ::Benchmark.bm(10) do |bm|
46
+ bm.report('Logging:') {this_many.times {logging.debug 'not logged'}}
47
+ bm.report('Logger:') {this_many.times {logger.debug 'not logged'}}
48
+ bm.report('Log4r:') {this_many.times {log4r.debug 'not logged'}} if log4r
49
+ end
50
+
51
+ puts "\n== Warn (logged) ==\n"
52
+ ::Benchmark.bm(10) do |bm|
53
+ sio.seek 0
54
+ bm.report('Logging:') {this_many.times {logging.warn 'logged'}}
55
+ sio.seek 0
56
+ bm.report('Logger:') {this_many.times {logger.warn 'logged'}}
57
+ sio.seek 0
58
+ bm.report('Log4r:') {this_many.times {log4r.warn 'logged'}} if log4r
59
+ end
60
+
61
+ puts "\n== Concat ==\n"
62
+ ::Benchmark.bm(10) do |bm|
63
+ sio.seek 0
64
+ bm.report('Logging:') {this_many.times {logging << 'logged'}}
65
+ sio.seek 0
66
+ bm.report('Logger:') {this_many.times {logger << 'logged'}}
67
+ puts "Log4r: not supported" if log4r
68
+ end
69
+ end
70
+
71
+ end # class Benchmark
72
+ end # module Logging
73
+
74
+
75
+ if __FILE__ == $0
76
+ bm = ::Logging::Benchmark.new
77
+ bm.run
78
+ end
79
+
80
+ # EOF
@@ -0,0 +1,46 @@
1
+ # $Id: test_basic.rb 15 2007-01-15 19:03:45Z tim_pease $
2
+
3
+ require 'test/setup.rb'
4
+
5
+ module TestLogging
6
+ module TestLayouts
7
+
8
+ class TestBasic < Test::Unit::TestCase
9
+ include LoggingTestCase
10
+
11
+ def setup
12
+ super
13
+ ::Logging.define_levels %w(debug info warn error fatal)
14
+ @layout = ::Logging::Layouts::Basic.new
15
+ @levels = ::Logging::LEVELS
16
+ end
17
+
18
+ def test_format
19
+ event = ::Logging::LogEvent.new( 'ArrayLogger', @levels['info'],
20
+ ['log message'], false)
21
+ assert_equal " INFO ArrayLogger : log message\n", @layout.format(event)
22
+
23
+ event.data = [[1, 2, 3, 4]]
24
+ assert_equal " INFO ArrayLogger : <Array> 1234\n", @layout.format(event)
25
+
26
+ event.level = @levels['debug']
27
+ event.data = [[1, 2, 3, 4], 'and some message']
28
+ log = "DEBUG ArrayLogger : <Array> 1234\n"
29
+ log << "DEBUG ArrayLogger : and some message\n"
30
+ assert_equal log, @layout.format(event)
31
+
32
+ event.logger = 'Test'
33
+ event.level = @levels['fatal']
34
+ event.data = [[1, 2, 3, 4], 'and some message', Exception.new]
35
+ log = "FATAL Test : <Array> 1234\n"
36
+ log << "FATAL Test : and some message\n"
37
+ log << "FATAL Test : <Exception> Exception\n"
38
+ assert_equal log, @layout.format(event)
39
+ end
40
+
41
+ end # class TestBasic
42
+
43
+ end # module TestLayouts
44
+ end # module TestLogging
45
+
46
+ # EOF
@@ -0,0 +1,175 @@
1
+ # $Id: test_pattern.rb 13 2007-01-15 17:19:37Z tim_pease $
2
+
3
+ require 'test/setup.rb'
4
+
5
+ module TestLogging
6
+ module TestLayouts
7
+
8
+ class TestPattern < Test::Unit::TestCase
9
+ include LoggingTestCase
10
+
11
+ def setup
12
+ super
13
+ ::Logging.define_levels %w(debug info warn error fatal)
14
+ @layout = ::Logging::Layouts::Pattern.new
15
+ @levels = ::Logging::LEVELS
16
+ @date_fmt = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
17
+ end
18
+
19
+ def test_date_method
20
+ assert_nil @layout.date_method
21
+ end
22
+
23
+ def test_date_method_eq
24
+ @layout.date_method = :to_f
25
+ assert_equal :to_f, @layout.date_method
26
+ assert_instance_of Float, @layout.format_date
27
+
28
+ @layout.date_method = 'usec'
29
+ assert_equal 'usec', @layout.date_method
30
+ assert_instance_of Fixnum, @layout.format_date
31
+
32
+ @layout.date_method = :to_s
33
+ assert_equal :to_s, @layout.date_method
34
+ assert_instance_of String, @layout.format_date
35
+
36
+ # now, even if we have defined a date_pattern, the date_method should
37
+ # supersede the date_pattern
38
+ @layout.date_pattern = '%Y'
39
+
40
+ @layout.date_method = 'usec'
41
+ assert_equal 'usec', @layout.date_method
42
+ assert_instance_of Fixnum, @layout.format_date
43
+ end
44
+
45
+ def test_date_pattern
46
+ assert_equal '%Y-%m-%d %H:%M:%S', @layout.date_pattern
47
+ end
48
+
49
+ def test_date_pattern_eq
50
+ @layout.date_pattern = '%Y'
51
+ assert_equal '%Y', @layout.date_pattern
52
+ assert_match %r/\A\d{4}\z/, @layout.format_date
53
+
54
+ @layout.date_pattern = '%H:%M'
55
+ assert_equal '%H:%M', @layout.date_pattern
56
+ assert_match %r/\A\d{2}:\d{2}\z/, @layout.format_date
57
+ end
58
+
59
+ def test_format
60
+ fmt = '\[' + @date_fmt + '\] %s -- %s : %s\n'
61
+
62
+ event = ::Logging::LogEvent.new('ArrayLogger', @levels['info'],
63
+ ['log message'], false)
64
+ rgxp = Regexp.new(sprintf(fmt, 'INFO ', 'ArrayLogger', 'log message'))
65
+ assert_match rgxp, @layout.format(event)
66
+
67
+ event.data = [[1, 2, 3, 4]]
68
+ rgxp = Regexp.new(sprintf(fmt, 'INFO ', 'ArrayLogger', '<Array> 1234'))
69
+ assert_match rgxp, @layout.format(event)
70
+
71
+ event.level = @levels['debug']
72
+ event.data = [[1, 2, 3, 4], 'and some message']
73
+ rgxp = Regexp.new(
74
+ sprintf(fmt, 'DEBUG', 'ArrayLogger', '<Array> 1234') +
75
+ sprintf(fmt, 'DEBUG', 'ArrayLogger', 'and some message'))
76
+ assert_match rgxp, @layout.format(event)
77
+
78
+ event.logger = 'Test'
79
+ event.level = @levels['fatal']
80
+ event.data = [[1, 2, 3, 4], 'and some message', Exception.new]
81
+ rgxp = Regexp.new(
82
+ sprintf(fmt, 'FATAL', 'Test', '<Array> 1234') +
83
+ sprintf(fmt, 'FATAL', 'Test', 'and some message') +
84
+ sprintf(fmt, 'FATAL', 'Test', '<Exception> Exception'))
85
+ assert_match rgxp, @layout.format(event)
86
+ end
87
+
88
+ def test_format_date
89
+ rgxp = Regexp.new @date_fmt
90
+ assert_match rgxp, @layout.format_date
91
+ end
92
+
93
+ def test_pattern
94
+ assert_equal "[%d] %-5l -- %c : %m\n", @layout.pattern
95
+ end
96
+
97
+ def test_pattern_eq
98
+ event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
99
+ ['log message'], false)
100
+
101
+ @layout.pattern = '%d'
102
+ assert_equal '%d', @layout.pattern
103
+ assert_match Regexp.new(@date_fmt), @layout.format(event)
104
+ end
105
+
106
+ def test_pattern_all
107
+ event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
108
+ ['log message'], false)
109
+ event.instance_variable_set :@file, 'test_file.rb'
110
+ event.instance_variable_set :@line, '123'
111
+ event.instance_variable_set :@method, 'method_name'
112
+
113
+ @layout.pattern = '%c'
114
+ assert_equal 'TestLogger', @layout.format(event)
115
+
116
+ @layout.pattern = '%d'
117
+ assert_match Regexp.new(@date_fmt), @layout.format(event)
118
+
119
+ @layout.pattern = '%F'
120
+ assert_equal 'test_file.rb', @layout.format(event)
121
+
122
+ @layout.pattern = '%l'
123
+ assert_equal 'INFO', @layout.format(event)
124
+
125
+ @layout.pattern = '%L'
126
+ assert_equal '123', @layout.format(event)
127
+
128
+ @layout.pattern = '%m'
129
+ assert_equal 'log message', @layout.format(event)
130
+
131
+ @layout.pattern = '%M'
132
+ assert_equal 'method_name', @layout.format(event)
133
+
134
+ @layout.pattern = '%p'
135
+ assert_match %r/\A\d+\z/, @layout.format(event)
136
+
137
+ @layout.pattern = '%r'
138
+ assert_match %r/\A\d+\z/, @layout.format(event)
139
+
140
+ @layout.pattern = '%t'
141
+ assert_match %r/\A\d+\z/, @layout.format(event)
142
+
143
+ @layout.pattern = '%%'
144
+ assert_equal '%', @layout.format(event)
145
+
146
+ # 'z' is not a recognized format character
147
+ assert_raise(ArgumentError) {
148
+ @layout.pattern = '[%d] %% %c - %l %z blah'
149
+ }
150
+ assert_equal '%', @layout.format(event)
151
+
152
+ @layout.pattern = '%5l'
153
+ assert_equal ' INFO', @layout.format(event)
154
+
155
+ @layout.pattern = '%-5l'
156
+ assert_equal 'INFO ', @layout.format(event)
157
+
158
+ @layout.pattern = '%.1l, %c'
159
+ assert_equal 'I, TestLogger', @layout.format(event)
160
+
161
+ @layout.pattern = '%7.7m'
162
+ assert_equal 'log mes', @layout.format(event)
163
+
164
+ event.data = ['tim']
165
+ assert_equal ' tim', @layout.format(event)
166
+
167
+ @layout.pattern = '%-7.7m'
168
+ assert_equal 'tim ', @layout.format(event)
169
+ end
170
+
171
+ end # class TestBasic
172
+ end # module TestLayouts
173
+ end # module TestLogging
174
+
175
+ # EOF