logging 2.0.0 → 2.3.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.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +8 -5
  4. data/History.txt +59 -0
  5. data/LICENSE +22 -0
  6. data/README.md +20 -41
  7. data/Rakefile +2 -2
  8. data/examples/appenders.rb +1 -1
  9. data/examples/layouts.rb +1 -1
  10. data/examples/lazy.rb +1 -1
  11. data/examples/mdc.rb +2 -2
  12. data/examples/rails4.rb +21 -0
  13. data/examples/reusing_layouts.rb +51 -0
  14. data/lib/logging.rb +99 -9
  15. data/lib/logging/appender.rb +13 -34
  16. data/lib/logging/appenders/buffering.rb +130 -59
  17. data/lib/logging/appenders/console.rb +68 -57
  18. data/lib/logging/appenders/file.rb +43 -22
  19. data/lib/logging/appenders/io.rb +22 -16
  20. data/lib/logging/appenders/rolling_file.rb +60 -26
  21. data/lib/logging/appenders/string_io.rb +1 -1
  22. data/lib/logging/appenders/syslog.rb +3 -4
  23. data/lib/logging/color_scheme.rb +1 -1
  24. data/lib/logging/diagnostic_context.rb +100 -73
  25. data/lib/logging/layout.rb +144 -16
  26. data/lib/logging/layouts/parseable.rb +50 -12
  27. data/lib/logging/layouts/pattern.rb +8 -9
  28. data/lib/logging/log_event.rb +19 -12
  29. data/lib/logging/logger.rb +117 -95
  30. data/lib/logging/proxy.rb +1 -1
  31. data/lib/logging/rails_compat.rb +4 -13
  32. data/lib/logging/version.rb +1 -1
  33. data/logging.gemspec +31 -32
  34. data/script/console +8 -0
  35. data/test/appenders/{test_periodic_flushing.rb → test_async_flushing.rb} +67 -14
  36. data/test/appenders/test_buffered_io.rb +19 -18
  37. data/test/appenders/test_console.rb +55 -12
  38. data/test/appenders/test_file.rb +48 -28
  39. data/test/appenders/test_rolling_file.rb +18 -12
  40. data/test/appenders/test_syslog.rb +6 -0
  41. data/test/benchmark.rb +42 -18
  42. data/test/layouts/test_json.rb +14 -1
  43. data/test/layouts/test_nested_exceptions.rb +124 -0
  44. data/test/layouts/test_pattern.rb +16 -3
  45. data/test/layouts/test_yaml.rb +15 -1
  46. data/test/performance.rb +66 -0
  47. data/test/setup.rb +26 -30
  48. data/test/test_appender.rb +2 -4
  49. data/test/test_layout.rb +49 -0
  50. data/test/test_log_event.rb +10 -2
  51. data/test/test_logger.rb +20 -3
  52. data/test/test_logging.rb +75 -4
  53. data/test/test_mapped_diagnostic_context.rb +15 -6
  54. data/test/test_nested_diagnostic_context.rb +6 -1
  55. metadata +23 -17
@@ -1,42 +1,38 @@
1
-
2
1
  # Equivalent to a header guard in C/C++
3
2
  # Used to prevent the class/module from being loaded more than once
4
3
  unless defined? LOGGING_TEST_SETUP
5
4
  LOGGING_TEST_SETUP = true
6
5
 
7
- require 'rubygems'
8
- require 'test/unit'
9
- begin
10
- require 'turn'
11
- rescue LoadError; end
6
+ require "rubygems"
7
+ require "test/unit"
8
+ require "tmpdir"
12
9
 
13
- # This line is needed for Ruby 1.9 -- hashes throw a "KeyError" in 1.9
14
- # whereas they throw an "IndexError" in 1.8
15
- #
16
- KeyError = IndexError if not defined? KeyError
10
+ LOGGING_TEST_TMPDIR = Dir.mktmpdir("logging")
11
+ Test::Unit.at_exit do
12
+ FileUtils.remove_entry(LOGGING_TEST_TMPDIR)
13
+ end
17
14
 
