module Logging
Constants
- LogEvent
Public Class Methods
This convenience method returns a Logger
instance configured to behave similarly to a core Ruby Logger
instance.
The device is the logging destination. This can be a filename (String) or an IO object (STDERR, STDOUT, an open File, etc.). The age is the number of old log files to keep or the frequency of rotation (daily
, weekly
, or monthly
). The size is the maximum logfile size and is only used when age is a number.
Using the same device twice will result in the same Logger
instance being returned. For example, if a Logger
is created using STDOUT then the same Logger
instance will be returned the next time STDOUT is used. A new Logger
instance can be obtained by closing the previous logger instance.
log1 = Logging.logger(STDOUT) log2 = Logging.logger(STDOUT) log1.object_id == log2.object_id #=> true log1.close log2 = Logging.logger(STDOUT) log1.object_id == log2.object_id #=> false
The format of the log messages can be changed using a few optional parameters. The :pattern
can be used to change the log message format. The :date_pattern
can be used to change how timestamps are formatted.
log = Logging.logger(STDOUT, :pattern => "[%d] %-5l : %m\n", :date_pattern => "%Y-%m-%d %H:%M:%S.%s")
See the documentation for the Logging::Layouts::Pattern
class for a full description of the :pattern and :date_pattern formatting strings.
# File lib/dldinternet/mixlib/logging.rb, line 57 def logger( *args ) return ::Logging::Logger if args.empty? opts = args.pop if args.last.instance_of?(Hash) opts ||= Hash.new dev = args.shift keep = age = args.shift size = args.shift name = case dev when String; dev when File; dev.path else dev.object_id.to_s end repo = ::Logging::Repository.instance return repo[name] if repo.has_logger? name l_opts = { :pattern => "%.1l, [%d #%p] %#{::Logging::MAX_LEVEL_LENGTH}l : %m\n", :date_pattern => '%Y-%m-%dT%H:%M:%S.%s' } [:pattern, :date_pattern, :date_method].each do |o| l_opts[o] = opts.delete(o) if opts.has_key? o end layout = ::Logging::Layouts::Pattern.new(l_opts) a_opts = Hash.new a_opts[:size] = size if size.instance_of?(Integer) a_opts[:age] = age if age.instance_of?(String) a_opts[:keep] = keep if keep.instance_of?(Integer) a_opts[:filename] = dev if dev.instance_of?(String) a_opts[:layout] = layout a_opts.merge! opts appender = case dev when String ::Logging::Appenders::RollingFile.new(name, a_opts) else ::Logging::Appenders::IO.new(name, dev, a_opts) end logger = ::Logging::Logger.new(name, opts) logger.add_appenders appender logger.additive = false class << logger def close @appenders.each {|a| a.close} h = ::Logging::Repository.instance.instance_variable_get :@h h.delete(@name) class << self; undef :close; end end end logger end
Creates a new log event with the given logger name, numeric level, array of data from the user to be logged, and boolean trace flag. If the trace flag is set to true
then Kernel::caller will be invoked to get the execution trace of the logging method.
# File lib/dldinternet/mixlib/logging.rb, line 148 def initialize( logger, level, data, trace ) f = l = m = '' if trace stack = Kernel.caller[::Logging::LogEvent.caller_index] return if stack.nil? match = CALLER_RGXP.match(stack) f = match[1] l = Integer(match[2]) m = match[3] unless match[3].nil? end super(logger, level, data, Time.now, f, l, m) end