logging 2.0.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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,6 +1,3 @@
1
-
2
- if defined? ActiveSupport
3
-
4
1
  module Logging
5
2
 
6
3
  # Rails compatibility module.
@@ -9,10 +6,9 @@ module Logging
9
6
  # some Rails extensions expect these methods to exist. Those methods are
10
7
  # implemented in this module and included in the Logging::Logger class when
11
8
  # the ActiveSupport gem is present.
12
- #
13
9
  module RailsCompat
14
10
 
15
- # A no-op implementation of the +formatter+ method.
11
+ # A no-op implementation of the `formatter` method.
16
12
  def formatter; end
17
13
 
18
14
  # A no-op implementation of the +silence+ method. Setting of log levels
@@ -20,17 +16,12 @@ module Logging
20
16
  # opinion that overriding the log level programmatically is a logical
21
17
  # error.
22
18
  #
23
- # Please see https://github.com/TwP/logging/issues/11 for a more detail
19
+ # Please see https://github.com/TwP/logging/issues/11 for a more detailed
24
20
  # discussion of the issue.
25
- #
26
21
  def silence( *args )
27
22
  yield self
28
23
  end
29
-
30
- end # RailsCompat
24
+ end
31
25
 
32
26
  Logger.send :include, RailsCompat
33
-
34
- end # Logging
35
- end # if defined?
36
-
27
+ end
@@ -1,5 +1,5 @@
1
1
  module Logging
2
- VERSION = "2.0.0".freeze
2
+ VERSION = "2.3.0".freeze
3
3
 
4
4
  # Returns the version string for the library.
5
5
  def self.version
@@ -1,46 +1,45 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: logging 2.0.0 ruby lib
2
+ # stub: logging 2.3.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "logging"
6
- s.version = "2.0.0"
5
+ s.name = "logging".freeze
6
+ s.version = "2.3.0"
7
7
 
