Module: Logging
- Defined in:
- lib/dldinternet/mixlib/logging.rb
Defined Under Namespace
Classes: ColorScheme, LogEvent, Logger
Class Method Summary collapse
-
.logger(*args) ⇒ Object
call-seq: Logging.logger( device, age = 7, size = 1048576 ) Logging.logger( device, age = ‘weekly’ ).
Class Method Details
.logger(*args) ⇒ Object
call-seq:
Logging.logger( device, age = 7, size = 1048576 )
Logging.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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 |