logging 0.9.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/History.txt +13 -0
  2. data/README.rdoc +51 -39
  3. data/Rakefile +1 -0
  4. data/examples/appenders.rb +44 -0
  5. data/examples/classes.rb +39 -0
  6. data/examples/formatting.rb +49 -0
  7. data/examples/hierarchies.rb +71 -0
  8. data/examples/layouts.rb +45 -0
  9. data/examples/loggers.rb +26 -0
  10. data/examples/names.rb +40 -0
  11. data/examples/simple.rb +14 -0
  12. data/lib/logging.rb +50 -21
  13. data/lib/logging/appender.rb +4 -56
  14. data/lib/logging/appenders.rb +120 -0
  15. data/lib/logging/appenders/buffering.rb +2 -1
  16. data/lib/logging/appenders/console.rb +0 -2
  17. data/lib/logging/appenders/rolling_file.rb +0 -2
  18. data/lib/logging/appenders/string_io.rb +1 -3
  19. data/lib/logging/appenders/syslog.rb +0 -7
  20. data/lib/logging/config/configurator.rb +1 -1
  21. data/lib/logging/config/yaml_configurator.rb +3 -7
  22. data/lib/logging/layout.rb +2 -4
  23. data/lib/logging/layouts.rb +47 -0
  24. data/lib/logging/layouts/basic.rb +2 -4
  25. data/lib/logging/layouts/parseable.rb +211 -0
  26. data/lib/logging/layouts/pattern.rb +6 -8
  27. data/lib/logging/log_event.rb +1 -1
  28. data/lib/logging/logger.rb +4 -4
  29. data/lib/spec/logging_helper.rb +2 -2
  30. data/test/appenders/test_buffered_io.rb +26 -18
  31. data/test/appenders/test_console.rb +10 -10
  32. data/test/appenders/test_email.rb +18 -19
  33. data/test/appenders/test_file.rb +12 -12
  34. data/test/appenders/test_growl.rb +11 -12
  35. data/test/appenders/test_io.rb +14 -15
  36. data/test/appenders/test_rolling_file.rb +15 -24
  37. data/test/appenders/test_syslog.rb +10 -10
  38. data/test/layouts/test_basic.rb +4 -5
  39. data/test/layouts/test_json.rb +112 -0
  40. data/test/layouts/test_pattern.rb +9 -9
  41. data/test/layouts/test_yaml.rb +121 -0
  42. data/test/setup.rb +1 -1
  43. data/test/test_appender.rb +0 -14
  44. data/test/test_log_event.rb +1 -1
  45. data/test/test_logging.rb +3 -3
  46. metadata +17 -2
@@ -12,8 +12,8 @@ module TestAppenders
12
12
 
13
13
  def setup
14
14
  super
15
- ::Logging.init
16
- @levels = ::Logging::LEVELS
15
+ Logging.init
16
+ @levels = Logging::LEVELS
17
17
  @logopt = defined?(::Syslog::LOG_NDELAY) ?
18
18
  (::Syslog::LOG_PERROR | ::Syslog::LOG_NDELAY) :
19
19
  (::Syslog::LOG_PERROR)
@@ -30,8 +30,8 @@ module TestAppenders
30
30
  stderr[1].close
31
31
 
32
32
  appender = create_syslog
33
- event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
34
- [1, 2, 3, 4], false)
33
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
34
+ [1, 2, 3, 4], false)
35
35
  appender.append event
36
36
  event.level = @levels['debug']
37
37
  event.data = 'the big log message'
@@ -63,8 +63,8 @@ module TestAppenders
63
63
  appender = create_syslog
64
64
  appender.close false
65
65
 
66
- event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
67
- [1, 2, 3, 4], false)
66
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
67
+ [1, 2, 3, 4], false)
68
68
  assert_raise(RuntimeError) {appender.append event}
69
69
  assert_equal true, appender.closed?
70
70
  end
@@ -149,7 +149,7 @@ module TestAppenders
149
149
  end
150
150
 
151
151
  def test_initialize_map