18
- require File.join(File.dirname(__FILE__), %w[.. lib logging])
15
+ if Test::Unit::TestCase.respond_to? :test_order=
16
+ Test::Unit::TestCase.test_order = :random
17
+ end
19
18
 
19
+ require File.expand_path("../../lib/logging", __FILE__)
20
20
 
21
21
  module TestLogging
22
- module LoggingTestCase
23
-
24
- TMP = 'tmp'
25
-
26
- def setup
27
- super
28
- Logging.reset
29
- FileUtils.rm_rf TMP
30
- FileUtils.mkdir TMP
22
+ module LoggingTestCase
23
+
24
+ def setup
25
+ super
26
+ Logging.reset
27
+ @tmpdir = LOGGING_TEST_TMPDIR
28
+ FileUtils.rm_rf(Dir.glob(File.join(@tmpdir, "*")))
29
+ end
30
+
31
+ def teardown
32
+ super
33
+ FileUtils.rm_rf(Dir.glob(File.join(@tmpdir, "*")))
34
+ end
31
35
  end
36
+ end
32
37
 
33
- def teardown
34
- super
35
- FileUtils.rm_rf TMP
36
- end
37
-
38
- end # LoggingTestCase
39
- end # TestLogging
40
-
41
- end # defined?
42
-
38
+ end
@@ -189,11 +189,9 @@ module TestLogging
189
189
  assert_equal 'test_appender', @appender.name
190
190
  end
191
191
 
192
- def test_inspect
193
- expected = "<Appender:0x%x name=\"test_appender\">" % @appender.object_id
194
- assert_equal expected, @appender.inspect
192
+ def test_to_s
193
+ assert_equal "<Appender name=\"test_appender\">", @appender.to_s
195
194
  end
196
-
197
195
  end # class TestAppender
198
196
  end # module TestLogging
199
197
 
@@ -111,6 +111,55 @@ module TestLogging
111
111
  assert_raise(ArgumentError) {::Logging::Layout.new :backtrace => 'foo'}
112
112
  end
113
113
 
114
+ def test_backtrace_accessors
115
+ assert @layout.backtrace?
116
+
117
+ @layout.backtrace = :off
118
+ refute @layout.backtrace?
119
+
120
+ @layout.backtrace = 'on'
121
+ assert_equal true, @layout.backtrace
122
+ end
123
+
124
+ def test_utc_offset
125
+ assert_nil @layout.utc_offset
126
+
127
+ @layout.utc_offset = 0
128
+ assert_equal 0, @layout.utc_offset
129
+
130
+ @layout.utc_offset = "UTC"
131
+ assert_equal 0, @layout.utc_offset
132
+
133
+ @layout.utc_offset = "+01:00"
134
+ assert_equal "+01:00", @layout.utc_offset
135
+
136
+ assert_raise(ArgumentError) {@layout.utc_offset = "06:00"}
137
+
138
+ @layout.utc_offset = nil
139
+ ::Logging.utc_offset = "UTC"
140
+ assert_nil @layout.utc_offset
141
+
142
+ layout = ::Logging::Layout.new
143
+ assert_equal 0, layout.utc_offset
144
+ end
145
+
146
+ def test_apply_utc_offset
147
+ time = Time.now.freeze
148
+
149
+ offset_time = @layout.apply_utc_offset(time)
150
+ assert_same time, offset_time
151
+
152
+ @layout.utc_offset = "UTC"
153
+ offset_time = @layout.apply_utc_offset(time)
154
+ assert_not_same time, offset_time
155
+ assert offset_time.utc?
156
+
157
+ @layout.utc_offset = "+01:00"
158
+ offset_time = @layout.apply_utc_offset(time)
159
+ assert_not_same time, offset_time
160
+ assert !offset_time.utc?
161
+ assert_equal 3600, offset_time.utc_offset
162
+ end
114
163
  end # class TestLayout
115
164
  end # module TestLogging
116
165
 
@@ -34,6 +34,14 @@ module TestLogging
34
34
  assert_match %r/test_log_event.rb\z/, @appender.event.file
35
35
  end
36
36
 
