Skip to content

Commit

Permalink
Parseable layouts ignore the "format_as" directive.
Browse files Browse the repository at this point in the history
Since we are already encoding the entire log event as a YAML or JSON string,
it makes sense to just use that encoding for the log message.
  • Loading branch information
TwP committed Sep 13, 2012
1 parent bf91a57 commit c120b18
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 4 additions & 5 deletions lib/logging/layout.rb
Expand Up @@ -115,17 +115,16 @@ def try_yaml( obj )
end

# Attempt to format the given object as a JSON string, but fall back to
# inspect formatting if JSON encoding does not make sense. This method will
# only format Array and Hash objects.
# inspect formatting if JSON encoding fails.
#
# obj - The Object to format.
#
# Returns a String representation of the object.
#
def try_json( obj )
case obj
when Hash, Array; MultiJson.encode(obj)
else obj.inspect end
MultiJson.encode(obj)
rescue StandardError
obj.inspect
end

end # class Layout
Expand Down
25 changes: 25 additions & 0 deletions lib/logging/layouts/parseable.rb
Expand Up @@ -201,6 +201,31 @@ def items=( ary )
create_format_method
end

# Public: Take a given object and convert it into a format suitable for
# inclusion as a log message. The conversion allows the object to be more
# easily expressed in YAML or JSON form.
#
# If the object is an Exception, then this method will return a Hash
# containing the exception class name, message, and backtrace (if any).
#
# obj - The Object to format
#
# Returns the formatted Object.
#
def format_obj( obj )
case obj
when Exception
h = { :class => obj.class.name,
:message => obj.message }
h[:backtrace] = obj.backtrace if @backtrace && !obj.backtrace.nil?
h
when Time
iso8601_format(obj)
else
obj
end
end

private

# Call the appropriate class level create format method based on the
Expand Down
4 changes: 2 additions & 2 deletions test/layouts/test_json.rb
Expand Up @@ -35,7 +35,7 @@ def test_format
assert_match %r/"timestamp":"#@date_fmt"/, format
assert_match %r/"level":"INFO"/, format
assert_match %r/"logger":"ArrayLogger"/, format
assert_match %r/"message":"<Array> #{Regexp.escape [1,2,3,4].to_s}"/, format
assert_match %r/"message":\[1,2,3,4\]/, format

event.level = @levels['debug']
event.data = 'and another message'
Expand All @@ -52,7 +52,7 @@ def test_format
assert_match %r/"timestamp":"#@date_fmt"/, format
assert_match %r/"level":"FATAL"/, format
assert_match %r/"logger":"Test"/, format
assert_match %r/"message":"<Exception> Exception"/, format
assert_match %r/"message":\{(?:"(?:class|message)":"Exception",?){2}\}/, format
end

def test_items
Expand Down
4 changes: 2 additions & 2 deletions test/layouts/test_yaml.rb
Expand Up @@ -27,7 +27,7 @@ def test_format
assert_yaml_match h, @layout.format(event)

event.data = [1, 2, 3, 4]
h['message'] = "<Array> #{[1,2,3,4]}"
h['message'] = [1,2,3,4]
assert_yaml_match h, @layout.format(event)

event.level = @levels['debug']
Expand All @@ -41,7 +41,7 @@ def test_format
event.data = Exception.new
h['level'] = 'FATAL'
h['logger'] = 'Test'
h['message'] = '<Exception> Exception'
h['message'] = {:class => 'Exception', :message => 'Exception'}
assert_yaml_match h, @layout.format(event)
end

Expand Down

0 comments on commit c120b18

Please sign in to comment.