152
- appender = ::Logging::Appenders::Syslog.new(
152
+ appender = Logging.appenders.syslog(
153
153
  'syslog_test',
154
154
  :logopt => @logopt,
155
155
  :map => {
@@ -171,8 +171,8 @@ module TestAppenders
171
171
  private
172
172
 
173
173
  def create_syslog
174
- layout = ::Logging::Layouts::Pattern.new(:pattern => '%5l %c : %m')
175
- ::Logging::Appenders::Syslog.new(
174
+ layout = Logging.layouts.pattern(:pattern => '%5l %c : %m')
175
+ Logging.appenders.syslog(
176
176
  'syslog_test',
177
177
  :logopt => @logopt,
178
178
  :facility => ::Syslog::LOG_USER,
@@ -184,7 +184,7 @@ module TestAppenders
184
184
  syslog.instance_variable_get :@map
185
185
  end
186
186
 
187
- end # class TestIO
187
+ end # class TestSyslog
188
188
 
189
189
  end # module TestAppenders
190
190
  end # module TestLogging
@@ -9,14 +9,13 @@ module TestLayouts
9
9
 
10
10
  def setup
11
11
  super
12
- ::Logging.init
13
- @layout = ::Logging::Layouts::Basic.new
14
- @levels = ::Logging::LEVELS
12
+ @layout = Logging.layouts.basic({})
13
+ @levels = Logging::LEVELS
15
14
  end
16
15
 
17
16
  def test_format
18
- event = ::Logging::LogEvent.new( 'ArrayLogger', @levels['info'],
19
- 'log message', false)
17
+ event = Logging::LogEvent.new( 'ArrayLogger', @levels['info'],
18
+ 'log message', false)
20
19
  assert_equal " INFO ArrayLogger : log message\n", @layout.format(event)
21
20
 
22
21
  event.data = [1, 2, 3, 4]
@@ -0,0 +1,112 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[.. setup])
3
+
4
+ module TestLogging
5
+ module TestLayouts
6
+
7
+ class TestJson < Test::Unit::TestCase
8
+ include LoggingTestCase
9
+
10
+ def setup
11
+ super
12
+ @layout = Logging.layouts.json({})
13
+ @levels = Logging::LEVELS
14
+ @date_fmt = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
15
+ Thread.current[:name] = nil
16
+ end
17
+
18
+ def test_initializer
19
+ assert_raise(ArgumentError) {
20
+ Logging.layouts.parseable.new :style => :foo
21
+ }
22
+ end
23
+
24
+ def test_format
25
+ fmt = %Q[\\{"timestamp":"#@date_fmt","level":"%s","logger":"%s","message":"%s"\\}\\n]
26
+
27
+ event = Logging::LogEvent.new('ArrayLogger', @levels['info'],
28
+ 'log message', false)
29
+ rgxp = Regexp.new(sprintf(fmt, 'INFO', 'ArrayLogger', 'log message'))
30
+ assert_match rgxp, @layout.format(event)
31
+
32
+ event.data = [1, 2, 3, 4]
33
+ rgxp = Regexp.new(sprintf(fmt, 'INFO', 'ArrayLogger',
34
+ Regexp.escape("<Array> #{[1,2,3,4]}")))
35
+ assert_match rgxp, @layout.format(event)
36
+
37
+ event.level = @levels['debug']
38
+ event.data = 'and another message'
39
+ rgxp = Regexp.new(sprintf(fmt, 'DEBUG', 'ArrayLogger',
40
+ 'and another message'))
41
+ assert_match rgxp, @layout.format(event)
42
+
43
+ event.logger = 'Test'
44
+ event.level = @levels['fatal']
45
+ event.data = Exception.new
46
+ rgxp = Regexp.new(sprintf(fmt, 'FATAL', 'Test', '<Exception> Exception'))
47
+ assert_match rgxp, @layout.format(event)
48
+ end
49
+
50
+ def test_items
51
+ assert_equal %w[timestamp level logger message], @layout.items
52
+ end
53
+
54
+ def test_items_eq
55
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
56
+ ['log message'], false)
57
+
58
+ @layout.items = %w[timestamp]
59
+ assert_equal %w[timestamp], @layout.items
60
+ assert_match %r/\{"timestamp":"#@date_fmt"\}\n/, @layout.format(event)
61
+
62
+ # 'foo' is not a recognized item
63
+ assert_raise(ArgumentError) {
64
+ @layout.items = %w[timestamp logger foo]
65
+ }
66
+ end
67
+
68
+ def test_items_all
69
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
70
+ 'log message', false)
71
+ event.instance_variable_set :@file, 'test_file.rb'
72
+ event.instance_variable_set :@line, 123
73
+ event.instance_variable_set :@method, 'method_name'
74
+
75
+ @layout.items = %w[logger]
76
+ assert_equal %Q[{"logger":"TestLogger"}\n], @layout.format(event)
77
+
78
+ @layout.items = %w[file]
79
+ assert_equal %Q[{"file":"test_file.rb"}\n], @layout.format(event)
80
+
81
+ @layout.items = %w[level]
82
+ assert_equal %Q[{"level":"INFO"}\n], @layout.format(event)
83
+
84
+ @layout.items = %w[line]
85
+ assert_equal %Q[{"line":123}\n], @layout.format(event)
86
+
87
+ @layout.items = %w[message]
88
+ assert_equal %Q[{"message":"log message"}\n], @layout.format(event)
89
+
90
+ @layout.items = %w[method]
91
+ assert_equal %Q[{"method":"method_name"}\n], @layout.format(event)
92
+
93
+ @layout.items = %w[pid]
94
+ assert_match %r/\A\{"pid":\d+\}\n\z/, @layout.format(event)
95
+
96
+ @layout.items = %w[millis]
97
+ assert_match %r/\A\{"millis":\d+\}\n\z/, @layout.format(event)
98
+
99
+ @layout.items = %w[thread_id]
100
+ assert_match %r/\A\{"thread_id":-?\d+\}\n\z/, @layout.format(event)
101
+
102
+ @layout.items = %w[thread]
103
+ assert_equal %Q[{"thread":null}\n], @layout.format(event)
104
+ Thread.current[:name] = "Main"
105
+ assert_equal %Q[{"thread":"Main"}\n], @layout.format(event)
106
+ end
107
+
108
+ end # class TestJson
109
+ end # module TestLayouts
110
+ end # module TestLogging
111
+
112
+ # EOF
@@ -9,10 +9,10 @@ module TestLayouts
9
9
 
10
10
  def setup
11
11
  super
12
- ::Logging.init
13
- @layout = ::Logging::Layouts::Pattern.new
14
- @levels = ::Logging::LEVELS
12
+ @layout = Logging.layouts.pattern({})
13
+ @levels = Logging::LEVELS
15
14
  @date_fmt = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
15
+ Thread.current[:name] = nil
16
16
  end
17
17
 
18
18
  def test_date_method
@@ -58,8 +58,8 @@ module TestLayouts
58
58
  def test_format
59
59
  fmt = '\[' + @date_fmt + '\] %s -- %s : %s\n'
60
60
 
61
- event = ::Logging::LogEvent.new('ArrayLogger', @levels['info'],
62
- 'log message', false)
61
+ event = Logging::LogEvent.new('ArrayLogger', @levels['info'],
62
+ 'log message', false)
63
63
  rgxp = Regexp.new(sprintf(fmt, 'INFO ', 'ArrayLogger', 'log message'))
64
64
  assert_match rgxp, @layout.format(event)
65
65
 
@@ -92,8 +92,8 @@ module TestLayouts
92
92
  end
93
93
 
94
94
  def test_pattern_eq
95
- event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
96
- ['log message'], false)
95
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
96
+ ['log message'], false)
97
97
 
