Class: SyslogLogger

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/syslog_logger_env_formatting.rb,
lib/syslog_logger.rb

Overview

Adds some default information to syslog messages. If you pass a User or Device object as the first parameter, it will log that user/device’s id If you pass a block from a controller, it will automatically log the current user id If you pass a block, the class name of the block’s context will be added

Examples logger.debug “O’Doyle Rules!”

#=> [development] [DEBUG: 2008-01-25 14:16:12.12345] O'Doyle Rules!

#from within a controller… logger.error is messed up!”

#=> [development] [ERROR: 2008-01-25 14:16:12.12345] [123] [ClassName] Something is messed up!

Constant Summary collapse

VERSION =

The version of SyslogLogger you are using.

'1.6.1'
LOGGER_MAP =

Maps Logger warning types to syslog(3) warning types.

{
  :unknown => :alert,
  :fatal   => :crit,
  :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.

{}
LOG_NAME_FIELD_WIDTH =
7
@@log_level_names =

short names for “DEBUG”, “INFO”, … must be ordered to correspond to severity constants defined in ActiveSupport::BufferedLogger::Severity @@log_level_names = %w( D I W E F U ) LOG_NAME_FIELD_WIDTH = 1

%w( DEBUG INFO WARN ERROR FATAL UNKNOWN )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts = nil) ⇒ SyslogLogger

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, only one program name may be chosen.



73
74
75
76
77
78
# File 'lib/syslog_logger.rb', line 73

def initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts=nil)
  @level = Logger::DEBUG

  return if defined? SYSLOG
  self.class.const_set :SYSLOG, Syslog.open(program_name, logopts, facility)
end

Instance Attribute Details

#levelObject

Log level for Logger compatibility.



65
66
67
# File 'lib/syslog_logger.rb', line 65

def level
  @level
end

Instance Method Details

#<<(message) ⇒ Object

In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN



101
102
103
# File 'lib/syslog_logger.rb', line 101

def <<(message)
  add(Logger::UNKNOWN, message)
end

#add(severity, message = nil, progname = nil, &block) ⇒ Object

Almost duplicates Logger#add. progname is ignored.



81
82
83
84
85
86
87
88
# File 'lib/syslog_logger.rb', line 81

def add(severity, message = nil, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  if severity >= @level
    message = clean(message || block.call)
    SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message)
  end
  true
end

#add_with_formatting(severity, message = nil, progname = nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syslog_logger_env_formatting.rb', line 25

def add_with_formatting(severity, message = nil, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  message = "[#{RAILS_ENV}] [#{@@log_level_names[severity].ljust(LOG_NAME_FIELD_WIDTH)}: #{time.strftime("%Y-%m-%d %H:%M:%S")}.#{time.usec.to_s.rjust(6, '0')}] #{message}"

  if(block)
    add_without_formatting(severity, message, progname,
      &Proc.new{g_log_formatter(severity, nil, user, &block)})
  else
    add_without_formatting(severity, message, progname)
  end
end

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

Allows messages of a particular log level to be ignored temporarily.



91
92
93
94
95
96
97
# File 'lib/syslog_logger.rb', line 91

def silence(temporary_level = Logger::ERROR)
  old_logger_level = @level
  @level = temporary_level
  yield
ensure
  @level = old_logger_level
end