8
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
- s.require_paths = ["lib"]
10
- s.authors = ["Tim Pease"]
11
- s.date = "2015-03-29"
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."
13
- s.email = "tim.pease@gmail.com"
14
- s.extra_rdoc_files = ["History.txt"]
15
- s.files = [".gitignore", ".travis.yml", "History.txt", "README.md", "Rakefile", "examples/appenders.rb", "examples/classes.rb", "examples/colorization.rb", "examples/custom_log_levels.rb", "examples/fork.rb", "examples/formatting.rb", "examples/hierarchies.rb", "examples/layouts.rb", "examples/lazy.rb", "examples/loggers.rb", "examples/mdc.rb", "examples/names.rb", "examples/rspec_integration.rb", "examples/simple.rb", "lib/logging.rb", "lib/logging/appender.rb", "lib/logging/appenders.rb", "lib/logging/appenders/buffering.rb", "lib/logging/appenders/console.rb", "lib/logging/appenders/file.rb", "lib/logging/appenders/io.rb", "lib/logging/appenders/rolling_file.rb", "lib/logging/appenders/string_io.rb", "lib/logging/appenders/syslog.rb", "lib/logging/color_scheme.rb", "lib/logging/diagnostic_context.rb", "lib/logging/filter.rb", "lib/logging/filters.rb", "lib/logging/filters/level.rb", "lib/logging/layout.rb", "lib/logging/layouts.rb", "lib/logging/layouts/basic.rb", "lib/logging/layouts/parseable.rb", "lib/logging/layouts/pattern.rb", "lib/logging/log_event.rb", "lib/logging/logger.rb", "lib/logging/proxy.rb", "lib/logging/rails_compat.rb", "lib/logging/repository.rb", "lib/logging/root_logger.rb", "lib/logging/utils.rb", "lib/logging/version.rb", "lib/rspec/logging_helper.rb", "lib/spec/logging_helper.rb", "logging.gemspec", "script/bootstrap", "test/appenders/test_buffered_io.rb", "test/appenders/test_console.rb", "test/appenders/test_file.rb", "test/appenders/test_io.rb", "test/appenders/test_periodic_flushing.rb", "test/appenders/test_rolling_file.rb", "test/appenders/test_string_io.rb", "test/appenders/test_syslog.rb", "test/benchmark.rb", "test/layouts/test_basic.rb", "test/layouts/test_color_pattern.rb", "test/layouts/test_json.rb", "test/layouts/test_pattern.rb", "test/layouts/test_yaml.rb", "test/setup.rb", "test/test_appender.rb", "test/test_color_scheme.rb", "test/test_filter.rb", "test/test_layout.rb", "test/test_log_event.rb", "test/test_logger.rb", "test/test_logging.rb", "test/test_mapped_diagnostic_context.rb", "test/test_nested_diagnostic_context.rb", "test/test_proxy.rb", "test/test_repository.rb", "test/test_root_logger.rb", "test/test_utils.rb"]
16
- s.homepage = "http://rubygems.org/gems/logging"
17
- s.rdoc_options = ["--main", "README.md"]
18
- s.rubyforge_project = "logging"
19
- s.rubygems_version = "2.2.2"
20
- s.summary = "A flexible and extendable logging library for Ruby"
21
- s.test_files = ["test/appenders/test_buffered_io.rb", "test/appenders/test_console.rb", "test/appenders/test_file.rb", "test/appenders/test_io.rb", "test/appenders/test_periodic_flushing.rb", "test/appenders/test_rolling_file.rb", "test/appenders/test_string_io.rb", "test/appenders/test_syslog.rb", "test/layouts/test_basic.rb", "test/layouts/test_color_pattern.rb", "test/layouts/test_json.rb", "test/layouts/test_pattern.rb", "test/layouts/test_yaml.rb", "test/test_appender.rb", "test/test_color_scheme.rb", "test/test_filter.rb", "test/test_layout.rb", "test/test_log_event.rb", "test/test_logger.rb", "test/test_logging.rb", "test/test_mapped_diagnostic_context.rb", "test/test_nested_diagnostic_context.rb", "test/test_proxy.rb", "test/test_repository.rb", "test/test_root_logger.rb", "test/test_utils.rb"]
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["Tim Pease".freeze]
11
+ s.date = "2020-07-04"
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
+ s.email = "tim.pease@gmail.com".freeze
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_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
+ s.homepage = "http://rubygems.org/gems/logging".freeze
17
+ s.rdoc_options = ["--main".freeze, "README.md".freeze]
18
+ s.rubygems_version = "3.0.1".freeze
19
+ s.summary = "A flexible and extendable logging library for Ruby".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
- s.add_runtime_dependency(%q<little-plugger>, ["~> 1.1"])
28
- s.add_runtime_dependency(%q<multi_json>, ["~> 1.10"])
29
- s.add_development_dependency(%q<flexmock>, ["~> 1.0"])
30
- s.add_development_dependency(%q<bones-git>, ["~> 1.3"])
31
- s.add_development_dependency(%q<bones>, [">= 3.8.3"])
26
+ s.add_runtime_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
27
+ s.add_runtime_dependency(%q<multi_json>.freeze, ["~> 1.14"])
28
+ s.add_development_dependency(%q<test-unit>.freeze, ["~> 3.3"])
29
+ s.add_development_dependency(%q<bones-git>.freeze, ["~> 1.3"])
30
+ s.add_development_dependency(%q<bones>.freeze, [">= 3.8.4"])
32
31
  else
33
- s.add_dependency(%q<little-plugger>, ["~> 1.1"])
34
- s.add_dependency(%q<multi_json>, ["~> 1.10"])
35
- s.add_dependency(%q<flexmock>, ["~> 1.0"])
36
- s.add_dependency(%q<bones-git>, ["~> 1.3"])
37
- s.add_dependency(%q<bones>, [">= 3.8.3"])
32
+ s.add_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
33
+ s.add_dependency(%q<multi_json>.freeze, ["~> 1.14"])
34
+ s.add_dependency(%q<test-unit>.freeze, ["~> 3.3"])
35
+ s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
36
+ s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
38
37
  end
39
38
  else
40
- s.add_dependency(%q<little-plugger>, ["~> 1.1"])
41
- s.add_dependency(%q<multi_json>, ["~> 1.10"])
42
- s.add_dependency(%q<flexmock>, ["~> 1.0"])
43
- s.add_dependency(%q<bones-git>, ["~> 1.3"])
44
- s.add_dependency(%q<bones>, [">= 3.8.3"])
39
+ s.add_dependency(%q<little-plugger>.freeze, ["~> 1.1"])
40
+ s.add_dependency(%q<multi_json>.freeze, ["~> 1.14"])
41
+ s.add_dependency(%q<test-unit>.freeze, ["~> 3.3"])
42
+ s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
43
+ s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
45
44
  end
46
45
  end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require "irb"
3
+ require "rubygems"
4
+
5
+ $LOAD_PATH.unshift "lib"
6
+ require "logging"
7
+
8
+ IRB.start
@@ -4,14 +4,15 @@ require File.expand_path('../setup', File.dirname(__FILE__))
4
4
  module TestLogging
