Skip to content
Browse files

Adding support for JSON encoding of objects.

Layouts can now format objects as JSON strings before writing them to the log
message. This is independent of the JSON parseable layout.
1 parent 4a56219 commit bf91a57042c458288745280e58b9d38463cb69ee @TwP committed Sep 13, 2012
Showing with 40 additions and 10 deletions.
  1. +5 −5 examples/formatting.rb
  2. +2 −1 lib/logging.rb
  3. +21 −4 lib/logging/layout.rb
  4. +7 −0 test/test_layout.rb
  5. +5 −0 test/test_logging.rb
View
10 examples/formatting.rb
@@ -5,14 +5,14 @@
# "format_as" option and a global "backtrace" option.
#
# The format_as option allows objects to be converted to a string using the
-# standard "to_s" method, the "inspect" method, or the "to_yaml" method
-# (this is independent of the YAML layout). The format_as option can be
-# overridden by each layout as desired.
+# standard "to_s" method, the "inspect" method, the "to_json" method, or the
+# "to_yaml" method (this is independent of the YAML layout). The format_as
+# option can be overridden by each layout as desired.
#
-# Logging.format_as :string # or :inspect or :yaml
+# Logging.format_as :string # or :inspect or :json or :yaml
#
# Exceptions are treated differently by the logging framework. The Exception
-# class is printed along with the message. Optionally, exception backtraces
+# class is printed along with the message. Optionally, the exception backtrace
# can be included in the logging output; this option is enabled by default.
#
# Logging.backtrace false
View
3 lib/logging.rb
@@ -320,14 +320,15 @@ def init( *args )
# * :string => to_s
# * :inspect => inspect
# * :yaml => to_yaml
+ # * :json => MultiJson.encode(obj)
#
# An +ArgumentError+ is raised if anything other than +:string+,
# +:inspect+, +:yaml+ is passed to this method.
#
def format_as( f )
f = f.intern if f.instance_of? String
- unless [:string, :inspect, :yaml].include? f
+ unless [:string, :inspect, :yaml, :json].include? f
raise ArgumentError, "unknown object format '#{f}'"
end
View
25 lib/logging/layout.rb
@@ -22,6 +22,7 @@ class Layout
# * :string => to_s
# * :inspect => inspect
# * :yaml => to_yaml
+ # * :json => MultiJson.encode(obj)
#
# If the format is not specified then the global object format is used
# (see Logging#format_as). If the global object format is not specified
@@ -37,7 +38,7 @@ def initialize( opts = {} )
f = f.intern if f.instance_of? String
@obj_format = case f
- when :inspect, :yaml; f
+ when :inspect, :yaml, :json; f
else :string end
b = opts.getopt(:backtrace, ::Logging.backtrace)
@@ -94,23 +95,39 @@ def format_obj( obj )
str << case @obj_format
when :inspect; obj.inspect
when :yaml; try_yaml(obj)
+ when :json; try_json(obj)
else obj.to_s end
str
end
end
- # call-seq:
- # try_yaml( obj )
- #
# Attempt to format the _obj_ using yaml, but fall back to inspect style
# formatting if yaml fails.
#
+ # obj - The Object to format.
+ #
+ # Returns a String representation of the object.
+ #
def try_yaml( obj )
"\n#{obj.to_yaml}"
rescue TypeError
obj.inspect
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.
+ #
+ # 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
+ end
+
end # class Layout
end # module Logging
View
7 test/test_layout.rb
@@ -26,6 +26,9 @@ def test_initialize
@layout = ::Logging::Layout.new :format_as => :inspect
assert_equal :inspect, obj_format[@layout]
+ @layout = ::Logging::Layout.new 'format_as' => :json
+ assert_equal :json, obj_format[@layout]
+
@layout = ::Logging::Layout.new 'format_as' => :yaml
assert_equal :yaml, obj_format[@layout]
@@ -73,6 +76,10 @@ def test_format_obj
r = @layout.format_obj obj
assert_equal '<Array> ["one", "two", "three", "four"]', r
+ @layout = ::Logging::Layout.new :format_as => :json
+ r = @layout.format_obj obj
+ assert_equal '<Array> ["one","two","three","four"]', r
+
@layout = ::Logging::Layout.new :format_as => :yaml
r = @layout.format_obj obj
assert_match %r/\A<Array> \n--- ?\n- one\n- two\n- three\n- four\n/, r
View
5 test/test_logging.rb
@@ -231,6 +231,11 @@ def test_format_as
assert_equal :inspect, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
+ ::Logging.format_as :json
+ assert ::Logging.const_defined?('OBJ_FORMAT')
+ assert_equal :json, ::Logging::OBJ_FORMAT
+ remove_const[:OBJ_FORMAT]
+
::Logging.format_as :yaml
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :yaml, ::Logging::OBJ_FORMAT

0 comments on commit bf91a57

Please sign in to comment.
Something went wrong with that request. Please try again.