Class: Hatchet::LoggerAppender

Inherits:
Object
  • Object
show all
Includes:
LevelManager
Defined in:
lib/hatchet/logger_appender.rb

Overview

Public: Class for wrapping a standard Logger with Hatchet’s class/module level log filtering.

Constant Summary

Constants included from LevelManager

Hatchet::LevelManager::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LevelManager

#clear_levels_cache!, #default_level, #enabled?, #level, #levels, #levels=, #levels_cache

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ LoggerAppender

Public: Creates a new Logger appender.

options - The Hash options used to setup the appender (default: {}).

:formatter - The formatter used to format log messages.
:levels    - The configuration of logging levels to the
             class/module level.
:logger    - The Logger messages are sent to. It will have its
             formatter changed to delegate entirely to the
             appender's formatter and its level set to debug so
             that it does not filter out any messages it is
             sent.

block - Optional block that can be used to customize the appender. The

appender itself is passed to the block.

Once the values from the options Hash have been applied and any modifications are made within the block the appender should have its levels, logger, and formatter set.

Yields:

  • (_self)

Yield Parameters:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hatchet/logger_appender.rb', line 40

def initialize(options = {})
  @formatter = options[:formatter]
  @logger = options[:logger]
  @levels = options[:levels] || {}

  yield self if block_given?

  @logger.level = ::Logger::DEBUG
  return unless @logger.respond_to? :formatter

  # Wipe the format of the core Logger. The Rails.logger doesn't have this
  # method for example.
  @logger.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
end

Instance Attribute Details

#formatterObject

Public: The formatter used to format the message before they are sent to the logger.



20
21
22
# File 'lib/hatchet/logger_appender.rb', line 20

def formatter
  @formatter
end

#loggerObject

Public: The Logger the appender encapsulates.



15
16
17
# File 'lib/hatchet/logger_appender.rb', line 15

def logger
  @logger
end

Instance Method Details

#add(level, context, message) ⇒ Object

Internal: Adds a message to the logger.

level - The level of the message. context - The context of the message. message - The unformatted message.

Returns nothing.



65
66
67
68
69
70
# File 'lib/hatchet/logger_appender.rb', line 65

def add(level, context, message)
  @logger.send level, @formatter.format(level, context, message)
rescue => e
  STDERR.puts "Failed to log message for #{context} with appender #{self} - #{level} - #{message}\n"
  STDERR.puts "#{e}\n"
end