logging 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,7 +41,8 @@ class Layout
41
41
  when :inspect, :yaml, :json; f
42
42
  else :string end
43
43
 
44
- self.backtrace = opts.fetch(:backtrace, ::Logging.backtrace)
44
+ self.backtrace = opts.fetch(:backtrace, ::Logging.backtrace)
45
+ self.utc_offset = opts.fetch(:utc_offset, ::Logging.utc_offset)
45
46
  end
46
47
 
47
48
  # call-seq:
@@ -63,6 +64,50 @@ class Layout
63
64
  attr_reader :backtrace
64
65
  alias :backtrace? :backtrace
65
66
 
67
+ # Set the UTC offset used when formatting time values. If left unset, the
68
+ # default local time zone will be used for time values. This method accepts
69
+ # the `utc_offset` format supported by the `Time#localtime` method in Ruby.
70
+ #
71
+ # Passing "UTC" or `0` as the UTC offset will cause all times to be reported
72
+ # in the UTC timezone.
73
+ #
74
+ # layout.utc_offset = "-07:00" # Mountain Standard Time in North America
75
+ # layout.utc_offset = "+01:00" # Central European Time
76
+ # layout.utc_offset = "UTC" # UTC
77
+ # layout.utc_offset = 0 # UTC
78
+ #
79
+ def utc_offset=( value )
80
+ @utc_offset = case value
81
+ when nil; nil
82
+ when "UTC", "GMT", 0; 0
83
+ else
84
+ Time.now.localtime(value)
85
+ value
86
+ end
87
+ end
88
+
89
+ # Returns the UTC offset.
90
+ attr_reader :utc_offset
91
+
92
+ # Internal: Helper method that applies the UTC offset to the given `time`
93
+ # instance. A new Time is returned that is equivalent to the original `time`
94
+ # but pinned to the timezone given by the UTC offset.
95
+ #
96
+ # If a UTC offset has not been set, then the original `time` instance is
97
+ # returned unchanged.
98
+ #
99
+ def apply_utc_offset( time )
100
+ return time if utc_offset.nil?
101
+
102
+ time = time.dup
103
+ if utc_offset == 0
104
+ time.utc
105
+ else
106
+ time.localtime(utc_offset)
107
+ end
108
+ time
109
+ end
110
+
66
111
  # call-seq:
67
112
  # format( event )
68
113
  #
@@ -177,8 +177,9 @@ module Logging::Layouts
177
177
  #
178
178
  # Creates a new Parseable layout using the following options:
179
179
  #
180
- # :style => :json or :yaml
181
- # :items => %w[timestamp level logger message]
180
+ # :style => :json or :yaml
181
+ # :items => %w[timestamp level logger message]
182
+ # :utc_offset => "-06:00" or -21600 or "UTC"
182
183
  #
183
184
  def initialize( opts = {} )
184
185
  super
@@ -241,9 +242,11 @@ module Logging::Layouts
241
242
  else raise ArgumentError, "unknown format style '#@style'" end
242
243
  end
243
244
 
244
- # Convert the given time _value_ into an ISO8601 formatted time string.
245
+ # Convert the given `time` into an ISO8601 formatted time string.
245
246
  #
246
- def iso8601_format( value )
247
+ def iso8601_format( time )
248
+ value = apply_utc_offset(time)
249
+
247
250
  str = value.strftime('%Y-%m-%dT%H:%M:%S')
248
251
  str << ('.%06d' % value.usec)
249
252
 
@@ -164,6 +164,7 @@ module Logging::Layouts
164
164
  def self.create_date_format_methods( pl )
165
165
  code = "undef :format_date if method_defined? :format_date\n"
166
166
  code << "def format_date( time )\n"
167
+ code << "time = apply_utc_offset(time)\n"
167
168
  if pl.date_method.nil?
168
169
  if pl.date_pattern =~ %r/%s/
169
170
  code << "time.strftime('#{pl.date_pattern.gsub('%s','%6N')}')\n"
@@ -203,7 +204,8 @@ module Logging::Layouts
203
204
  #
204
205
  # :pattern => "[%d] %-5l -- %c : %m\n"
205
206
  # :date_pattern => "%Y-%m-%d %H:%M:%S"
206
- # :date_method => 'usec' or 'to_s'
207
+ # :date_method => "usec" or "to_s"
208
+ # :utc_offset => "-06:00" or -21600 or "UTC"
207
209
  # :color_scheme => :default
208
210
  #
209
211
  # If used, :date_method will supersede :date_pattern.
@@ -216,7 +218,7 @@ module Logging::Layouts
216
218
  #
217
219
  def initialize( opts = {} )
218
220
  super
219
- @created_at = Time.now
221
+ @created_at = Time.now.freeze
220
222
 
221
223
  @date_pattern = opts.fetch(:date_pattern, nil)
222
224
  @date_method = opts.fetch(:date_method, nil)
@@ -2,8 +2,7 @@
2
2
  module Logging
3
3
 
4
4
  # This class defines a logging event.
