Class: Logger::Syslog

Inherits:
Object
  • Object
show all
Includes:
Severity
Defined in:
lib/syslog-logger.rb

Constant Summary collapse

VERSION =

The version of Logger::Syslog you are using.

'1.6.8'
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.

{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Fills in variables for Logger compatibility. If this is the first instance of Logger::Syslog, 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.



96
97
98
99
100
101
102
103
104
# File 'lib/syslog-logger.rb', line 96

def initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts=nil)
  @default_formatter = Logger::SyslogFormatter.new
  @formatter         = nil
  @progname          = nil
  @level             = Logger::DEBUG

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

Instance Attribute Details

#formatterObject

Logging formatter. formatter#call is invoked with 4 arguments; severity, time, progname and msg for each log. Bear in mind that time is a Time and msg is an Object that user passed and it could not be a String. It is expected to return a logdev#write-able Object. Default formatter is used when no formatter is set.



85
86
87
# File 'lib/syslog-logger.rb', line 85

def formatter
  @formatter
end

#levelObject Also known as: sev_threshold

Log level for Logger compatibility.



66
67
68
# File 'lib/syslog-logger.rb', line 66

def level
  @level
end

#prognameObject

Logging program name.



69
70
71
# File 'lib/syslog-logger.rb', line 69

def progname
  @progname
end

Instance Method Details

#<<(message) ⇒ Object

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



135
136
137
# File 'lib/syslog-logger.rb', line 135

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

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

Almost duplicates Logger#add. progname is ignored.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/syslog-logger.rb', line 107

def add(severity, message = nil, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  if severity < @level
    return true
  end
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end
  SYSLOG.send(LEVEL_LOGGER_MAP[severity], format_message(format_severity(severity), Time.now, progname, clean(message)))
  true
end

#datetime_formatObject



76
77
78
# File 'lib/syslog-logger.rb', line 76

def datetime_format
  @default_formatter.datetime_format
end

#datetime_format=(datetime_format) ⇒ Object

Logging date-time format (string passed to strftime).



72
73
74
# File 'lib/syslog-logger.rb', line 72

def datetime_format=(datetime_format)
  @default_formatter.datetime_format = datetime_format
end

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

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



125
126
127
128
129
130
131
# File 'lib/syslog-logger.rb', line 125

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