98
98
  @layout.pattern = '%d'
99
99
  assert_equal '%d', @layout.pattern
@@ -101,8 +101,8 @@ module TestLayouts
101
101
  end
102
102
 
103
103
  def test_pattern_all
104
- event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
105
- 'log message', false)
104
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
105
+ 'log message', false)
106
106
  event.instance_variable_set :@file, 'test_file.rb'
107
107
  event.instance_variable_set :@line, '123'
108
108
  event.instance_variable_set :@method, 'method_name'
@@ -0,0 +1,121 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[.. setup])
3
+
4
+ module TestLogging
5
+ module TestLayouts
6
+
7
+ class TestYaml < Test::Unit::TestCase
8
+ include LoggingTestCase
9
+
10
+ def setup
11
+ super
12
+ @layout = Logging.layouts.yaml({})
13
+ @levels = Logging::LEVELS
14
+ @date_fmt = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
15
+ Thread.current[:name] = nil
16
+ end
17
+
18
+ def test_format
19
+ h = {
20
+ 'level' => 'INFO',
21
+ 'logger' => 'ArrayLogger',
22
+ 'message' => 'log message'
23
+ }
24
+
25
+ event = Logging::LogEvent.new('ArrayLogger', @levels['info'],
26
+ 'log message', false)
27
+ assert_yaml_match h, @layout.format(event)
28
+
29
+ event.data = [1, 2, 3, 4]
30
+ h['message'] = "<Array> #{[1,2,3,4]}"
31
+ assert_yaml_match h, @layout.format(event)
32
+
33
+ event.level = @levels['debug']
34
+ event.data = 'and another message'
35
+ h['level'] = 'DEBUG'
36
+ h['message'] = 'and another message'
37
+ assert_yaml_match h, @layout.format(event)
38
+
39
+ event.logger = 'Test'
40
+ event.level = @levels['fatal']
41
+ event.data = Exception.new
42
+ h['level'] = 'FATAL'
43
+ h['logger'] = 'Test'
44
+ h['message'] = '<Exception> Exception'
45
+ assert_yaml_match h, @layout.format(event)
46
+ end
47
+
48
+ def test_items
49
+ assert_equal %w[timestamp level logger message], @layout.items
50
+ end
51
+
52
+ def test_items_eq
53
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
54
+ ['log message'], false)
55
+
56
+ @layout.items = %w[timestamp]
57
+ assert_equal %w[timestamp], @layout.items
58
+ assert_match %r/--- \ntimestamp: #@date_fmt\n/, @layout.format(event)
59
+
60
+ # 'foo' is not a recognized item
61
+ assert_raise(ArgumentError) {
62
+ @layout.items = %w[timestamp logger foo]
63
+ }
64
+ end
65
+
66
+ def test_items_all
67
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
68
+ 'log message', false)
69
+ event.instance_variable_set :@file, 'test_file.rb'
70
+ event.instance_variable_set :@line, 123
71
+ event.instance_variable_set :@method, 'method_name'
72
+
73
+ @layout.items = %w[logger]
74
+ assert_equal %Q[--- \nlogger: TestLogger\n], @layout.format(event)
75
+
76
+ @layout.items = %w[file]
77
+ assert_equal %Q[--- \nfile: test_file.rb\n], @layout.format(event)
78
+
79
+ @layout.items = %w[level]
80
+ assert_equal %Q[--- \nlevel: INFO\n], @layout.format(event)
81
+
82
+ @layout.items = %w[line]
83
+ assert_equal %Q[--- \nline: 123\n], @layout.format(event)
84
+
85
+ @layout.items = %w[message]
86
+ assert_equal %Q[--- \nmessage: log message\n], @layout.format(event)
87
+
88
+ @layout.items = %w[method]
89
+ assert_equal %Q[--- \nmethod: method_name\n], @layout.format(event)
90
+
91
+ @layout.items = %w[pid]
92
+ assert_match %r/\A--- \npid: \d+\n\z/, @layout.format(event)
93
+
94
+ @layout.items = %w[millis]
95
+ assert_match %r/\A--- \nmillis: \d+\n\z/, @layout.format(event)
96
+
97
+ @layout.items = %w[thread_id]
98
+ assert_match %r/\A--- \nthread_id: -?\d+\n\z/, @layout.format(event)
99
+
100
+ @layout.items = %w[thread]
101
+ assert_equal %Q[--- \nthread: \n], @layout.format(event)
102
+ Thread.current[:name] = "Main"
103
+ assert_equal %Q[--- \nthread: Main\n], @layout.format(event)
104
+ end
105
+
106
+ private
107
+
108
+ def assert_yaml_match( expected, actual )
109
+ actual = YAML.load(actual)
110
+
111
+ assert_match %r/#@date_fmt/o, actual['timestamp']
112
+ assert_equal expected['level'], actual['level']
113
+ assert_equal expected['logger'], actual['logger']
114
+ assert_equal expected['message'], actual['message']
115
+ end
116
+
117
+ end # class TestYaml
118
+ end # module TestLayouts
119
+ end # module TestLogging
120
+
121
+ # EOF
data/test/setup.rb CHANGED
@@ -59,7 +59,7 @@ module LoggingTestCase
59
59
  super