37
+ def test_file_with_basepath
38
+ ::Logging.basepath = File.expand_path("../../", __FILE__)
39
+
40
+ @logger.caller_tracing = true
41
+ @logger.warn "warning message"
42
+ assert_equal "test/test_log_event.rb", @appender.event.file
43
+ end
44
+
37
45
  def test_level
38
46
  assert_equal 1, @event.level
39
47
  end
@@ -60,12 +68,12 @@ module TestLogging
60
68
  assert_equal 'MyLogger', @event.logger
61
69
  end
62
70
 
63
- def test_method
71
+ def test_method_name
64
72
  assert_equal '', @event.file
65
73
 
66
74
  @logger.caller_tracing = true
67
75
  @logger.debug 'debug message'
68
- assert_equal 'test_method', @appender.event.method
76
+ assert_equal 'test_method_name', @appender.event.method_name
69
77
  end
70
78
 
71
79
  end # class TestLogEvent
@@ -33,6 +33,16 @@ module TestLogging
33
33
  assert_nil a1.readline
34
34
  assert_nil a2.readline
35
35
 
36
+ log.add(1) { "block style message" }
37
+ assert_equal " INFO A Logger : block style message\n", a1.readline
38
+ assert_nil a1.readline
39
+ assert_nil a2.readline
40
+
41
+ log.add(1, nil, 'this should be logged (when used by Rails.logger.extend(ActiveSupport::Logger.broadcast())')
42
+ assert_equal " INFO A Logger : this should be logged (when used by Rails.logger.extend(ActiveSupport::Logger.broadcast())\n", a1.readline
43
+ assert_nil a1.readline
44
+ assert_nil a2.readline
45
+
36
46
  log.add(2,[1,2,3,4])
37
47
  assert_equal " WARN A Logger : <Array> #{[1,2,3,4]}\n", a1.readline
38
48
  assert_nil a1.readline
@@ -76,6 +86,10 @@ module TestLogging
76
86
  assert_nil a2.readline
77
87
  end
78
88
 
89
+ def test_add_block
90
+
91
+ end
92
+
79
93
  def test_add_appenders
80
94
  log = ::Logging::Logger.new 'A'
81
95
 
@@ -251,11 +265,14 @@ module TestLogging
251
265
  assert_nil a2.readline
252
266
  end
253
267
 
254
- def test_inspect
268
+ def test_inspect_matches_default
255
269
  root = ::Logging::Logger.new :root
256
270
 
257
- str = "<#{root.class.name}:0x%x name=\"#{root.name}\">" % root.object_id
258
- assert_equal str, root.inspect
271
+ # `to_s` triggers the default inspect behavior
272
+ expected = root.to_s.match(/0x[a-f\d]+/)[0]
273
+ actual = root.inspect.match(/0x[a-f\d]+/)[0]
274
+
275
+ assert_equal expected, actual
259
276
  end
260
277
 
261
278
  def test_level
@@ -11,8 +11,8 @@ module TestLogging
11
11
  @levels = ::Logging::LEVELS
12
12
  @lnames = ::Logging::LNAMES
13
13
 
14
- @fn = File.join(TMP, 'test.log')
15
- @glob = File.join(TMP, '*.log')
14
+ @fn = File.join(@tmpdir, 'test.log')
15
+ @glob = File.join(@tmpdir, '*.log')
16
16
  end
17
17
 
18
18
  def test_backtrace
@@ -39,6 +39,52 @@ module TestLogging
39
39
  assert_raise(ArgumentError) {::Logging.backtrace 'foo'}
40
40
  end
41
41
 
