Module: Logging

Extended by:
LittlePlugger
Defined in:
lib/logging/color_scheme.rb,
lib/logging.rb,
lib/logging/proxy.rb,
lib/logging/layout.rb,
lib/logging/logger.rb,
lib/logging/layouts.rb,
lib/logging/appender.rb,
lib/logging/appenders.rb,
lib/logging/log_event.rb,
lib/logging/repository.rb,
lib/logging/root_logger.rb,
lib/logging/rails_compat.rb,
lib/logging/diagnostic_context.rb

Overview

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

Defined Under Namespace

Modules: Appenders, Config, Layouts, MappedDiagnosticContext, NestedDiagnosticContext, Plugins, RailsCompat, Stats Classes: Appender, ColorScheme, Layout, LogEvent, Logger, Proxy, Repository, RootLogger

Constant Summary

LIBPATH =
::File.expand_path('..', __FILE__) + ::File::SEPARATOR
PATH =
::File.expand_path('../..', __FILE__) + ::File::SEPARATOR
LEVELS =
{}
LNAMES =
[]

Class Method Summary (collapse)

Class Method Details

+ (Object) appenders



159
160
161
# File 'lib/logging.rb', line 159

def appenders
  ::Logging::Appenders
end

+ (Object) backtrace(b = nil)



352
353
354
355
356
357
358
359
360
361
362
# File 'lib/logging.rb', line 352

def backtrace( b = nil )
  @backtrace = true unless defined? @backtrace
  return @backtrace if b.nil?

  @backtrace = case b
      when :on, 'on', true;    true
      when :off, 'off', false; false
      else
        raise ArgumentError, "backtrace must be true or false"
      end
end

+ (Object) clear_diagnostic_contexts(all = false)

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.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# 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

+ (Object) color_scheme(name, opts = {})



170
171
172
173
174
175
176
# File 'lib/logging.rb', line 170

def color_scheme( name, opts = {} )
  if opts.empty?
    ::Logging::ColorScheme[name]
  else
    ::Logging::ColorScheme.new(name, opts)
  end
end

+ (Object) configure(*args, &block)

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

+ (Object) consolidate(*args)



211
212
213
214
# File 'lib/logging.rb', line 211

def consolidate( *args )
  ::Logging::Repository.instance.add_master(*args)
  self
end

+ (Object) format_as(f)



328
329
330
331
332
333
334
335
336
337
# File 'lib/logging.rb', line 328

def format_as( f )
  f = f.intern if f.instance_of? String

  unless [:string, :inspect, :yaml, :json].include? f
    raise ArgumentError, "unknown object format '#{f}'"
  end

  module_eval "OBJ_FORMAT = :#{f}", __FILE__, __LINE__
  self
end

+ (Object) globally(name = :logger)



244
245
246
247
248
# File 'lib/logging.rb', line 244

def globally( name = :logger )
  Module.new {
    eval "def #{name}() @_logging_logger ||= ::Logging::Logger[self] end"
  }
end

+ (Object) init(*args)



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/logging.rb', line 287

def init( *args )
  args = %w(debug info warn error fatal) if args.empty?

  args.flatten!
  levels = LEVELS.clear
  names = LNAMES.clear

  id = 0
  args.each do |lvl|
    lvl = levelify lvl
    unless levels.has_key?(lvl) or lvl == 'all' or lvl == 'off'
      levels[lvl] = id
      names[id] = lvl.upcase
      id += 1
    end
  end

  longest = names.inject {|x,y| (x.length > y.length) ? x : y}
  longest = 'off' if longest.length < 3
  module_eval "MAX_LEVEL_LENGTH = #{longest.length}", __FILE__, __LINE__

  initialize_plugins
  levels.keys
end

+ (Boolean) initialized?

Return true if the Logging framework is initialized.

Returns:

  • (Boolean)


523
524
525
# File 'lib/logging.rb', line 523

def initialized?
  const_defined? :MAX_LEVEL_LENGTH
end

+ (Object) layouts



153
154
155
# File 'lib/logging.rb', line 153

def layouts
  ::Logging::Layouts
end

+ (Object) level_num(level)



487
488
489
490
491
492
493
# File 'lib/logging.rb', line 487

def level_num( level )
  l = levelify(level) rescue level
  case l
  when 'all'; 0
  when 'off'; LEVELS.length
  else begin; Integer(l); rescue ArgumentError; LEVELS[l] end end
end

+ (Object) levelify(level)



479
480
481
482
483
484
# File 'lib/logging.rb', line 479

def levelify( level )
  case level
  when String; level.downcase
  when Symbol; level.to_s.downcase
  else raise ArgumentError, "levels must be a String or Symbol" end
end

+ (Object) libpath(*args, &block)



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/logging.rb', line 374

def libpath( *args, &block )
  rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
  if block
    begin
      $LOAD_PATH.unshift LIBPATH
      rv = block.call
    ensure
      $LOAD_PATH.shift
    end
  end
  return rv
end

+ (Object) log_internal(level = 1, &block)



496
497
498
# File 'lib/logging.rb', line 496

def log_internal( level = 1, &block )
  ::Logging::Logger[::Logging].__send__(levelify(LNAMES[level]), &block)
end

+ (Object) logger(*args)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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

+ (Object) mdc

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

Returns MappedDiagnosticContext



243
# File 'lib/logging/diagnostic_context.rb', line 243

def self.mdc() MappedDiagnosticContext end

+ (Object) ndc

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

Returns NestedDiagnosticContext



250
# File 'lib/logging/diagnostic_context.rb', line 250

def self.ndc() NestedDiagnosticContext end

+ (Object) path(*args, &block)



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/logging.rb', line 391

def path( *args, &block )
  rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
  if block
    begin
      $LOAD_PATH.unshift PATH
      rv = block.call
    ensure
      $LOAD_PATH.shift
    end
  end
  return rv
end

+ (Object) reopen



182
183
184
185
186
# File 'lib/logging.rb', line 182

def reopen
  log_internal {'re-opening all appenders'}
  ::Logging::Appenders.each {|appender| appender.reopen}
  self
end

+ (Object) reset

Reset the Logging framework to it's uninitialized state



509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/logging.rb', line 509

def reset
  ::Logging::Repository.reset
  ::Logging::Appenders.reset
  ::Logging::ColorScheme.reset
  ::Logging.clear_diagnostic_contexts(true)
  LEVELS.clear
  LNAMES.clear
  remove_instance_variable :@backtrace if defined? @backtrace
  remove_const :MAX_LEVEL_LENGTH if const_defined? :MAX_LEVEL_LENGTH
  remove_const :OBJ_FORMAT if const_defined? :OBJ_FORMAT
  self
end

+ (Object) show_configuration(io = STDOUT, logger = 'root', indent = 0)



463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/logging.rb', line 463

def show_configuration( io = STDOUT, logger = 'root', indent = 0 )
  logger = ::Logging::Logger[logger] unless ::Logging::Logger === logger

  logger._dump_configuration(io, indent)

  indent += 2
  children = ::Logging::Repository.instance.children(logger.name)
  children.sort {|a,b| a.name <=> b.name}.each do |child|
    ::Logging.show_configuration(io, child, indent)
  end

  self
end

+ (Object) shutdown(*args)



501
502
503
504
505
506
# File 'lib/logging.rb', line 501

def shutdown( *args )
  return unless initialized?
  log_internal {'shutdown called - closing all appenders'}
  ::Logging::Appenders.each {|appender| appender.close}
  nil
end

+ (Object) version



366
367
368
# File 'lib/logging.rb', line 366

def version
  @version ||= File.read(path('version.txt')).strip
end