logging 2.2.2 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  module Logging
2
2
  module Filters ; end
3
3
  require libpath('logging/filters/level')
4
- end
4
+ end
@@ -103,7 +103,7 @@ module Logging::Layouts
103
103
  'message' => 'format_obj(event.data)'.freeze,
104
104
  'file' => 'event.file'.freeze,
105
105
  'line' => 'event.line'.freeze,
106
- 'method' => 'event.method'.freeze,
106
+ 'method' => 'event.method_name'.freeze,
107
107
  'hostname' => "'#{Socket.gethostname}'".freeze,
108
108
  'pid' => 'Process.pid'.freeze,
109
109
  'millis' => 'Integer((event.time-@created_at)*1000)'.freeze,
@@ -307,7 +307,7 @@ module Logging::Layouts
307
307
  'l' => '::Logging::LNAMES[event.level]'.freeze,
308
308
  'L' => 'event.line'.freeze,
309
309
  'm' => 'format_obj(event.data)'.freeze,
310
- 'M' => 'event.method'.freeze,
310
+ 'M' => 'event.method_name'.freeze,
311
311
  'h' => "'#{Socket.gethostname}'".freeze,
312
312
  'p' => 'Process.pid'.freeze,
313
313
  'r' => 'Integer((event.time-@created_at)*1000).to_s'.freeze,
@@ -12,10 +12,11 @@ module Logging
12
12
  # * $3 == method name (might be nil)
