Class: PsdLogger
Overview
PsdLogger includes code from SeattleRb’s SyslogLogger
Sample usage with Rails
config/environment/production.rb
Add the following lines:
require 'psd_logger'
RAILS_DEFAULT_LOGGER = PsdLogger.new
Constant Summary collapse
- LOGGER_MAP =
Maps Logger warning types to syslog(3) warning types.
{ :unknown => :alert, :fatal => :alert, :error => :err, :warn => :warning, :info => :info, :debug => :debug }
- LOGGER_LEVEL_MAP =
Maps Logger log levels to their values so we can silence.
{}
- LEVEL_LOGGER_MAP =
Maps Logger log level values to syslog log levels.
{}
Instance Attribute Summary collapse
-
#filter ⇒ Object
Log level for Logger compatibility.
-
#level ⇒ Object
Log level for Logger compatibility.
Instance Method Summary collapse
-
#<<(message) ⇒ Object
In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN.
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add.
-
#initialize(tag_suffix = nil) ⇒ PsdLogger
constructor
Fills in variables for Logger compatibility.
-
#silence(temporary_level = Logger::ERROR) ⇒ Object
Allows messages of a particular log level to be ignored temporarily.
Constructor Details
#initialize(tag_suffix = nil) ⇒ PsdLogger
Fills in variables for Logger compatibility. If this is the first instance of SyslogLogger, program_name
may be set to change the logged program name and facility
may be set to specify a custom facility with your syslog daemon.
Due to the way syslog works, program name may be set once only (needs to reopen otherwise).
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/psd_logger.rb', line 83 def initialize(tag_suffix = nil) return if defined? SYSLOG if tag_suffix.nil? if defined?(Rails) tag_suffix = 'rails' else tag_suffix = 'logger' end end @level = PsdLogger::INFO self.class.const_set :SYSLOG, Syslog.open("psd_#{tag_suffix}") self.debug("PsdLogger.initialize()") end |
Instance Attribute Details
#filter ⇒ Object
Log level for Logger compatibility.
74 75 76 |
# File 'lib/psd_logger.rb', line 74 def filter @filter end |
#level ⇒ Object
Log level for Logger compatibility.
74 75 76 |
# File 'lib/psd_logger.rb', line 74 def level @level end |
Instance Method Details
#<<(message) ⇒ Object
In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN
128 129 130 |
# File 'lib/psd_logger.rb', line 128 def <<() add(Logger::UNKNOWN, ) end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add. progname
is prepended to the beginning of a message.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/psd_logger.rb', line 101 def add(severity, = nil, progname = nil, &block) severity ||= Logger::UNKNOWN if severity >= @level prepend = progname ? "[#{progname}] " : nil prepend ||= @filter ? "[#{@filter}] " : '' = clean( || block.call) if defined?(Rails) and not @APP_PREFIX @APP_PREFIX = "[#{Deployed::APP_NAME rescue Rails.root.split.last}:#{Rails.env}]" end = "#{@APP_PREFIX} #{}" SYSLOG.send LEVEL_LOGGER_MAP[severity], prepend + clean() end true end |
#silence(temporary_level = Logger::ERROR) ⇒ Object
Allows messages of a particular log level to be ignored temporarily.
118 119 120 121 122 123 124 |
# File 'lib/psd_logger.rb', line 118 def silence(temporary_level = Logger::ERROR) old_logger_level = @level @level = temporary_level yield ensure @level = old_logger_level end |