60
60
  ::Logging.backtrace
61
61
  ::Logging.__send__(:remove_instance_variable, :@backtrace)
62
- h = ::Logging::Appender.instance_variable_get(:@appenders)
62
+ h = ::Logging::Appenders.instance_variable_get(:@appenders)
63
63
  h.each_value {|a| a.close(false) unless a.nil? || a.closed?}
64
64
  h.clear
65
65
  FileUtils.rm_rf TMP
@@ -40,20 +40,6 @@ module TestLogging
40
40
  assert_raise(RuntimeError) {@appender.append @event}
41
41
  end
42
42
 
43
- def test_class_stderr
44
- stderr = ::Logging::Appender.stderr
45
- assert_instance_of ::Logging::Appenders::Stderr, stderr
46
- assert_equal 'stderr', stderr.name
47
- assert_same stderr, ::Logging::Appender.stderr
48
- end
49
-
50
- def test_class_stdout
51
- stdout = ::Logging::Appender.stdout
52
- assert_instance_of ::Logging::Appenders::Stdout, stdout
53
- assert_equal 'stdout', stdout.name
54
- assert_same stdout, ::Logging::Appender.stdout
55
- end
56
-
57
43
  def test_close
58
44
  assert_equal false, @appender.closed?
59
45
 
@@ -48,7 +48,7 @@ module TestLogging
48
48
 
