Class: Log4jruby::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/log4jruby/rails.rb

Overview

Configure Rails logging from a config/initializers file

Setting up log4j using config.logger from within the Rails initialization process may not be possible if the CLASSPATH has not yet been setup. This class can be used to configure the logging from within a config/initializers/ file.

ex.

require 'log4jruby/rails'
Log4jruby::Rails.configure do |c|
  c.logger_name = 'MyApp'
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Rails

:nodoc:

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
# File 'lib/log4jruby/rails.rb', line 25

def initialize #:nodoc:
  @logger_name = 'Rails'
  @tracing = (::Rails.env != 'production')

  yield(self)
end

Instance Attribute Details

#logger_nameString

Default is ‘Rails’

Returns:

  • (String)

    the current value of logger_name



22
23
24
# File 'lib/log4jruby/rails.rb', line 22

def logger_name
  @logger_name
end

#tracingString

Defaults to false in ‘production’ or true otherwise

Returns:

  • (String)

    the current value of tracing



22
23
24
# File 'lib/log4jruby/rails.rb', line 22

def tracing
  @tracing
end

Class Method Details

.configure {|config| ... } ⇒ Object

Sets rails Base loggers(e.g. ActionController::Base, ActiveRecord::Base, etc)

Yields:

  • (config)

    Block to customize configuration



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/log4jruby/rails.rb', line 38

def configure(&block)
  config = new(&block)

  Logger.root.tracing = config.tracing

  logger = Log4jruby::Logger.get(config.logger_name)

  set_logger('ActionController', logger)
  set_logger('ActiveRecord', logger)
  set_logger('ActiveResource', logger)
  set_logger('ActionMailer', logger)
end