5
5
  module TestAppenders
6
6
 
7
- class TestPeriodicFlushing < Test::Unit::TestCase
7
+ class TestAsyncFlushing < Test::Unit::TestCase
8
8
  include LoggingTestCase
9
9
 
10
10
  def setup
11
11
  super
12
- @appender = Logging.appenders.string_io(
13
- 'test_appender', :flush_period => 2
14
- )
12
+ @appender = Logging.appenders.string_io \
13
+ 'test_appender',
14
+ :flush_period => 2
15
+
15
16
  @appender.clear
16
17
  @sio = @appender.sio
17
18
  @levels = Logging::LEVELS
@@ -42,12 +43,12 @@ module TestAppenders
42
43
  assert_equal 200, @appender.auto_flushing
43
44
  end
44
45
 
45
- def test_periodic_flusher_running
46
- flusher = @appender.instance_variable_get(:@periodic_flusher)
47
- assert_instance_of Logging::Appenders::Buffering::PeriodicFlusher, flusher
46
+ def test_async_flusher_running
47
+ flusher = @appender.instance_variable_get(:@async_flusher)
48
+ assert_instance_of Logging::Appenders::Buffering::AsyncFlusher, flusher
48
49
 
49
50
  sleep 0.250 # give the flusher thread another moment to start
50
- assert flusher.waiting?, 'the periodic flusher should be waiting for a signal'
51
+ assert flusher.waiting?, 'the async flusher should be waiting for a signal'
51
52
  end
52
53
 
53
54
  def test_append
@@ -129,13 +130,65 @@ module TestAppenders
129
130
  assert_nil(readline)
130
131
  end
131
132
 
132
- private
133
- def readline
134
- @appender.readline
133
+ def test_setting_flush_period_to_nil
134
+ flusher = @appender.instance_variable_get(:@async_flusher)
135
+ assert_instance_of Logging::Appenders::Buffering::AsyncFlusher, flusher
136
+
137
+ @appender.flush_period = nil
138
+
139
+ assert_nil @appender.instance_variable_get(:@async_flusher)
135
140
  end
136
141
 
137
- end # class TestPeriodicFlushing
142
+ def test_setting_negative_flush_period
143
+ assert_raise(ArgumentError) { @appender.flush_period = -1 }
144
+ end
145
+
146
+ def test_async_writes
147
+ @appender.auto_flushing = 3
148
+ @appender.flush_period = nil
149
+ @appender.async = true
150
+
151
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
152
+ [1, 2, 3, 4], false)
153
+
154
+ flusher = @appender.instance_variable_get(:@async_flusher)
155
+ assert_instance_of Logging::Appenders::Buffering::AsyncFlusher, flusher
138
156
 
139
- end # module TestAppenders
140
- end # module TestLogging
157
+ @appender.append event
158
+ assert_nil(readline)
141
159
 
160
+ event.level = @levels['debug']
161
+ event.data = 'the big log message'
162
+ @appender.append event
163
+ sleep 0.250
164
+ assert_nil(readline)
165
+
166
+ event.level = @levels['info']
167
+ event.data = 'just FYI'
168
+ @appender.append event # might write here, might not
169
+ sleep 0.250 # so sleep a little to let the write occur
170
+
171
+ assert_equal " WARN TestLogger : <Array> #{[1, 2, 3, 4]}\n", readline
172
+ assert_equal "DEBUG TestLogger : the big log message\n", readline
173
+ assert_equal " INFO TestLogger : just FYI\n", readline
174
+
175
+ event.level = @levels['warn']
176
+ event.data = 'this is your last warning!'
177
+ @appender.append event
178
+ assert_nil(readline)
179
+
180
+ @appender.close_method = :close_write
181
+ @appender.close
182
+
183
+ assert_equal " WARN TestLogger : this is your last warning!\n", readline
184
+
185
+ assert_nil @appender.instance_variable_get(:@async_flusher)
186
+ end
187
+
188
+ private
189
+ def readline
190
+ @appender.readline
191
+ end
192
+ end
193
+ end
194
+ end
@@ -10,9 +10,12 @@ module TestAppenders
10
10
 
11
11
  def setup
12
12
  super
13
- @appender = Logging.appenders.string_io(
14
- 'test_appender', :auto_flushing => 3, :immediate_at => :error
15
- )
13
+ @appender = Logging.appenders.string_io \
14
+ 'test_appender',
15
+ :auto_flushing => 3,
16
+ :immediate_at => :error,
17
+ :encoding => 'UTF-8'
18
+
16
19
  @appender.clear
