Class: Logging::Layout

### Home Classes Methods

### In Files

- lib/logging/layout.rb

### Parent

Object

### Methods

- ::new
- #footer
- #format
- #format_obj
- #header
- #try_json
- #try_yaml

### Files

- History.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Logging
- Logging::Appender
- Logging::Appenders
- Logging::Appenders::Buffering
- Logging::Appenders::Email
- Logging::Appenders::File
- Logging::Appenders::Growl
- Logging::Appenders::IO
- Logging::Appenders::RollingFile
- Logging::Appenders::Stderr
- Logging::Appenders::Stdout
- Logging::Appenders::StringIo
- Logging::Appenders::Syslog
- Logging::ColorScheme
- Logging::Config
- Logging::Config::Configurator
- Logging::Config::Configurator::DSL
- Logging::Config::Configurator::Error
- Logging::Config::Configurator::TopLevelDSL
- Logging::Config::YamlConfigurator
- Logging::Config::YamlConfigurator::Error
- Logging::Layout
- Logging::Layouts
- Logging::Layouts::Basic
- Logging::Layouts::Parseable
- Logging::Layouts::Pattern
- Logging::Logger
- Logging::MappedDiagnosticContext
- Logging::NestedDiagnosticContext
- Logging::Proxy
- Logging::RailsCompat
- Logging::Repository
- Logging::Repository::Singleton
- Logging::RootLogger
- Logging::Stats
- Logging::Stats::Sampler
- Logging::Stats::Tracker
- RSpec
- RSpec::Core
- RSpec::Core::Configuration
- RSpec::LoggingHelper
- RSpec::LoggingHelper
- Spec
- Spec::Runner
- Spec::Runner::Configuration
- Object
- Object::LoggingHelper
- File
- FileUtils
- Hash
- Kernel
- Module
- ReentrantMutex
- String

No matching classes.

# 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

- :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.

    # 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.getopt(:format_as, default)
      f = f.intern if f.instance_of? String

      @obj_format = case f
                    when :inspect, :yaml, :json; 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

### 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 59
    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 83
    def format_obj( obj )
      case obj
      when String; obj
      when Exception
        str = "<#{obj.class.name}> #{obj.message}"
        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)
               when :json; try_json(obj)
               else obj.to_s end
        str
      end
    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 67
    def header( ) '' end

try_json( obj ) click to toggle source

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.

    # File lib/logging/layout.rb, line 124
    def try_json( obj )
      MultiJson.encode(obj)
    rescue StandardError
      obj.inspect
    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.

obj - The Object to format.

Returns a String representation of the object.

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.