Loggability::

Logger class

A subclass of Logger that provides additional API for configuring outputters, formatters, etc.

Constants

DEFAULT_DEVICE

Default log ‘device’

DEFAULT_SHIFT_AGE

Default ‘shift age’

DEFAULT_SHIFT_SIZE

Default ‘shift size’

Attributes

created_from R

The line that caused this logger to be created.

Public Class Methods

from_std_logger( logger )

Return an equivalent Loggability::Logger object for the given logger.

# File lib/loggability/logger.rb, line 99
def self::from_std_logger( logger )
        device = logger.instance_variable_get( :@logdev ) or
                raise ArgumentError, "%p doesn't appear to be a Logger (no @logdev)" % [ logger ]

        newlogger = self.new( device.dev )

        newlogger.level     = logger.level
        newlogger.formatter = logger.formatter

        return newlogger
end
new( logdev=DEFAULT_DEVICE, *args )

Create a new Logger wrapper that will output to the specified logdev.

# File lib/loggability/logger.rb, line 117
def initialize( logdev=DEFAULT_DEVICE, *args )
        super( nil )

        self.level = if $DEBUG then :debug else :warn end
        self.output_to( logdev, *args )

        @created_from = caller( 3 ).first
        @default_formatter = Loggability::Formatter.create( :default )
end

Public Instance Methods

<<( message )

Append operator – Override Logger’s append so log messages always have formatting, and are always appended at :debug level.

# File lib/loggability/logger.rb, line 179
def <<( message )
        unless self.logdev.nil?
                self.add( Logger::DEBUG, message )
        end
end
add( severity, message=nil, progname=nil ) { || ... }

Log a message if the severity is high enough. Overridden to account for the overridden level.

# File lib/loggability/logger.rb, line 157
def add( severity, message=nil, progname=nil )
        return true if severity < self.sev_threshold
        progname ||= @progname

        unless message
                if block_given?
                        message = yield
                else
                        message = progname
                        progname = @progname
                end
        end

        msg = self.format_message( self.format_severity(severity), Time.now, progname, message )
        self.logdev.write( msg )

        return true
end
inspect()

Return a human-readable representation of the object suitable for debugging.

# File lib/loggability/logger.rb, line 138
def inspect
        dev = if self.logdev.respond_to?( :dev )
                        self.logdev.dev.class
                else
                        self.logdev
                end

        return "#<%p:%#x severity: %s, formatter: %s, outputting to: %p>" % [
                self.class,
                self.object_id * 2,
                self.level,
                self.formatter.class.name.sub( /.*::/, '' ).downcase,
                dev,
        ]
end
restore_settings( settings )

Restore the level, logdev, and formatter from the given settings.

# File lib/loggability/logger.rb, line 206
def restore_settings( settings )
        self.level = settings[:level]            if settings[ :level ]
        self.output_to( settings[:logdev] )      if settings[ :logdev ]
        self.format_with( settings[:formatter] ) if settings[ :formatter ]
end
settings()

Return a Hash that contains its settings suitable for restoration via restore_settings later.

# File lib/loggability/logger.rb, line 196
def settings
        return {
                level:     self.level,
                logdev:    self.logdev,
                formatter: self.formatter,
        }
end
write( message )

Rack::CommonLogger compatibility method – append a message at ‘info’ level.

# File lib/loggability/logger.rb, line 187
def write( message )
        unless self.logdev.nil?
                self.add( Logger::INFO, message )
        end
end

Output Device

↑ top

Attributes

logdev RW

The raw log device

Public Instance Methods

output_to( target, *args )

Change the log device to log to target instead of what it was before. Any additional args are passed to the LogDevice’s constructor. In addition to Logger’s support for logging to IO objects and files (given a filename in a String), this method can also set up logging to any object that responds to <<.

# File lib/loggability/logger.rb, line 284
def output_to( target, *args )
        if target.is_a?( Logger::LogDevice ) || target.is_a?( Loggability::LogDevice )
                self.logdev = target
        elsif target.respond_to?( :write ) || target.is_a?( String )
                opts = { :shift_age => args.shift || 0, :shift_size => args.shift || 1048576 }
                self.logdev = Logger::LogDevice.new( target, **opts )
        elsif target.respond_to?( :any? ) && target.any?( Loggability::LogDevice )
                self.logdev = MultiDevice.new( target )
        elsif target.respond_to?( :<< )
                self.logdev = Loggability::LogDevice.create( :appending, target )
        elsif target.is_a?( Symbol )
                self.logdev = Loggability::LogDevice.create( target, *args )
        else
                raise ArgumentError, "don't know how to output to %p (a %p)" % [ target, target.class ]
        end
end
Also aliased as: write_to
write_to( target, *args )
Alias for: output_to

Output Formatting API