17
20
  @sio = @appender.sio
18
21
  @levels = Logging::LEVELS
@@ -191,24 +194,22 @@ module TestAppenders
191
194
  assert_nil(readline)
192
195
  end
193
196
 
194
- if Object.const_defined?(:Encoding)
195
- def test_force_encoding
196
- a = 'ümlaut'
197
- b = 'hello ümlaut'.force_encoding('BINARY')
197
+ def test_force_encoding
198
+ a = 'ümlaut'
199
+ b = 'hello ümlaut'.force_encoding('BINARY')
198
200
 
199
- event_a = Logging::LogEvent.new('TestLogger', @levels['info'], a, false)
200
- event_b = Logging::LogEvent.new('TestLogger', @levels['info'], b, false)
201
+ event_a = Logging::LogEvent.new('TestLogger', @levels['info'], a, false)
202
+ event_b = Logging::LogEvent.new('TestLogger', @levels['info'], b, false)
201
203
 
202
- @appender.append event_a
203
- @appender.append event_b
204
- assert_nil(readline)
204
+ @appender.append event_a
205
+ @appender.append event_b
206
+ assert_nil(readline)
205
207
 
206
- @appender.append event_a
207
- assert_equal " INFO TestLogger : #{a}\n", readline
208
- assert_equal " INFO TestLogger : #{b.force_encoding('UTF-8')}\n", readline
209
- assert_equal " INFO TestLogger : #{a}\n", readline
210
- assert_nil(readline)
211
- end
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)
212
213
  end
213
214
 
214
215
  private
@@ -4,6 +4,14 @@ require File.expand_path('../setup', File.dirname(__FILE__))
4
4
  module TestLogging
5
5
  module TestAppenders
6
6
 
7
+ class TestConsole < Test::Unit::TestCase
8
+ include LoggingTestCase
9
+
10
+ def test_initialize
11
+ assert_raise(RuntimeError) { Logging::Appenders::Console.new("test") }
12
+ end
13
+ end
14
+
7
15
  class TestStdout < Test::Unit::TestCase
8
16
  include LoggingTestCase
9
17
 
@@ -12,11 +20,10 @@ module TestAppenders
12
20
 
13
21
  appender = Logging.appenders.stdout
14
22
  assert_equal 'stdout', appender.name
15
- assert_same STDOUT, appender.instance_variable_get(:@io)
16
23
 
17
- appender.close
18
- assert_equal true, appender.closed?
19
- assert_equal false, STDOUT.closed?
24
+ io = appender.instance_variable_get(:@io)
25
+ refute_same STDOUT, io
26
+ assert_equal STDOUT.fileno, io.fileno
20
27
 
21
28
  appender = Logging.appenders.stdout('foo')
22
29
  assert_equal 'foo', appender.name
@@ -30,7 +37,26 @@ module TestAppenders
30
37
  assert_equal 3, appender.level
31
38
  end
32
39
 
33
- 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
+ assert io.closed?
49
+ refute STDOUT.closed?
50
+
51
+ appender.reopen
52
+ refute appender.closed?
53
+
54
+ new_io = appender.instance_variable_get(:@io)
55
+ refute_same io, new_io
56
+ refute new_io.closed?
57
+ assert io.closed?
58
+ end
59
+ end
34
60
 
35
61
  class TestStderr < Test::Unit::TestCase
36
62
  include LoggingTestCase
@@ -40,11 +66,10 @@ module TestAppenders
40
66
 
41
67
  appender = Logging.appenders.stderr
42
68
  assert_equal 'stderr', appender.name
43
- assert_same STDERR, appender.instance_variable_get(:@io)
44
69
 
45
- appender.close
46
- assert_equal true, appender.closed?
47
- assert_equal false, STDERR.closed?
70
+ io = appender.instance_variable_get(:@io)
71
+ refute_same STDERR, io
72
+ assert_same STDERR.fileno, io.fileno
48
73
 
49
74
  appender = Logging.appenders.stderr('foo')
50
75
  assert_equal 'foo', appender.name
@@ -58,8 +83,26 @@ module TestAppenders
58
83
  assert_equal 3, appender.level
59
84
  end
60
85
 
61
- end # class TestStderr
86
+ def test_reopen
87
+ Logging::Repository.instance
62
88
 