42
+ def test_utc_offset
43
+ assert_nil ::Logging.utc_offset
44
+
45
+ ::Logging.utc_offset = 0
46
+ assert_equal 0, ::Logging.utc_offset
47
+
48
+ ::Logging.utc_offset = "UTC"
49
+ assert_equal 0, ::Logging.utc_offset
50
+
51
+ ::Logging.utc_offset = "+01:00"
52
+ assert_equal "+01:00", ::Logging.utc_offset
53
+
54
+ assert_raise(ArgumentError) {::Logging.utc_offset = "06:00"}
55
+ end
56
+
57
+ def test_cause_depth
58
+ assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth
59
+
60
+ ::Logging.cause_depth = 0
61
+ assert_equal 0, ::Logging.cause_depth
62
+
63
+ ::Logging.cause_depth = nil
64
+ assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth
65
+
66
+ ::Logging.cause_depth = "1024"
67
+ assert_equal 1024, ::Logging.cause_depth
68
+
69
+ ::Logging.cause_depth = -1
70
+ assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth
71
+
72
+ assert_raise(ArgumentError) {::Logging.cause_depth = "foo"}
73
+ end
74
+
75
+ def test_basepath
76
+ assert_nil ::Logging.basepath
77
+
78
+ ::Logging.basepath = ""
79
+ assert_nil ::Logging.basepath
80
+
81
+ ::Logging.basepath = "./"
82
+ assert_equal File.expand_path("../../", __FILE__), ::Logging.basepath
83
+
84
+ ::Logging.reset
85
+ assert_nil ::Logging.basepath
86
+ end
87
+
42
88
  def test_logger
43
89
  assert_raise(TypeError) {::Logging.logger []}
44
90
 
@@ -207,6 +253,31 @@ module TestLogging
207
253
  assert_match %r/\d+\.\d+\.\d+/, ::Logging.version
208
254
  end
209
255
 
210
- end # class TestLogging
211
- end # module TestLogging
256
+ class Failer
257
+ class WriteError < StandardError ; end
258
+ def self.write(*args)
259
+ raise WriteError.new("Oh noooooo")
260
+ end
261
+ end
212
262
 
263
+ def test_error_handling
264
+ logger = ::Logging.logger Failer, 2, 100
265
+ logger.appenders.first.level = :debug
266
+
267
+ # No errors are raised by default
268
+ logger.fatal 'this is a debug message'
269
+ # Always reset the level; we disable appenders that raise by setting them
270
+ # to :off
271
+ logger.appenders.first.level = :debug
272
+
273
+ begin
274
+ Logging.raise_errors = true
275
+ assert_raises Failer::WriteError do
276
+ logger.fatal 'this fails because the file descriptor is closed'
277
+ end
278
+ ensure
279
+ Logging.raise_errors = false
280
+ end
281
+ end
282
+ end
283
+ end
@@ -105,12 +105,21 @@ module TestLogging
105
105
 
106
106
  assert_not_equal context.object_id, Logging.mdc.context.object_id
107
107
 
108
- assert_equal 1, Logging.mdc['foo']
109
- assert_equal 'buz', Logging.mdc['baz']
110
- assert_equal 'something else', Logging.mdc['foobar']
111
- assert_nil Logging.mdc['unique']
112
-
113
- assert_equal 1, Logging.mdc.stack.length
108
+ if Logging::INHERIT_CONTEXT
109
+ assert_equal 1, Logging.mdc['foo']
110
+ assert_equal 'buz', Logging.mdc['baz']
111
+ assert_equal 'something else', Logging.mdc['foobar']
112
+ assert_nil Logging.mdc['unique']
113
+
114
+ assert_equal 1, Logging.mdc.stack.length
115
+ else
116
+ assert_nil Logging.mdc['foo']
117
+ assert_nil Logging.mdc['baz']
118
+ assert_nil Logging.mdc['foobar']
119
+ assert_nil Logging.mdc['unique']
120
+
121
+ assert_equal 1, Logging.mdc.stack.length
122
+ end
114
123
  }
115
124
 
116
125
  Thread.pass until t.status == 'sleep'
@@ -85,7 +85,12 @@ module TestLogging
85
85
  sleep
86
86
 
87
87
  assert_not_equal ary.object_id, Logging.ndc.context.object_id
88
- assert_equal %w[first second], Logging.ndc.context
88
+
89
+ if Logging::INHERIT_CONTEXT
90
+ assert_equal %w[first second], Logging.ndc.context
91
+ else
92
+ assert_empty Logging.ndc.context
93
+ end
89
94
  }
90
95
 
91
96
  Thread.pass until t.status == 'sleep'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-29 00:00:00.000000000 Z
11
+ date: 2020-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: little-plugger
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.10'
33
+ version: '1.14'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.10'
40
+ version: '1.14'
41
41
  - !ruby/object:Gem::Dependency