↑ top

Public Instance Methods

format_as( formatter=nil )
Alias for: format_with
format_message( severity, datetime, progname, msg )

Format a log message using the current formatter and return it.

# File lib/loggability/logger.rb, line 314
def format_message( severity, datetime, progname, msg )
        self.formatter.call( severity, datetime, progname, msg )
end
format_severity( severity )

Return the formatted name of the given severity.

# File lib/loggability/logger.rb, line 320
def format_severity( severity )
        name = LOG_LEVEL_NAMES[ severity ] || severity.to_s
        return name.upcase
end
format_with( formatter=nil ) { |severity, datetime, progname, msg| ... }

Set a new formatter for the logger. If formatter is nil or :default, this causes the logger to fall back to its default formatter. If it’s a Symbol other than :default, it looks for a similarly-named formatter under loggability/formatter/ and uses that. If formatter is an object that responds to call (e.g., a Proc or a Method object), that object is used directly.

Procs and methods should have the method signature: (severity, datetime, progname, msg).

# Load and use the HTML formatter
MyProject.logger.format_with( :html )

# Call self.format_logmsg(...) to format messages
MyProject.logger.format_with( self.method(:format_logmsg) )

# Reset to the default
MyProject.logger.format_with( :default )
# File lib/loggability/logger.rb, line 343
def format_with( formatter=nil, &block ) # :yield: severity, datetime, progname, msg
        formatter ||= block

        if formatter.nil? || formatter == :default
                @formatter = nil

        elsif formatter.respond_to?( :call )
                @formatter = formatter

        elsif formatter.respond_to?( :to_sym )
                @formatter = Loggability::Formatter.create( formatter )

        else
                raise ArgumentError, "don't know what to do with a %p formatter (%p)" %
                        [ formatter.class, formatter ]
        end
end
Also aliased as: format_as , formatter=
formatter()

Return the current formatter used to format log messages.

# File lib/loggability/logger.rb, line 308
def formatter
        return ( @formatter || @default_formatter )
end
formatter=( formatter=nil )
Alias for: format_with

Progname Proxy

↑ top

Public Instance Methods

proxy_for( object )

Create a logging proxy for object that will include its name as the ‘progname’ of each message.

# File lib/loggability/logger.rb, line 374
def proxy_for( object )
        return ObjectNameProxy.new( self, object )
end

Severity Level

↑ top

Public Instance Methods

debug!()

Sets the log level to :debug.

# File lib/loggability/logger.rb, line 241
def debug!; self.sev_threshold = Logger::DEBUG; end
debug?()

Returns true if the log level allows entries with severity :debug to be written, false otherwise.

# File lib/loggability/logger.rb, line 238
def debug?; self.sev_threshold <= Logger::DEBUG; end
error!()

Sets the log level to :error.

# File lib/loggability/logger.rb, line 262
def error!; self.sev_threshold = Logger::ERROR; end
error?()

Returns true if the log level allows entries with severity :error to be written, false otherwise.

# File lib/loggability/logger.rb, line 259
def error?; self.sev_threshold <= Logger::ERROR; end
fatal!()

Sets the log level to :fatal.

# File lib/loggability/logger.rb, line 269
def fatal!; self.sev_threshold = Logger::FATAL; end
fatal?()

Returns true if the log level allows entries with severity :fatal to be written, false otherwise.

# File lib/loggability/logger.rb, line 266
def fatal?; self.sev_threshold <= Logger::FATAL; end
info!()

Sets the log level to :info.

# File lib/loggability/logger.rb, line 248
def info!; self.sev_threshold = Logger::INFO; end
info?()

Returns true if the log level allows entries with severity :info to be written, false otherwise.

# File lib/loggability/logger.rb, line 245
def info?; self.sev_threshold <= Logger::INFO; end
level()

Return the logger’s level as a Symbol.

# File lib/loggability/logger.rb, line 218
def level
        return LOG_LEVEL_NAMES[ self.sev_threshold ]
end
level=( newlevel )

Set the logger level to newlevel, which can be a numeric level (e.g., Logger::DEBUG, etc.), or a symbolic level (e.g., :debug, :info, etc.)

# File lib/loggability/logger.rb, line 225
def level=( newlevel )
        newlevel = LOG_LEVELS[ newlevel.to_sym ] if
                newlevel.respond_to?( :to_sym ) && LOG_LEVELS.key?( newlevel.to_sym )
        super( newlevel )
end
warn!()

Sets the log level to :warn.

# File lib/loggability/logger.rb, line 255
def warn!; self.sev_threshold = Logger::WARN; end
warn?()

Returns true if the log level allows entries with severity :warn to be written, false otherwise.

# File lib/loggability/logger.rb, line 252
def warn?; self.sev_threshold <= Logger::WARN; end