5
- #
6
- LogEvent = Struct.new( :logger, :level, :data, :time, :file, :line, :method ) {
5
+ class LogEvent
7
6
  # :stopdoc:
8
7
 
9
8
  # Regular expression used to parse out caller information
@@ -16,6 +15,8 @@ module Logging
16
15
  CALLER_INDEX = ((defined? JRUBY_VERSION and JRUBY_VERSION > '1.6') or (defined? RUBY_ENGINE and RUBY_ENGINE[%r/^rbx/i])) ? 1 : 2
17
16
  # :startdoc:
18
17
 
18
+ attr_accessor :logger, :level, :data, :time, :file, :line, :method
19
+
19
20
  # call-seq:
20
21
  # LogEvent.new( logger, level, [data], caller_tracing )
21
22
  #
@@ -25,20 +26,26 @@ module Logging
25
26
  # invoked to get the execution trace of the logging method.
26
27
  #
27
28
  def initialize( logger, level, data, caller_tracing )
28
- f = l = m = ''
29
+ self.logger = logger
30
+ self.level = level
31
+ self.data = data
32
+ self.time = Time.now.freeze
29
33
 
30
34
  if caller_tracing
31
35
  stack = Kernel.caller[CALLER_INDEX]
32
36
  return if stack.nil?
33
37
 
34
38
  match = CALLER_RGXP.match(stack)
35
- f = match[1]
36
- l = Integer(match[2])
37
- m = match[3] unless match[3].nil?
39
+ self.file = match[1]
40
+ self.line = Integer(match[2])
41
+ self.method = match[3] unless match[3].nil?
42
+
43
+ if (bp = ::Logging.basepath) && !bp.empty? && file.index(bp) == 0
44
+ self.file = file.slice(bp.length + 1, file.length - bp.length)
45
+ end
46
+ else
47
+ self.file = self.line = self.method = ''
38
48
  end
39
-
40
- super(logger, level, data, Time.now, f, l, m)
41
49
  end
42
- }
43
- end # module Logging
44
-
50
+ end
51
+ end
@@ -264,7 +264,7 @@ module Logging
264
264
  # level = :all
265
265
  #
266
266
  # Set the level for this logger. The level can be either a +String+, a
267
- # +Symbol+, or a +Fixnum+. An +ArgumentError+ is raised if this is not
267
+ # +Symbol+, or an +Integer+. An +ArgumentError+ is raised if this is not
268
268
  # the case.
269
269
  #
270
270
  # There are two special levels -- "all" and "off". The former will
@@ -294,7 +294,7 @@ module Logging
294
294
  else
295
295
  lvl = case level
296
296
  when String, Symbol; ::Logging::level_num(level)
297
- when Fixnum; level
297
+ when Integer; level
298
298
  else
299
299
  raise ArgumentError,
300
300
  "level must be a String, Symbol, or Integer"
@@ -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.1.0".freeze
2
+ VERSION = "2.2.0".freeze
3
3
 
4
4
  # Returns the version string for the library.
5
5
  def self.version
@@ -1,47 +1,46 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: logging 2.0.0 ruby lib
2
+ # stub: logging 2.1.0.48 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.1.0.48"
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.license = "MIT"
13
- 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."
14
- s.email = "tim.pease@gmail.com"
15
- s.extra_rdoc_files = ["History.txt"]
16
- 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"]
17
- s.homepage = "http://rubygems.org/gems/logging"
18
- s.rdoc_options = ["--main", "README.md"]
19
- s.rubyforge_project = "logging"
20
- s.rubygems_version = "2.2.2"
21
- s.summary = "A flexible and extendable logging library for Ruby"
22
- 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 = "2017-01-08"
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_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.rubyforge_project = "logging".freeze
19
+ s.rubygems_version = "2.5.2".freeze
20
+ 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]
23
22
 
24
23
  if s.respond_to? :specification_version then
25
24
  s.specification_version = 4
26
25
 
27
26
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
28
- s.add_runtime_dependency(%q<little-plugger>, ["~> 1.1"])
29
- s.add_runtime_dependency(%q<multi_json>, ["~> 1.10"])
30
- s.add_development_dependency(%q<flexmock>, ["~> 1.0"])
31
- s.add_development_dependency(%q<bones-git>, ["~> 1.3"])
32
- s.add_development_dependency(%q<bones>, [">= 3.8.3"])
27
+ 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"])
30
+ s.add_development_dependency(%q<bones-git>.freeze, ["~> 1.3"])
31
+ s.add_development_dependency(%q<bones>.freeze, [">= 3.8.4"])
33
32
  else
34
- s.add_dependency(%q<little-plugger>, ["~> 1.1"])
35
- s.add_dependency(%q<multi_json>, ["~> 1.10"])
36
- s.add_dependency(%q<flexmock>, ["~> 1.0"])
37
- s.add_dependency(%q<bones-git>, ["~> 1.3"])
38
- s.add_dependency(%q<bones>, [">= 3.8.3"])
33
+ 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"])
36
+ s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
37
+ s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
39
38
  end
