Class: Logging::Layout

Inherits:
Object
  • Object
show all
Defined in:
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Layout

call-seq:

Layout.new( :format_as => :string )

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

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logging/layout.rb', line 31

def initialize( opts = {} )
  ::Logging.init unless ::Logging.initialized?

  default = ::Logging.const_defined?('OBJ_FORMAT') ?
            ::Logging::OBJ_FORMAT : nil

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

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

  self.backtrace  = opts.fetch(:backtrace,  ::Logging.backtrace)
  self.utc_offset = opts.fetch(:utc_offset, ::Logging.utc_offset)
end

Instance Attribute Details

#backtraceObject Also known as: backtrace?

Returns the backtrace setting.



64
65
66
# File 'lib/logging/layout.rb', line 64

def backtrace
  @backtrace
end

#utc_offsetObject

Returns the UTC offset.



90
91
92
# File 'lib/logging/layout.rb', line 90

def utc_offset
  @utc_offset
end

Instance Method Details

#apply_utc_offset(time) ⇒ Object

Internal: Helper method that applies the UTC offset to the given ‘time` instance. A new Time is returned that is equivalent to the original `time` but pinned to the timezone given by the UTC offset.

If a UTC offset has not been set, then the original ‘time` instance is returned unchanged.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/logging/layout.rb', line 99

def apply_utc_offset( time )
  return time if utc_offset.nil?

  time = time.dup
  if utc_offset == 0
    time.utc
  else
    time.localtime(utc_offset)
  end
  time
end

call-seq:

footer

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



132
# File 'lib/logging/layout.rb', line 132

def footer( ) '' end

#format(event) ⇒ Object

call-seq:

format( event )

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



117
# File 'lib/logging/layout.rb', line 117

def format( event ) nil end

#format_obj(obj) ⇒ Object

call-seq:

format_obj( obj )

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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/logging/layout.rb', line 141

def format_obj( obj )
  case obj
  when String; obj
  when Exception
    ary = ["<#{obj.class.name}> #{obj.message}"]
    if backtrace? && !obj.backtrace.nil?
      ary.concat(obj.backtrace)
    end
    if defined?(obj.cause) && !obj.cause.nil?
      ary << "--- Caused by ---"
      ary << format_obj(obj.cause)
    end
    ary.join("\n\t")
  when nil; "<#{obj.class.name}> nil"
  else
    str = "<#{obj.class.name}> "
    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

#headerObject

call-seq:

header

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



125
# File 'lib/logging/layout.rb', line 125

def header( ) '' end

#try_json(obj) ⇒ Object

Attempt to format the given object as a JSON string, but fall back to inspect formatting if JSON encoding fails.

obj - The Object to format.

Returns a String representation of the object.



186
187
188
189
190
# File 'lib/logging/layout.rb', line 186

def try_json( obj )
  MultiJson.encode(obj)
rescue StandardError
  obj.inspect
end

#try_yaml(obj) ⇒ Object

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.



173
174
175
176
177
# File 'lib/logging/layout.rb', line 173

def try_yaml( obj )
  "\n#{obj.to_yaml}"
rescue TypeError
  obj.inspect
end