49
49
  @logger.trace = true
50
50
  @logger.error 'error message'
51
- assert_match %r/\d+/, @appender.event.line
51
+ assert_equal 50, @appender.event.line
52
52
  end
53
53
 
54
54
  def test_logger
data/test/test_logging.rb CHANGED
@@ -50,11 +50,11 @@ module TestLogging
50
50
  assert_equal 3, ::Logging::Logger.root.level
51
51
 
52
52
  # verify the appenders
53
- h = ::Logging::Appender.instance_variable_get :@appenders
53
+ h = ::Logging::Appenders.instance_variable_get :@appenders
54
54
  assert_equal ['logfile', 'stderr'], h.keys.sort
55
55
 
56
56
  # start with the File appender
57
- logfile = ::Logging::Appender['logfile']
57
+ logfile = ::Logging::Appenders['logfile']
58
58
  assert_instance_of ::Logging::Appenders::File, logfile
59
59
  assert_equal 0, logfile.level
60
60
  assert_equal 'tmp/temp.log', logfile.instance_variable_get(:@fn)
@@ -66,7 +66,7 @@ module TestLogging
66
66
  assert_nil layout.date_pattern
67
67
 
68
68
  # and now the Stderr appender
69
- stderr = ::Logging::Appender['stderr']
69
+ stderr = ::Logging::Appenders['stderr']
70
70
  assert_instance_of ::Logging::Appenders::Stderr, stderr
71
71
  assert_equal 0, stderr.level
72
72
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-15 00:00:00 -06:00
12
+ date: 2009-04-17 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -60,8 +60,17 @@ files:
60
60
  - data/logging.rb
61
61
  - data/logging.yaml
62
62
  - data/simple_logging.rb
63
+ - examples/appenders.rb
64
+ - examples/classes.rb
65
+ - examples/formatting.rb
66
+ - examples/hierarchies.rb
67
+ - examples/layouts.rb
68
+ - examples/loggers.rb
69
+ - examples/names.rb
70
+ - examples/simple.rb
63
71
  - lib/logging.rb
64
72
  - lib/logging/appender.rb
73
+ - lib/logging/appenders.rb
65
74
  - lib/logging/appenders/buffering.rb
66
75
  - lib/logging/appenders/console.rb
67
76
  - lib/logging/appenders/email.rb
@@ -74,7 +83,9 @@ files:
74
83
  - lib/logging/config/configurator.rb
75
84
  - lib/logging/config/yaml_configurator.rb
76
85
  - lib/logging/layout.rb
86
+ - lib/logging/layouts.rb
77
87
  - lib/logging/layouts/basic.rb
88
+ - lib/logging/layouts/parseable.rb
78
89
  - lib/logging/layouts/pattern.rb
79
90
  - lib/logging/log_event.rb
80
91
  - lib/logging/logger.rb
@@ -108,7 +119,9 @@ files:
108
119
  - test/config/test_configurator.rb
109
120
  - test/config/test_yaml_configurator.rb
110
121
  - test/layouts/test_basic.rb
122
+ - test/layouts/test_json.rb
111
123
  - test/layouts/test_pattern.rb
124
+ - test/layouts/test_yaml.rb
112
125
  - test/setup.rb
113
126
  - test/test_appender.rb
114
127
  - test/test_layout.rb
@@ -158,7 +171,9 @@ test_files:
158
171
  - test/config/test_configurator.rb
159
172
  - test/config/test_yaml_configurator.rb
160
173
  - test/layouts/test_basic.rb
174
+ - test/layouts/test_json.rb
161
175
  - test/layouts/test_pattern.rb
176
+ - test/layouts/test_yaml.rb
162
177
  - test/test_appender.rb
163
178
  - test/test_layout.rb
164
179
  - test/test_log_event.rb