Parent

Logging::Layout

The Layout class provides methods for formatting log events into a string representation. Layouts are used by Appenders to format log events before writing them to the logging destination.

All other Layouts inherit from this class which provides stub methods. Each subclass should provide a format method. A layout can be used by more than one Appender so all the methods need to be thread safe.

Public Class Methods

new( :format_as => :string ) click to toggle source

Creates a new layout that will format objects as strings using the given :format_as style. This can be one of :string, :inspect, or :yaml. These formatting commands map to the following object methods:

  • :string => to_s

  • :inspect => inspect

  • :yaml => to_yaml

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 then :string is used.

    # File lib/logging/layout.rb, line 30
30:   def initialize( opts = {} )
31:     ::Logging.init unless ::Logging.initialized?
32: 
33:     default = ::Logging.const_defined?('OBJ_FORMAT') ?
34:               ::Logging::OBJ_FORMAT : nil
35: 
36:     f = opts.getopt(:format_as, default)
37:     f = f.intern if f.instance_of? String
38: 
39:     @obj_format = case f
40:                   when :inspect, :yaml; f
41:                   else :string end
42: 
43:     b = opts.getopt(:backtrace, ::Logging.backtrace)
44:     @backtrace = case b
45:         when :on, 'on', true;    true
46:         when :off, 'off', false; false
47:         else
48:           raise ArgumentError, "backtrace must be true or false"
49:         end
50:   end

Public Instance Methods

format( event ) click to toggle source

Returns a string representation of the given logging event. It is up to subclasses to implement this method.

    # File lib/logging/layout.rb, line 58
58:   def format( event ) nil end
format_obj( obj ) click to toggle source

Return a string representation of the given object. Depending upon the configuration of the logger system the format will be an inspect based representation or a yaml based representation.

     # File lib/logging/layout.rb, line 82
 82:   def format_obj( obj )
 83:     case obj
 84:     when String; obj
 85:     when Exception
 86:       str = "<#{obj.class.name}> #{obj.message}"
 87:       if @backtrace && !obj.backtrace.nil?
 88:         str << "\n\t" << obj.backtrace.join("\n\t")
 89:       end
 90:       str
 91:     when nil; "<#{obj.class.name}> nil"
 92:     else
 93:       str = "<#{obj.class.name}> "
 94:       str << case @obj_format
 95:              when :inspect; obj.inspect
 96:              when :yaml; try_yaml(obj)
 97:              else obj.to_s end
 98:       str
 99:     end
100:   end
header click to toggle source

Returns a header string to be used at the beginning of a logging appender.

    # File lib/logging/layout.rb, line 66
66:   def header( ) '' end
try_yaml( obj ) click to toggle source

Attempt to format the obj using yaml, but fall back to inspect style formatting if yaml fails.

     # File lib/logging/layout.rb, line 108
108:   def try_yaml( obj )
109:     "\n#{obj.to_yaml}"
110:   rescue TypeError
111:     obj.inspect
112:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.