40
39
  else
41
- s.add_dependency(%q<little-plugger>, ["~> 1.1"])
42
- s.add_dependency(%q<multi_json>, ["~> 1.10"])
43
- s.add_dependency(%q<flexmock>, ["~> 1.0"])
44
- s.add_dependency(%q<bones-git>, ["~> 1.3"])
45
- s.add_dependency(%q<bones>, [">= 3.8.3"])
40
+ 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"])
43
+ s.add_dependency(%q<bones-git>.freeze, ["~> 1.3"])
44
+ s.add_dependency(%q<bones>.freeze, [">= 3.8.4"])
46
45
  end
47
46
  end
@@ -166,6 +166,19 @@ module TestLayouts
166
166
  assert_match %r/"ndc":\[\]/, format
167
167
  end
168
168
 
169
+ def test_utc_offset
170
+ layout = Logging.layouts.json(:items => %w[timestamp])
171
+ event = Logging::LogEvent.new('TimestampLogger', @levels['info'], 'log message', false)
172
+ event.time = Time.utc(2016, 12, 1, 12, 0, 0).freeze
173
+
174
+ assert_equal %Q/{"timestamp":"2016-12-01T12:00:00.000000Z"}\n/, layout.format(event)
175
+
176
+ layout.utc_offset = "-06:00"
177
+ assert_equal %Q/{"timestamp":"2016-12-01T06:00:00.000000-06:00"}\n/, layout.format(event)
178
+
179
+ layout.utc_offset = "+01:00"
180
+ assert_equal %Q/{"timestamp":"2016-12-01T13:00:00.000000+01:00"}\n/, layout.format(event)
181
+ end
169
182
  end # class TestJson
170
183
  end # module TestLayouts
171
184
  end # module TestLogging
@@ -26,7 +26,7 @@ module TestLayouts
26
26
 
27
27
  @layout.date_method = 'usec'
28
28
  assert_equal 'usec', @layout.date_method
29
- assert_instance_of Fixnum, @layout.format_date(Time.now)
29
+ assert_kind_of Integer, @layout.format_date(Time.now)
30
30
 
31
31
  @layout.date_method = :to_s
32
32
  assert_equal :to_s, @layout.date_method
@@ -38,7 +38,7 @@ module TestLayouts
38
38
 
39
39
  @layout.date_method = 'usec'
40
40
  assert_equal 'usec', @layout.date_method
41
- assert_instance_of Fixnum, @layout.format_date(Time.now)
41
+ assert_kind_of Integer, @layout.format_date(Time.now)
42
42
  end
43
43
 
44
44
  def test_date_pattern
@@ -230,6 +230,19 @@ module TestLayouts
230
230
  assert_equal 'context a', @layout.format(event)
231
231
  end
232
232
 
233
+ def test_utc_offset
234
+ layout = Logging.layouts.pattern(:pattern => "%d", :utc_offset => "UTC")
235
+ event = Logging::LogEvent.new('DateLogger', @levels['info'], 'log message', false)
236
+ event.time = Time.utc(2016, 12, 1, 12, 0, 0).freeze
237
+
238
+ assert_equal "2016-12-01T12:00:00", layout.format(event)
239
+
240
+ layout.utc_offset = "-06:00"
241
+ assert_equal "2016-12-01T06:00:00", layout.format(event)
242
+
243
+ layout.utc_offset = "+01:00"
244
+ assert_equal "2016-12-01T13:00:00", layout.format(event)
245
+ end
233
246
  end # TestBasic
234
247
  end # TestLayouts
235
248
  end # TestLogging
@@ -148,6 +148,20 @@ module TestLayouts
148
148
  assert_match %r/\nndc: \[\]\n/, format
149
149
  end
150
150
 
151
+ def test_utc_offset
152
+ layout = Logging.layouts.yaml(:items => %w[timestamp])
153
+ event = Logging::LogEvent.new('TimestampLogger', @levels['info'], 'log message', false)
154
+ event.time = Time.utc(2016, 12, 1, 12, 0, 0).freeze
155
+
156
+ assert_equal %Q{---\ntimestamp: '2016-12-01T12:00:00.000000Z'\n}, layout.format(event)
157
+
158
+ layout.utc_offset = "-06:00"
159
+ assert_equal %Q{---\ntimestamp: '2016-12-01T06:00:00.000000-06:00'\n}, layout.format(event)
160
+
161
+ layout.utc_offset = "+01:00"
162
+ assert_equal %Q{---\ntimestamp: '2016-12-01T13:00:00.000000+01:00'\n}, layout.format(event)
163
+ end
164
+
151
165
  private
152
166
 
153
167
  def assert_yaml_match( expected, actual )
@@ -120,6 +120,46 @@ module TestLogging
120
120
  @layout.backtrace = 'on'
121
121
  assert_equal true, @layout.backtrace
122
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
123
163
  end # class TestLayout
124
164
  end # module TestLogging
125
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