Class: Logger

Inherits:
Object show all
Defined in:
lib/active_support/core_ext/logger.rb,
lib/active_support/core_ext/logger.rb

Overview

Extensions to the built-in Ruby logger.

If you want to use the default log formatter as defined in the Ruby core, then you will need to set the formatter for the logger as in:

logger.formatter = Formatter.new

You can then specify the datetime format, for example:

logger.datetime_format = "%Y-%m-%d"

Note: This logger is deprecated in favor of ActiveSupport::BufferedLogger

Defined Under Namespace

Classes: SimpleFormatter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

Overwrite initialize to set a default formatter.



71
72
73
74
# File 'lib/active_support/core_ext/logger.rb', line 71

def initialize(*args)
  old_initialize(*args)
  self.formatter = SimpleFormatter.new
end

Class Method Details

.define_around_helper(level) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
# File 'lib/active_support/core_ext/logger.rb', line 6

def self.define_around_helper(level)
  module_eval <<-end_eval, __FILE__, __LINE__ + 1
    def around_#{level}(before_message, after_message)  # def around_debug(before_message, after_message, &block)
      self.#{level}(before_message)                     #   self.debug(before_message)
      return_value = yield(self)                        #   return_value = yield(self)
      self.#{level}(after_message)                      #   self.debug(after_message)
      return_value                                      #   return_value
    end                                                 # end
  end_eval
end

Instance Method Details

#datetime_formatObject

Get the logging datetime format. Returns nil if the formatter does not support datetime formatting.



65
66
67
# File 'lib/active_support/core_ext/logger.rb', line 65

def datetime_format
  formatter.datetime_format if formatter.respond_to?(:datetime_format)
end

#datetime_format=(datetime_format) ⇒ Object

Logging date-time format (string passed to strftime). Ignored if the formatter does not respond to datetime_format=.



58
59
60
# File 'lib/active_support/core_ext/logger.rb', line 58

def datetime_format=(datetime_format)
  formatter.datetime_format = datetime_format if formatter.respond_to?(:datetime_format=)
end

#old_datetime_formatObject



62
# File 'lib/active_support/core_ext/logger.rb', line 62

alias :old_datetime_format :datetime_format

#old_datetime_format=Object



55
# File 'lib/active_support/core_ext/logger.rb', line 55

alias :old_datetime_format= :datetime_format=

#old_initializeObject



69
# File 'lib/active_support/core_ext/logger.rb', line 69

alias :old_initialize :initialize

#silence(temporary_level = Logger::ERROR) ⇒ Object

Silences the logger for the duration of the block.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_support/core_ext/logger.rb', line 41

def silence(temporary_level = Logger::ERROR)
  if silencer
    begin
      old_logger_level, self.level = level, temporary_level
      yield self
    ensure
      self.level = old_logger_level
    end
  else
    yield self
  end
end

#silencerObject

:singleton-method: Set to false to disable the silencer



37
# File 'lib/active_support/core_ext/logger.rb', line 37

cattr_accessor :silencer