Class: Logging::Layout
- Inherits:
-
Object
- Object
- Logging::Layout
- 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.
Direct Known Subclasses
Logging::Layouts::Basic, Logging::Layouts::Parseable, Logging::Layouts::Pattern
Instance Method Summary collapse
-
#footer ⇒ Object
call-seq: footer.
-
#format(event) ⇒ Object
call-seq: format( event ).
-
#format_obj(obj) ⇒ Object
call-seq: format_obj( obj ).
-
#header ⇒ Object
call-seq: header.
-
#initialize(opts = {}) ⇒ Layout
constructor
call-seq: Layout.new( :format_as => :string ).
-
#try_yaml(obj) ⇒ Object
call-seq: try_yaml( obj ).
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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/logging/layout.rb', line 30 def initialize( opts = {} ) ::Logging.init unless ::Logging.const_defined? :MAX_LEVEL_LENGTH 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
#footer ⇒ Object
call-seq:
Returns a footer string to be used at the end of a logging appender.
73 |
# File 'lib/logging/layout.rb', line 73 def ( ) '' 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/logging/layout.rb', line 58 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 represenation or a yaml
based representation.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/logging/layout.rb', line 82 def format_obj( obj ) case obj when String; obj when Exception str = "<#{obj.class.name}> #{obj.}" if @backtrace && !obj.backtrace.nil? str << "\n\t" << obj.backtrace.join("\n\t") end str when nil; "<#{obj.class.name}> nil" else str = "<#{obj.class.name}> " str << case @obj_format when :inspect; obj.inspect when :yaml; try_yaml(obj) else obj.to_s end str end end |
#header ⇒ Object
call-seq:
header
Returns a header string to be used at the beginning of a logging appender.
66 |
# File 'lib/logging/layout.rb', line 66 def header( ) '' end |
#try_yaml(obj) ⇒ Object
call-seq:
try_yaml( obj )
Attempt to format the obj using yaml, but fall back to inspect style formatting if yaml fails.
108 109 110 111 112 |
# File 'lib/logging/layout.rb', line 108 def try_yaml( obj ) "\n#{obj.to_yaml}" rescue TypeError obj.inspect end |