lib/logging/layout.rb in logging-2.1.0 vs lib/logging/layout.rb in logging-2.2.0

- old
+ new

@@ -39,11 +39,12 @@ @obj_format = case f when :inspect, :yaml, :json; f else :string end - self.backtrace = opts.fetch(:backtrace, ::Logging.backtrace) + self.backtrace = opts.fetch(:backtrace, ::Logging.backtrace) + self.utc_offset = opts.fetch(:utc_offset, ::Logging.utc_offset) end # call-seq: # layout.backtrace = true # @@ -60,9 +61,53 @@ end # Returns the backtrace setting. attr_reader :backtrace alias :backtrace? :backtrace + + # Set the UTC offset used when formatting time values. If left unset, the + # default local time zone will be used for time values. This method accepts + # the `utc_offset` format supported by the `Time#localtime` method in Ruby. + # + # Passing "UTC" or `0` as the UTC offset will cause all times to be reported + # in the UTC timezone. + # + # layout.utc_offset = "-07:00" # Mountain Standard Time in North America + # layout.utc_offset = "+01:00" # Central European Time + # layout.utc_offset = "UTC" # UTC + # layout.utc_offset = 0 # UTC + # + def utc_offset=( value ) + @utc_offset = case value + when nil; nil + when "UTC", "GMT", 0; 0 + else + Time.now.localtime(value) + value + end + end + + # Returns the UTC offset. + attr_reader :utc_offset + + # Internal: Helper method that applies the UTC offset to the given `time` + # instance. A new Time is returned that is equivalent to the original `time` + # but pinned to the timezone given by the UTC offset. + # + # If a UTC offset has not been set, then the original `time` instance is + # returned unchanged. + # + def apply_utc_offset( time ) + return time if utc_offset.nil? + + time = time.dup + if utc_offset == 0 + time.utc + else + time.localtime(utc_offset) + end + time + end # call-seq: # format( event ) # # Returns a string representation of the given logging _event_. It is