module Logging

color_scheme.rb

Created by Jeremy Hinegardner on 2007-01-24 Copyright 2007. All rights reserved

This is Free Software. See LICENSE and COPYING for details

Constants

LogEvent

This class defines a logging event.

Public Class Methods

clear_diagnostic_contexts( all = false ) click to toggle source

Public: Convenience method that will clear both the Mapped Diagnostic Context and the Nested Diagnostic Context of the current thread. If the `all` flag passed to this method is true, then the diagnostic contexts for every thread in the application will be cleared.

all - Boolean flag used to clear the context of every Thread (default is false)

Returns the Logging module.

# File lib/logging/diagnostic_context.rb, line 261
def self.clear_diagnostic_contexts( all = false )
  if all
    Thread.exclusive {
      Thread.list.each { |thread|
        thread[MappedDiagnosticContext::NAME].clear if thread[MappedDiagnosticContext::NAME]
        thread[NestedDiagnosticContext::NAME].clear if thread[NestedDiagnosticContext::NAME]
      }
    }
  else
    MappedDiagnosticContext.clear
    NestedDiagnosticContext.clear
  end

  self
end
mdc() click to toggle source

Public: Accessor method for getting the current Thread's MappedDiagnosticContext.

Returns MappedDiagnosticContext

# File lib/logging/diagnostic_context.rb, line 243
def self.mdc() MappedDiagnosticContext end
ndc() click to toggle source

Public: Accessor method for getting the current Thread's NestedDiagnosticContext.

Returns NestedDiagnosticContext

# File lib/logging/diagnostic_context.rb, line 250
def self.ndc() NestedDiagnosticContext end

Public Instance Methods

configure( filename ) click to toggle source
configure { block }

Configures the Logging framework using the configuration information found in the given file. The file extension should be either '.yaml' or '.yml' (XML configuration is not yet supported).

# File lib/logging.rb, line 39
def configure( *args, &block )
  if block
    return ::Logging::Config::Configurator.process(&block)
  end

  filename = args.shift
  raise ArgumentError, 'a filename was not given' if filename.nil?

  case File.extname(filename)
  when '.yaml', '.yml'
    ::Logging::Config::YamlConfigurator.load(filename, *args)
  else raise ArgumentError, 'unknown configuration file format' end
end
logger( device, age = 7, size = 1048576 ) click to toggle source
logger( device, age = 'weekly' )

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/logging.rb, line 92
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?(Fixnum)
  a_opts[:age]  = age  if age.instance_of?(String)
  a_opts[:keep] = keep if keep.instance_of?(Fixnum)
  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)
  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