13
13
  CALLER_RGXP = %r/([-\.\/\(\)\w]+):(\d+)(?::in `([^']+)')?/o
14
14
  #CALLER_INDEX = 2
15
- CALLER_INDEX = ((defined? JRUBY_VERSION and JRUBY_VERSION > '1.6') or (defined? RUBY_ENGINE and RUBY_ENGINE[%r/^rbx/i])) ? 1 : 2
15
+ CALLER_INDEX = ((defined?(JRUBY_VERSION) && JRUBY_VERSION > '1.6' && JRUBY_VERSION < '9.0') ||
16
+ (defined?(RUBY_ENGINE) && RUBY_ENGINE[%r/^rbx/i])) ? 1 : 2
16
17
  # :startdoc:
17
18
 
18
- attr_accessor :logger, :level, :data, :time, :file, :line, :method
19
+ attr_accessor :logger, :level, :data, :time, :file, :line, :method_name
19
20
 
20
21
  # call-seq:
21
22
  # LogEvent.new( logger, level, [data], caller_tracing )
@@ -36,15 +37,15 @@ module Logging
36
37
  return if stack.nil?
37
38
 
38
39
  match = CALLER_RGXP.match(stack)
39
- self.file = match[1]
40
- self.line = Integer(match[2])
41
- self.method = match[3] unless match[3].nil?
40
+ self.file = match[1]
41
+ self.line = Integer(match[2])
42
+ self.method_name = match[3] unless match[3].nil?
42
43
 
43
44
  if (bp = ::Logging.basepath) && !bp.empty? && file.index(bp) == 0
44
45
  self.file = file.slice(bp.length + 1, file.length - bp.length)
45
46
  end
46
47
  else
47
- self.file = self.line = self.method = ''
48
+ self.file = self.line = self.method_name = ''
48
49
  end
49
50
  end
50
51
  end
@@ -24,8 +24,6 @@ module Logging
24
24
  #
25
25
  class Logger
26
26
 
27
- @mutex = Mutex.new # :nodoc:
28
-
29
27
  # Returns the root logger.
30
28
  def self.root
31
29
  ::Logging::Repository.instance[:root]
@@ -48,7 +46,10 @@ module Logging
48
46
  logger = repo[name]
49
47
  return logger unless logger.nil?
50
48
 
51
- @mutex.synchronize do
49
+ # Share the same mutex that's used by 'define_log_methods' because
50
+ # it iterates over the hash of loggers, and adding a new key to a hash
51
+ # while iterating over it produces an error.
52
+ ::Logging::Logger.mutex.synchronize do
52
53
  logger = repo[name]
53
54
  return logger unless logger.nil? # thread-safe double checking
54
55
 
@@ -118,6 +119,14 @@ module Logging
118
119
  code.join("\n")
119
120
  end
120
121
 
122
+ @mutex = ReentrantMutex.new
123
+
124
+ # Returns a global ReentrantMutex for use when creating Logger instances
125
+ # and/or updating log levels.
126
+ def self.mutex
127
+ @mutex
128
+ end
129
+
121
130
  attr_reader :name, :parent, :additive, :caller_tracing
122
131
 
123
132
  # call-seq:
@@ -411,7 +420,7 @@ module Logging
411
420
  def define_log_methods( force = false, code = nil )
412
421
  return if has_own_level? and !force
413
422
 
414
- ::Logging::Logger._reentrant_mutex.synchronize do
423
+ ::Logging::Logger.mutex.synchronize do
415
424
  ::Logging::Logger.define_log_methods(self)
416
425
  ::Logging::Repository.instance.children(name).each do |child|
417
426
  child.define_log_methods
@@ -423,12 +432,6 @@ module Logging
423
432
  # :stopdoc:
424
433
  public
425
434
 
426
- @reentrant_mutex = ReentrantMutex.new
427
-
428
- def self._reentrant_mutex
429
- @reentrant_mutex
430
- end
431
-
432
435
  # call-seq:
433
436
  # _meta_eval( code )
434
437
  #
@@ -511,6 +514,5 @@ module Logging
511
514
  end
512
515
  # :startdoc:
513
516
 
514
- end # Logger
515
- end # Logging
516
-
517
+ end
518
+ end
data/lib/logging/proxy.rb CHANGED
@@ -47,7 +47,7 @@ module Logging
47
47
 
48
48
  # All hail the magic of method missing. Here is where we are going to log
49
49
  # the method call and then forward to the proxied object. The return value
50
- # from the proxied objet method call is passed back.
50
+ # from the proxied object method call is passed back.
51
51
  #
52
52
  def method_missing( name, *args, &block )
53
53
  @logger << "#@leader#{name}(#{args.inspect[1..-2]})\n"
@@ -1,5 +1,5 @@
1
1
  module Logging
2
- VERSION = "2.2.2".freeze
2
+ VERSION = "2.3.1".freeze
3
3
 
4
4
  # Returns the version string for the library.
5
5
  def self.version
data/lib/logging.rb CHANGED
@@ -258,6 +258,7 @@ module Logging
258
258
  module_eval "MAX_LEVEL_LENGTH = #{longest.length}", __FILE__, __LINE__
259
259
 
260
260
  self.cause_depth = nil unless defined? @cause_depth
261
+ self.raise_errors = false unless defined? @raise_errors
261
262
 
262
263
  initialize_plugins
263
264
  levels.keys
@@ -268,8 +269,8 @@ module Logging
268
269
  #
269
270
  # Defines the default _obj_format_ method to use when converting objects
270
271
  # into string representations for logging. _obj_format_ can be one of
271
- # <tt>:string</tt>, <tt>:inspect</tt>, or <tt>:yaml</tt>. These
272
- # formatting commands map to the following object methods
272
+ # <tt>:string</tt>, <tt>:inspect</tt>, <tt>:json</tt> or <tt>:yaml</tt>.
273
+ # These formatting commands map to the following object methods
273
274
  #
274
275
  # * :string => to_s
275
276
  # * :inspect => inspect
@@ -277,7 +278,7 @@ module Logging
277
278
  # * :json => MultiJson.encode(obj)
278
279
  #
279
280
  # An +ArgumentError+ is raised if anything other than +:string+,
280
- # +:inspect+, +:yaml+ is passed to this method.
281
+ # +:inspect+, +:json+ or +:yaml+ is passed to this method.
281
282
  #
282
283
  def format_as( f )
283
284
  f = f.intern if f.instance_of? String
@@ -490,6 +491,21 @@ module Logging
490
491
  io
491
492
  end
492
493
 
494
+ # Raise an exception when an error is encountered while logging, be it with
495
+ # a backing store, formatter, or anything else. You probably wouldn't want
496
+ # to enable this outside of test.
497
+ #
498
+ # Not that only one error will ever be raised per logging backend, as
499
+ # backends that raise errors on write will be set to :off.
500
+ def raise_errors=(boolean)
501
+ @raise_errors = boolean
502
+ end
503
+
504
+ # Whether or not we should raise errors when writing logs.
505
+ def raise_errors?
506
+ @raise_errors
507
+ end
508
+
493
509
  # :stopdoc:
494
510
  # Convert the given level into a canonical form - a lowercase string.
495
511
  def levelify( level )
@@ -518,7 +534,7 @@ module Logging
518
534
  # exception will be raised again.
519
535
  def log_internal_error( err )
520
536
  log_internal(-2) { err }
521
- raise err if Thread.abort_on_exception
537
+ raise err if ::Logging.raise_errors?
522
538
  end
523
539
 
524
540
  # Close all appenders
@@ -569,8 +585,7 @@ module Logging
569
585
  require libpath('logging/diagnostic_context')
570
586
 
571
587
  require libpath('logging/rails_compat')
572
- end # module Logging
573
-
588
+ end
574
589
 
575
590
  # This finalizer will close all the appenders that exist in the system.
576
591
  # This is needed for closing IO streams and connections to the syslog server
data/logging.gemspec CHANGED
@@ -1,45 +1,44 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: logging 2.1.0.48 ruby lib
2
+ # stub: logging 2.3.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "logging".freeze
6
- s.version = "2.1.0.48"
6
+ s.version = "2.3.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Tim Pease".freeze]
11
- s.date = "2017-01-08"
11
+ s.date = "2020-07-04"
12
12
  s.description = "**Logging** is a flexible logging library for use in Ruby programs based on the\ndesign of Java's log4j library. It features a hierarchical logging system,\ncustom level names, multiple output destinations per log event, custom\nformatting, and more.".freeze
13
13
  s.email = "tim.pease@gmail.com".freeze
14
14
  s.extra_rdoc_files = ["History.txt".freeze]
15
- s.files = [".gitignore".freeze, ".travis.yml".freeze, "History.txt".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "examples/appenders.rb".freeze, "examples/classes.rb".freeze, "examples/colorization.rb".freeze, "examples/custom_log_levels.rb".freeze, "examples/fork.rb".freeze, "examples/formatting.rb".freeze, "examples/hierarchies.rb".freeze, "examples/layouts.rb".freeze, "examples/lazy.rb".freeze, "examples/loggers.rb".freeze, "examples/mdc.rb".freeze, "examples/names.rb".freeze, "examples/rails4.rb".freeze, "examples/reusing_layouts.rb".freeze, "examples/rspec_integration.rb".freeze, "examples/simple.rb".freeze, "lib/logging.rb".freeze, "lib/logging/appender.rb".freeze, "lib/logging/appenders.rb".freeze, "lib/logging/appenders/buffering.rb".freeze, "lib/logging/appenders/console.rb".freeze, "lib/logging/appenders/file.rb".freeze, "lib/logging/appenders/io.rb".freeze, "lib/logging/appenders/rolling_file.rb".freeze, "lib/logging/appenders/string_io.rb".freeze, "lib/logging/appenders/syslog.rb".freeze, "lib/logging/color_scheme.rb".freeze, "lib/logging/diagnostic_context.rb".freeze, "lib/logging/filter.rb".freeze, "lib/logging/filters.rb".freeze, "lib/logging/filters/level.rb".freeze, "lib/logging/layout.rb".freeze, "lib/logging/layouts.rb".freeze, "lib/logging/layouts/basic.rb".freeze, "lib/logging/layouts/parseable.rb".freeze, "lib/logging/layouts/pattern.rb".freeze, "lib/logging/log_event.rb".freeze, "lib/logging/logger.rb".freeze, "lib/logging/proxy.rb".freeze, "lib/logging/rails_compat.rb".freeze, "lib/logging/repository.rb".freeze, "lib/logging/root_logger.rb".freeze, "lib/logging/utils.rb".freeze, "lib/logging/version.rb".freeze, "lib/rspec/logging_helper.rb".freeze, "lib/spec/logging_helper.rb".freeze, "logging.gemspec".freeze, "script/bootstrap".freeze, "script/console".freeze, "test/appenders/test_async_flushing.rb".freeze, "test/appenders/test_buffered_io.rb".freeze, "test/appenders/test_console.rb".freeze, "test/appenders/test_file.rb".freeze, "test/appenders/test_io.rb".freeze, "test/appenders/test_rolling_file.rb".freeze, "test/appenders/test_string_io.rb".freeze, "test/appenders/test_syslog.rb".freeze, "test/benchmark.rb".freeze, "test/layouts/test_basic.rb".freeze, "test/layouts/test_color_pattern.rb".freeze, "test/layouts/test_json.rb".freeze, "test/layouts/test_pattern.rb".freeze, "test/layouts/test_yaml.rb".freeze, "test/performance.rb".freeze, "test/setup.rb".freeze, "test/test_appender.rb".freeze, "test/test_color_scheme.rb".freeze, "test/test_filter.rb".freeze, "test/test_layout.rb".freeze, "test/test_log_event.rb".freeze, "test/test_logger.rb".freeze, "test/test_logging.rb".freeze, "test/test_mapped_diagnostic_context.rb".freeze, "test/test_nested_diagnostic_context.rb".freeze, "test/test_proxy.rb".freeze, "test/test_repository.rb".freeze, "test/test_root_logger.rb".freeze, "test/test_utils.rb".freeze]
15
+ s.files = [".gitignore".freeze, ".travis.yml".freeze, "History.txt".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "examples/appenders.rb".freeze, "examples/classes.rb".freeze, "examples/colorization.rb".freeze, "examples/custom_log_levels.rb".freeze, "examples/fork.rb".freeze, "examples/formatting.rb".freeze, "examples/hierarchies.rb".freeze, "examples/layouts.rb".freeze, "examples/lazy.rb".freeze, "examples/loggers.rb".freeze, "examples/mdc.rb".freeze, "examples/names.rb".freeze, "examples/rails4.rb".freeze, "examples/reusing_layouts.rb".freeze, "examples/rspec_integration.rb".freeze, "examples/simple.rb".freeze, "lib/logging.rb".freeze, "lib/logging/appender.rb".freeze, "lib/logging/appenders.rb".freeze, "lib/logging/appenders/buffering.rb".freeze, "lib/logging/appenders/console.rb".freeze, "lib/logging/appenders/file.rb".freeze, "lib/logging/appenders/io.rb".freeze, "lib/logging/appenders/rolling_file.rb".freeze, "lib/logging/appenders/string_io.rb".freeze, "lib/logging/appenders/syslog.rb".freeze, "lib/logging/color_scheme.rb".freeze, "lib/logging/diagnostic_context.rb".freeze, "lib/logging/filter.rb".freeze, "lib/logging/filters.rb".freeze, "lib/logging/filters/level.rb".freeze, "lib/logging/layout.rb".freeze, "lib/logging/layouts.rb".freeze, "lib/logging/layouts/basic.rb".freeze, "lib/logging/layouts/parseable.rb".freeze, "lib/logging/layouts/pattern.rb".freeze, "lib/logging/log_event.rb".freeze, "lib/logging/logger.rb".freeze, "lib/logging/proxy.rb".freeze, "lib/logging/rails_compat.rb".freeze, "lib/logging/repository.rb".freeze, "lib/logging/root_logger.rb".freeze, "lib/logging/utils.rb".freeze, "lib/logging/version.rb".freeze, "lib/rspec/logging_helper.rb".freeze, "lib/spec/logging_helper.rb".freeze, "logging.gemspec".freeze, "script/bootstrap".freeze, "script/console".freeze, "test/appenders/test_async_flushing.rb".freeze, "test/appenders/test_buffered_io.rb".freeze, "test/appenders/test_console.rb".freeze, "test/appenders/test_file.rb".freeze, "test/appenders/test_io.rb".freeze, "test/appenders/test_rolling_file.rb".freeze, "test/appenders/test_string_io.rb".freeze, "test/appenders/test_syslog.rb".freeze, "test/benchmark.rb".freeze, "test/layouts/test_basic.rb".freeze, "test/layouts/test_color_pattern.rb".freeze, "test/layouts/test_json.rb".freeze, "test/layouts/test_nested_exceptions.rb".freeze, "test/layouts/test_pattern.rb".freeze, "test/layouts/test_yaml.rb".freeze, "test/performance.rb".freeze, "test/setup.rb".freeze, "test/test_appender.rb".freeze, "test/test_color_scheme.rb".freeze, "test/test_filter.rb".freeze, "test/test_layout.rb".freeze, "test/test_log_event.rb".freeze, "test/test_logger.rb".freeze, "test/test_logging.rb".freeze, "test/test_mapped_diagnostic_context.rb".freeze, "test/test_nested_diagnostic_context.rb".freeze, "test/test_proxy.rb".freeze, "test/test_repository.rb".freeze, "test/test_root_logger.rb".freeze, "test/test_utils.rb".freeze]
16
16
  s.homepage = "http://rubygems.org/gems/logging".freeze
17
17
  s.rdoc_options = ["--main".freeze, "README.md".freeze]
18
- s.rubyforge_project = "logging".freeze
19
- s.rubygems_version = "2.5.2".freeze
18
+ s.rubygems_version = "3.0.1".freeze
20
19
  s.summary = "A flexible and extendable logging library for Ruby".freeze
21
- s.test_files = ["test/appenders/test_async_flushing.rb".freeze, "test/appenders/test_buffered_io.rb".freeze, "test/appenders/test_console.rb".freeze, "test/appenders/test_file.rb".freeze, "test/appenders/test_io.rb".freeze, "test/appenders/test_rolling_file.rb".freeze, "test/appenders/test_string_io.rb".freeze, "test/appenders/test_syslog.rb".freeze, "test/layouts/test_basic.rb".freeze, "test/layouts/test_color_pattern.rb".freeze, "test/layouts/test_json.rb".freeze, "test/layouts/test_pattern.rb".freeze, "test/layouts/test_yaml.rb".freeze, "test/test_appender.rb".freeze, "test/test_color_scheme.rb".freeze, "test/test_filter.rb".freeze, "test/test_layout.rb".freeze, "test/test_log_event.rb".freeze, "test/test_logger.rb".freeze, "test/test_logging.rb".freeze, "test/test_mapped_diagnostic_context.rb".freeze, "test/test_nested_diagnostic_context.rb".freeze, "test/test_proxy.rb".freeze, "test/test_repository.rb".freeze, "test/test_root_logger.rb".freeze, "test/test_utils.rb".freeze]
20
+ s.test_files = ["test/appenders/test_async_flushing.rb".freeze, "test/appenders/test_buffered_io.rb".freeze, "test/appenders/test_console.rb".freeze, "test/appenders/test_file.rb".freeze, "test/appenders/test_io.rb".freeze, "test/appenders/test_rolling_file.rb".freeze, "test/appenders/test_string_io.rb".freeze, "test/appenders/test_syslog.rb".freeze, "test/layouts/test_basic.rb".freeze, "test/layouts/test_color_pattern.rb".freeze, "test/layouts/test_json.rb".freeze, "test/layouts/test_nested_exceptions.rb".freeze, "test/layouts/test_pattern.rb".freeze, "test/layouts/test_yaml.rb".freeze, "test/test_appender.rb".freeze, "test/test_color_scheme.rb".freeze, "test/test_filter.rb".freeze, "test/test_layout.rb".freeze, "test/test_log_event.rb".freeze, "test/test_logger.rb".freeze, "test/test_logging.rb".freeze, "test/test_mapped_diagnostic_context.rb".freeze, "test/test_nested_diagnostic_context.rb".freeze, "test/test_proxy.rb".freeze, "test/test_repository.rb".freeze, "test/test_root_logger.rb".freeze, "test/test_utils.rb".freeze]
22
21
 
23
22
  if s.respond_to? :specification_version then
24
23
  s.specification_version = 4
25
24
 
26
25
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
26
  s.add_runtime_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
28
- s.add_runtime_dependency(%q<multi_json>.freeze, ["~> 1.10"])
29
- s.add_development_dependency(%q<test-unit>.freeze, ["~> 3.1"])
27
+ s.add_runtime_dependency(%q<multi_json>.freeze, ["~> 1.14"])
28
+ s.add_development_dependency(%q<test-unit>.freeze, ["~> 3.3"])
30
29
  s.add_development_dependency(%q<bones-git>.freeze, ["~> 1.3"])
31
30
  s.add_development_dependency(%q<bones>.freeze, [">= 3.8.4"])
32
31
  else
33
32
  s.add_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
34
- s.add_dependency(%q<multi_json>.freeze, ["~> 1.10"])
35
- s.add_dependency(%q<test-unit>.freeze, ["~> 3.1"])
33
+ s.add_dependency(%q<multi_json>.freeze, ["~> 1.14"])
34
+ s.add_dependency(%q<test-unit>.freeze, ["~> 3.3"])
36
35
  s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
37
36
  s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
38
37
  end
39
38
  else
40
39
  s.add_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
41
- s.add_dependency(%q<multi_json>.freeze, ["~> 1.10"])
42
- s.add_dependency(%q<test-unit>.freeze, ["~> 3.1"])
40
+ s.add_dependency(%q<multi_json>.freeze, ["~> 1.14"])
41
+ s.add_dependency(%q<test-unit>.freeze, ["~> 3.3"])
43
42
  s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
44
43
  s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
45
44
  end
@@ -194,24 +194,22 @@ module TestAppenders
194
194
  assert_nil(readline)
195
195
  end
196
196
 
197
- if Object.const_defined?(:Encoding)
198
- def test_force_encoding
199
- a = 'ümlaut'
200
- b = 'hello ümlaut'.force_encoding('BINARY')
201
-
202
- event_a = Logging::LogEvent.new('TestLogger', @levels['info'], a, false)
203
- event_b = Logging::LogEvent.new('TestLogger', @levels['info'], b, false)
204
-
205
- @appender.append event_a
206
- @appender.append event_b
207
- assert_nil(readline)
208
-
209
- @appender.append event_a
210
- assert_equal " INFO TestLogger : #{a}\n", readline
211
- assert_equal " INFO TestLogger : #{b.force_encoding('UTF-8')}\n", readline
212
- assert_equal " INFO TestLogger : #{a}\n", readline
213
- assert_nil(readline)
214
- end
197
+ def test_force_encoding
198
+ a = 'ümlaut'
199
+ b = 'hello ümlaut'.force_encoding('BINARY')
200
+
201
+ event_a = Logging::LogEvent.new('TestLogger', @levels['info'], a, false)
202
+ event_b = Logging::LogEvent.new('TestLogger', @levels['info'], b, false)
203
+
204
+ @appender.append event_a
205
+ @appender.append event_b
206
+ assert_nil(readline)
207
+
208
+ @appender.append event_a
209
+ assert_equal " INFO TestLogger : #{a}\n", readline
210
+ assert_equal " INFO TestLogger : #{b.force_encoding('UTF-8')}\n", readline
211
+ assert_equal " INFO TestLogger : #{a}\n", readline
212
+ assert_nil(readline)
215
213
  end
216
214
 
217
215
  private
@@ -20,11 +20,10 @@ module TestAppenders
20
20
 
21
21
  appender = Logging.appenders.stdout
22
22
  assert_equal 'stdout', appender.name
23
- assert_same STDOUT, appender.instance_variable_get(:@io)
24
23
 
25
- appender.close
26
- assert_equal true, appender.closed?
27
- assert_equal false, STDOUT.closed?
24
+ io = appender.instance_variable_get(:@io)
25
+ assert_same STDOUT, io
26
+ assert_equal STDOUT.fileno, io.fileno
28
27
 
29
28
  appender = Logging.appenders.stdout('foo')
30
29
  assert_equal 'foo', appender.name
@@ -38,7 +37,26 @@ module TestAppenders
38
37
  assert_equal 3, appender.level
39
38
  end
40
39
 
41
- end # class TestStdout
40
+ def test_reopen
41
+ Logging::Repository.instance
42
+
43
+ appender = Logging.appenders.stdout
44
+ io = appender.instance_variable_get(:@io)
45
+
46
+ appender.close
47
+ assert appender.closed?
48
+ refute io.closed?
49
+ refute STDOUT.closed?
50
+
51
+ appender.reopen
52
+ refute appender.closed?
53
+
54
+ new_io = appender.instance_variable_get(:@io)
55
+ assert_same io, new_io
56
+ refute new_io.closed?
57
+ refute io.closed?
58
+ end
59
+ end
42
60
 
43
61
  class TestStderr < Test::Unit::TestCase
44
62
  include LoggingTestCase
@@ -48,11 +66,10 @@ module TestAppenders
48
66
 
49
67
  appender = Logging.appenders.stderr
50
68
  assert_equal 'stderr', appender.name
51
- assert_same STDERR, appender.instance_variable_get(:@io)
52
69
 
53
- appender.close
54
- assert_equal true, appender.closed?
55
- assert_equal false, STDERR.closed?
70
+ io = appender.instance_variable_get(:@io)
71
+ assert_same STDERR, io
72
+ assert_equal STDERR.fileno, io.fileno
56
73
 
57
74
  appender = Logging.appenders.stderr('foo')
58
75
  assert_equal 'foo', appender.name
@@ -66,8 +83,26 @@ module TestAppenders
66
83
  assert_equal 3, appender.level
67
84
  end
68
85
 
69
- end # class TestStderr
86
+ def test_reopen
87
+ Logging::Repository.instance
70
88
 
71
- end # module TestAppenders
72
- end # module TestLogging
89
+ appender = Logging.appenders.stderr
90
+ io = appender.instance_variable_get(:@io)
91
+
92
+ appender.close
93
+ assert appender.closed?
94
+ refute io.closed?
95
+ refute STDERR.closed?
96
+
97
+ appender.reopen
98
+ refute appender.closed?
99
+
100
+ new_io = appender.instance_variable_get(:@io)
101
+ assert_same io, new_io
102
+ refute new_io.closed?
103
+ refute io.closed?
104
+ end
105
+ end
106
+ end
107
+ end
73
108
 
@@ -14,10 +14,10 @@ module TestAppenders
14
14
  super
15
15
  Logging.init
16
16
 
17
- FileUtils.mkdir [File.join(TMP, 'dir'), File.join(TMP, 'uw_dir')]
18
- FileUtils.chmod 0555, File.join(TMP, 'uw_dir')
19
- FileUtils.touch File.join(TMP, 'uw_file')
20
- FileUtils.chmod 0444, File.join(TMP, 'uw_file')
17
+ FileUtils.mkdir [File.join(@tmpdir, 'dir'), File.join(@tmpdir, 'uw_dir')]
18
+ FileUtils.chmod 0555, File.join(@tmpdir, 'uw_dir')
19
+ FileUtils.touch File.join(@tmpdir, 'uw_file')
20
+ FileUtils.chmod 0444, File.join(@tmpdir, 'uw_file')
21
21
  end
22
22
 
23
23
  def test_factory_method_validates_input
@@ -27,27 +27,27 @@ module TestAppenders
27
27
  end
28
28
 
29
29
  def test_class_assert_valid_logfile
30
- log = File.join(TMP, 'uw_dir', 'file.log')
30
+ log = File.join(@tmpdir, 'uw_dir', 'file.log')
31
31
  assert_raise(ArgumentError) do
32
32
  Logging.appenders.file(log).class.assert_valid_logfile(log)
33
33
  end
34
34
 
35
- log = File.join(TMP, 'dir')
35
+ log = File.join(@tmpdir, 'dir')
36
36
  assert_raise(ArgumentError) do
37
37
  Logging.appenders.file(log).class.assert_valid_logfile(log)
38
38
  end
39
39
 
40
- log = File.join(TMP, 'uw_file')
40
+ log = File.join(@tmpdir, 'uw_file')
41
41
  assert_raise(ArgumentError) do
42
42
  Logging.appenders.file(log).class.assert_valid_logfile(log)
43
43
  end
44
44
 
45
- log = File.join(TMP, 'file.log')
45
+ log = File.join(@tmpdir, 'file.log')
46
46
  assert Logging.appenders.file(log).class.assert_valid_logfile(log)
47
47
  end
48
48
 
49
49
  def test_initialize
50
- log = File.join(TMP, 'file.log')
50
+ log = File.join(@tmpdir, 'file.log')
51
51
  appender = Logging.appenders.file(NAME, :filename => log)
52
52
  assert_equal 'logfile', appender.name
53
53
  assert_equal ::File.expand_path(log), appender.filename
@@ -87,7 +87,7 @@ module TestAppenders
87
87
  end
88
88
 
89
89
  def test_changing_directories
90
- log = File.join(TMP, 'file.log')
90
+ log = File.join(@tmpdir, 'file.log')
91
91
  appender = Logging.appenders.file(NAME, :filename => log)
92
92
 
93
93
  assert_equal 'logfile', appender.name
@@ -95,29 +95,45 @@ module TestAppenders
95
95
 
96
96
  begin
97
97
  pwd = Dir.pwd
98
- Dir.chdir TMP
98
+ Dir.chdir @tmpdir
99
99
  assert_nothing_raised { appender.reopen }
100
100
  ensure
101
101
  Dir.chdir pwd
102
102
  end
103
103
  end
104
104
 
105
- if Object.const_defined? :Encoding
105
+ def test_encoding
106
+ log = File.join(@tmpdir, 'file-encoding.log')
107
+ appender = Logging.appenders.file(NAME, :filename => log, :encoding => 'ASCII')
106
108
 
107
- def test_encoding
108
- log = File.join(TMP, 'file-encoding.log')
109
- appender = Logging.appenders.file(NAME, :filename => log, :encoding => 'ASCII')
109
+ appender << "A normal line of text\n"
110
+ appender << "ümlaut\n"
111
+ appender.close
110
112
 
111
- appender << "A normal line of text\n"
112
- appender << "ümlaut\n"
113
- appender.close
113
+ lines = File.readlines(log, :encoding => 'UTF-8')
114
+ assert_equal "A normal line of text\n", lines[0]
115
+ assert_equal "ümlaut\n", lines[1]
114
116
 
115
- lines = File.readlines(log, :encoding => 'UTF-8')
116
- assert_equal "A normal line of text\n", lines[0]
117
- assert_equal "ümlaut\n", lines[1]
117
+ cleanup
118
+ end
119
+
120
+ def test_reopening_should_not_truncate_the_file
121
+ log = File.join(@tmpdir, 'truncate.log')
122
+ appender = Logging.appenders.file(NAME, filename: log, truncate: true)
123
+
124
+ appender << "This will be the first line\n"
125
+ appender << "This will be the second line\n"
126
+ appender << "This will be the third line\n"
127
+ appender.reopen
118
128
 
119
- cleanup
129
+ File.open(log, 'r') do |file|
130
+ assert_equal "This will be the first line\n", file.readline
131
+ assert_equal "This will be the second line\n", file.readline
132
+ assert_equal "This will be the third line\n", file.readline
133
+ assert_raise(EOFError) {file.readline}
120
134
  end
135
+
136
+ cleanup
121
137
  end
122
138
 
123
139
  private
@@ -127,7 +143,6 @@ module TestAppenders
127
143
  Logging.appenders[NAME] = nil
128
144
  end
129
145
  end
130
-
131
146
  end # TestFile
132
147
 
133
148
  end # TestAppenders
@@ -13,9 +13,9 @@ module TestAppenders
13
13
  super
14
14
  Logging.init
15
15
 
16
- @fn = File.expand_path('test.log', TMP)
17
- @fn_fmt = File.expand_path('test.%d.log', TMP)
18
- @glob = File.expand_path('*.log', TMP)
16
+ @fn = File.expand_path('test.log', @tmpdir)
17
+ @fn_fmt = File.expand_path('test.%d.log', @tmpdir)
18
+ @glob = File.expand_path('*.log', @tmpdir)
19
19
  end
20
20
 
21
21
  def test_factory_method_validates_input
@@ -93,8 +93,8 @@ module TestAppenders
93
93
  end
94
94
 
95
95
  def test_age
96
- d_glob = File.join(TMP, 'test.*.log')
97
- dt_glob = File.join(TMP, 'test.*-*.log')
96
+ d_glob = File.join(@tmpdir, 'test.*.log')
97
+ dt_glob = File.join(@tmpdir, 'test.*-*.log')
98
98
  age_fn = @fn + '.age'
99
99
 
100
100
  assert_equal [], Dir.glob(@glob)
@@ -205,7 +205,7 @@ module TestAppenders
205
205
 
206
206
  begin
207
207
  pwd = Dir.pwd
208
- Dir.chdir TMP
208
+ Dir.chdir @tmpdir
209
209
 
210
210
  ap << 'X' * 100; ap.flush
211
211
  assert_equal 1, Dir.glob(@glob).length
@@ -249,9 +249,9 @@ module TestAppenders
249
249
  end
250
250
 
251
251
  def test_custom_numberd_filename
252
- fn = File.expand_path('test.log{{.%d}}', TMP)
253
- filename = File.expand_path('test.log', TMP)
254
- glob = File.expand_path('test.log.*', TMP)
252
+ fn = File.expand_path('test.log{{.%d}}', @tmpdir)
253
+ filename = File.expand_path('test.log', @tmpdir)
254
+ glob = File.expand_path('test.log.*', @tmpdir)
255
255
 
256
256
  assert_equal [], Dir.glob(glob)
257
257
  ap = Logging.appenders.rolling_file(NAME, :filename => fn, :size => 100, :keep => 2)
@@ -285,10 +285,10 @@ module TestAppenders
285
285
  end
286
286
 
287
287
  def test_custom_timestamp_filename
288
- fn = File.expand_path('test{{.%S:%M}}.log', TMP)
289
- filename = File.expand_path('test.log', TMP)
288
+ fn = File.expand_path('test{{.%S:%M}}.log', @tmpdir)
289
+ filename = File.expand_path('test.log', @tmpdir)
290
290
  age_file = filename + '.age'
291
- glob = File.expand_path('test.*.log', TMP)
291
+ glob = File.expand_path('test.*.log', @tmpdir)
292
292
 
293
293
  assert_equal [], Dir.glob(glob)
294
294
  ap = Logging.appenders.rolling_file(NAME, :filename => fn, :age => 1, :keep => 2)
@@ -78,7 +78,7 @@ module TestLayouts
78
78
  'log message', false)
79
79
  event.file = 'test_file.rb'
80
80
  event.line = 123
81
- event.method = 'method_name'
81
+ event.method_name = 'method_name'
82
82
 
83
83
  @layout.items = %w[logger]
84
84
  assert_equal %Q[{"logger":"TestLogger"}\n], @layout.format(event)
@@ -19,7 +19,7 @@ module TestLogging
19
19
  end
20
20
 
21
21
  layout = Logging.layouts.basic({})
22
- log = layout.format_obj(e)
22
+ log = layout.format_obj(err)
23
23
  assert_not_nil log.index('<StandardError> root exception')
24
24
 
25
25
  if err.respond_to?(:cause)
@@ -45,7 +45,7 @@ module TestLogging
45
45
  end
46
46
 
47
47
  layout = Logging.layouts.basic(cause_depth: 1)
48
- log = layout.format_obj(e)
48
+ log = layout.format_obj(err)
49
49
  assert_not_nil log.index('<StandardError> root exception')
50
50
 
51
51
  if err.respond_to?(:cause)
@@ -68,12 +68,12 @@ module TestLogging
68
68
  end
69
69
 
70
70
  layout = Logging.layouts.parseable.new
71
- log = layout.format_obj(e)
71
+ log = layout.format_obj(err)
72
72
  assert_equal 'StandardError', log[:class]
73
73
  assert_equal 'root exception', log[:message]
74
74
  assert log[:backtrace].size > 0
75
75
 
76
- if e.respond_to?(:cause)
76
+ if err.respond_to?(:cause)
77
77
  assert_not_nil log[:cause]
78
78
 
79
79
  log = log[:cause]
@@ -101,13 +101,13 @@ module TestLogging
101
101
  end
102
102
 
103
103
  layout = Logging.layouts.parseable.new(cause_depth: 1)
104
- log = layout.format_obj(e)
104
+ log = layout.format_obj(err)
105
105
 
106
106
  assert_equal 'StandardError', log[:class]
107
107
  assert_equal 'root exception', log[:message]
108
108
  assert log[:backtrace].size > 0
109
109
 
110
- if e.respond_to?(:cause)
110
+ if err.respond_to?(:cause)
111
111
  assert_not_nil log[:cause]
112
112
 
113
113
  log = log[:cause]