Class: Logging::Layout

Inherits:
Object show all
Defined in:
lib/gems/logging-0.9.4/lib/logging/layout.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Layout

call-seq:

Layout.new( :format_as => :string )

Creates a new layout that will format objecs 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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gems/logging-0.9.4/lib/logging/layout.rb', line 32

def initialize( opts = {} )
  default = ::Logging.const_defined?('OBJ_FORMAT') ?
            ::Logging::OBJ_FORMAT : nil

  f = opts.getopt(:format_as, default)
  f = f.intern if f.instance_of? String

  @obj_format = case f
                when :inspect, :yaml; f
                else :string end

  b = opts.getopt(:backtrace, ::Logging.backtrace)
  @backtrace = case b
      when :on, 'on', true;    true
      when :off, 'off', false; false
      else
        raise ArgumentError, "backtrace must be true or false"
      end
end

Instance Method Details

call-seq:

footer

Returns a footer string to be used at the end of a logging appender.



73
# File 'lib/gems/logging-0.9.4/lib/logging/layout.rb', line 73

def footer( ) '' end

#format(event) ⇒ Object

call-seq:

format( event )

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



58
# File 'lib/gems/logging-0.9.4/lib/logging/layout.rb', line 58

def format( event ) nil end

#headerObject

call-seq:

header

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



66
# File 'lib/gems/logging-0.9.4/lib/logging/layout.rb', line 66

def header( ) '' end