42
- name: flexmock
42
+ name: test-unit
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.0'
47
+ version: '3.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.0'
54
+ version: '3.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bones-git
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,16 +72,16 @@ dependencies:
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 3.8.3
75
+ version: 3.8.4
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 3.8.3
82
+ version: 3.8.4
83
83
  description: |-
84
- Logging is a flexible logging library for use in Ruby programs based on the
84
+ **Logging** is a flexible logging library for use in Ruby programs based on the
85
85
  design of Java's log4j library. It features a hierarchical logging system,
86
86
  custom level names, multiple output destinations per log event, custom
87
87
  formatting, and more.
@@ -94,6 +94,7 @@ files:
94
94
  - ".gitignore"
95
95
  - ".travis.yml"
96
96
  - History.txt
97
+ - LICENSE
97
98
  - README.md
98
99
  - Rakefile
99
100
  - examples/appenders.rb
@@ -108,6 +109,8 @@ files:
108
109
  - examples/loggers.rb
109
110
  - examples/mdc.rb
110
111
  - examples/names.rb
112
+ - examples/rails4.rb
113
+ - examples/reusing_layouts.rb
111
114
  - examples/rspec_integration.rb
112
115
  - examples/simple.rb
113
116
  - lib/logging.rb
@@ -142,11 +145,12 @@ files:
142
145
  - lib/spec/logging_helper.rb
143
146
  - logging.gemspec
144
147
  - script/bootstrap
148
+ - script/console
149
+ - test/appenders/test_async_flushing.rb
145
150
  - test/appenders/test_buffered_io.rb
146
151
  - test/appenders/test_console.rb
147
152
  - test/appenders/test_file.rb
148
153
  - test/appenders/test_io.rb
149
- - test/appenders/test_periodic_flushing.rb
150
154
  - test/appenders/test_rolling_file.rb
151
155
  - test/appenders/test_string_io.rb
152
156
  - test/appenders/test_syslog.rb
@@ -154,8 +158,10 @@ files:
154
158
  - test/layouts/test_basic.rb
155
159
  - test/layouts/test_color_pattern.rb
156
160
  - test/layouts/test_json.rb
161
+ - test/layouts/test_nested_exceptions.rb
157
162
  - test/layouts/test_pattern.rb
158
163
  - test/layouts/test_yaml.rb
164
+ - test/performance.rb
159
165
  - test/setup.rb
160
166
  - test/test_appender.rb
161
167
  - test/test_color_scheme.rb
@@ -173,7 +179,7 @@ files:
173
179
  homepage: http://rubygems.org/gems/logging
174
180
  licenses: []
175
181
  metadata: {}
176
- post_install_message:
182
+ post_install_message:
177
183
  rdoc_options:
178
184
  - "--main"
179
185
  - README.md
@@ -190,23 +196,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
196
  - !ruby/object:Gem::Version
191
197
  version: '0'
192
198
  requirements: []
193
- rubyforge_project: logging
194
- rubygems_version: 2.2.2
195
- signing_key:
199
+ rubygems_version: 3.0.1
200
+ signing_key:
196
201
  specification_version: 4
197
202
  summary: A flexible and extendable logging library for Ruby
198
203
  test_files:
204
+ - test/appenders/test_async_flushing.rb
199
205
  - test/appenders/test_buffered_io.rb
200
206
  - test/appenders/test_console.rb
201
207
  - test/appenders/test_file.rb
202
208
  - test/appenders/test_io.rb
203
- - test/appenders/test_periodic_flushing.rb
204
209
  - test/appenders/test_rolling_file.rb
205
210
  - test/appenders/test_string_io.rb
206
211
  - test/appenders/test_syslog.rb
207
212
  - test/layouts/test_basic.rb
208
213
  - test/layouts/test_color_pattern.rb
209
214
  - test/layouts/test_json.rb
215
+ - test/layouts/test_nested_exceptions.rb
210
216
  - test/layouts/test_pattern.rb
211
217
  - test/layouts/test_yaml.rb
212
218
  - test/test_appender.rb