63
- end # module TestAppenders
64
- end # module TestLogging
89
+ appender = Logging.appenders.stderr
90
+ io = appender.instance_variable_get(:@io)
91
+
92
+ appender.close
93
+ assert appender.closed?
94
+ assert io.closed?
95
+ refute STDERR.closed?
96
+
97
+ appender.reopen
98
+ refute appender.closed?
99
+
100
+ new_io = appender.instance_variable_get(:@io)
101
+ refute_same io, new_io
102
+ refute new_io.closed?
103
+ assert io.closed?
104
+ end
105
+ end
106
+ end
107
+ end
65
108
 
@@ -14,34 +14,40 @@ 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
+ end
22
+
23
+ def test_factory_method_validates_input
24
+ assert_raise(ArgumentError) do
25
+ Logging.appenders.file
26
+ end
21
27
  end
22
28
 
23
29
  def test_class_assert_valid_logfile
24
- log = File.join(TMP, 'uw_dir', 'file.log')
30
+ log = File.join(@tmpdir, 'uw_dir', 'file.log')
25
31
  assert_raise(ArgumentError) do
26
- Logging.appenders.file.assert_valid_logfile(log)
32
+ Logging.appenders.file(log).class.assert_valid_logfile(log)
27
33
  end
28
34
 
29
- log = File.join(TMP, 'dir')
35
+ log = File.join(@tmpdir, 'dir')
30
36
  assert_raise(ArgumentError) do
31
- Logging.appenders.file.assert_valid_logfile(log)
37
+ Logging.appenders.file(log).class.assert_valid_logfile(log)
32
38
  end
33
39
 
34
- log = File.join(TMP, 'uw_file')
40
+ log = File.join(@tmpdir, 'uw_file')
35
41
  assert_raise(ArgumentError) do
36
- Logging.appenders.file.assert_valid_logfile(log)
42
+ Logging.appenders.file(log).class.assert_valid_logfile(log)
37
43
  end
38
44
 
39
- log = File.join(TMP, 'file.log')
40
- assert Logging.appenders.file.assert_valid_logfile(log)
45
+ log = File.join(@tmpdir, 'file.log')
46
+ assert Logging.appenders.file(log).class.assert_valid_logfile(log)
41
47
  end
42
48
 
43
49
  def test_initialize
44
- log = File.join(TMP, 'file.log')
50
+ log = File.join(@tmpdir, 'file.log')
45
51
  appender = Logging.appenders.file(NAME, :filename => log)
46
52
  assert_equal 'logfile', appender.name
47
53
  assert_equal ::File.expand_path(log), appender.filename
@@ -81,7 +87,7 @@ module TestAppenders
81
87
  end
82
88
 
83
89
  def test_changing_directories
84
- log = File.join(TMP, 'file.log')
90
+ log = File.join(@tmpdir, 'file.log')
85
91
  appender = Logging.appenders.file(NAME, :filename => log)
86
92
 
87
93
  assert_equal 'logfile', appender.name
@@ -89,30 +95,45 @@ module TestAppenders
89
95
 
90
96
  begin
91
97
  pwd = Dir.pwd
92
- Dir.chdir TMP
98
+ Dir.chdir @tmpdir
93
99
  assert_nothing_raised { appender.reopen }
94
100
  ensure
95
101
  Dir.chdir pwd
96
102
  end
97
103
  end
98
104
 
99
- 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')
108
+
109
+ appender << "A normal line of text\n"
110
+ appender << "ümlaut\n"
111
+ appender.close
100
112
 
101
- def test_encoding
102
- log = File.join(TMP, 'file-encoding.log')
103
- #appender = Logging.appenders.file(NAME, :filename => log, :encoding => 'ISO-8859-16')
104
- appender = Logging.appenders.file(NAME, :filename => log, :encoding => 'ASCII')
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]
105
116
 
106
- appender << "A normal line of text\n"
107
- appender << "ümlaut\n"
108
- appender.close
117
+ cleanup
118
+ end
109
119
 
110
- lines = File.readlines(log)
111
- assert_equal "A normal line of text\n", lines[0]
112
- assert_equal "ümlaut\n", lines[1]
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)
113
123
 
114
- cleanup
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
128
+
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}
115
134
  end
135
+
136
+ cleanup
116
137
  end
117
138
 
118
139
  private
@@ -122,7 +143,6 @@ module TestAppenders
122
143
  Logging.appenders[NAME] = nil
123
144
  end
124
145
  end
125
-
126
146
  end # TestFile
127
147
 
128
148
  end # TestAppenders