lib/logging/layouts/parseable.rb in logging-1.6.1 vs lib/logging/layouts/parseable.rb in logging-1.6.2

- old
+ new

@@ -56,14 +56,14 @@ # logger: Foo # message: <RuntimeError> Oooops!! # # The output order of the fields is not guaranteed to be the same as the # order specified in the _items_ list. This is because Ruby hashes are not - # ordered by default (unless your running this in Ruby 1.9). + # ordered by default (unless you're running this in Ruby 1.9). # # When configured to output log events in JSON format, each log message - # will be formatted as an object (in the JSON sense of the work) on it's + # will be formatted as an object (in the JSON sense of the word) on it's # own line in the log output. Therefore, to parse the output you must read # it line by line and parse the individual objects. Taking the same # example above the JSON output would be: # # {"timestamp":"2009-04-17 16:15:42","level":"INFO","logger":"Foo::Bar","message":"this is a log message"} @@ -76,11 +76,11 @@ # :stopdoc: # Arguments to sprintf keyed to directive letters DIRECTIVE_TABLE = { 'logger' => 'event.logger', - 'timestamp' => 'event.time.strftime(Pattern::ISO8601)', + 'timestamp' => 'event.time', 'level' => '::Logging::LNAMES[event.level]', 'message' => 'format_obj(event.data)', 'file' => 'event.file', 'line' => 'event.line', 'method' => 'event.method', @@ -130,22 +130,22 @@ # :startdoc: # call-seq: # Parseable.json( opts ) # - # Create a new Parseable layout that outputs log events usig JSON style + # Create a new Parseable layout that outputs log events using JSON style # formatting. See the initializer documentation for available options. # def self.json( opts = {} ) opts[:style] = 'json' new(opts) end # call-seq: # Parseable.yaml( opts ) # - # Create a new Parseable layout that outputs log events usig YAML style + # Create a new Parseable layout that outputs log events using YAML style # formatting. See the initializer documentation for available options. # def self.yaml( opts = {} ) opts[:style] = 'yaml' new(opts) @@ -189,20 +189,34 @@ # def format_as_json( value ) case value when String, Integer, Float; value.inspect when nil; 'null' - else value.to_s.inspect end + when Time; %Q{"#{iso8601_format(value)}"} + else %Q{"#{value.inspect}"} end end # Call the appropriate class level create format method based on the # style of this parseable layout. # def create_format_method case @style when :json; Parseable.create_json_format_method(self) when :yaml; Parseable.create_yaml_format_method(self) else raise ArgumentError, "unknown format style '#@style'" end + end + + # Convert the given time _value_ into an ISO8601 formatted time string. + # + def iso8601_format( value ) + str = value.strftime('%Y-%m-%dT%H:%M:%S') + str << ('.%06d' % value.usec) + + offset = value.gmt_offset.abs + return str << 'Z' if offset == 0 + + offset = sprintf('%02d:%02d', offset / 3600, offset % 3600 / 60) + return str << (value.gmt_offset < 0 ? '-' : '+') << offset end end # Parseable end # Logging::Layouts