Class: Logger::Syslog

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

Constant Summary collapse

VERSION =

The version of Logger::Syslog you are using.

'1.7.1'
LOGGER_MAP =

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

{
  :unknown => Syslog::LOG_ALERT,
  :fatal   => Syslog::LOG_CRIT,
  :error   => Syslog::LOG_ERR,
  :warn    => Syslog::LOG_WARNING,
  :info    => Syslog::LOG_INFO,
  :debug   => Syslog::LOG_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.



100
101
102
103
104
105
106
107
108
109
# File 'lib/logger-syslog.rb', line 100

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

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

Instance Attribute Details

#facilityObject

This is the facility the logger will log to. You can have several loggers that log to different facilities.



89
90
91
# File 'lib/logger-syslog.rb', line 89

def facility
  @facility
end

#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/logger-syslog.rb', line 85

def formatter
  @formatter
end

#levelObject Also known as: sev_threshold

Log level for Logger compatibility.



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

def level
  @level
end

#prognameObject

Logging program name.



69
70
71
# File 'lib/logger-syslog.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



144
145
146
# File 'lib/logger-syslog.rb', line 144

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

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

Almost duplicates Logger#add. progname is ignored.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/logger-syslog.rb', line 112

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

  # breakup multiple lines into multiple syslog messages
  message.each_line do | msg |
    SYSLOG.log(LEVEL_LOGGER_MAP[severity] | @facility, format_message(format_severity(severity), Time.now, progname, clean(msg)))
  end
  true
end

#datetime_formatObject



76
77
78
# File 'lib/logger-syslog.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/logger-syslog.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.



134
135
136
137
138
139
140
# File 'lib/logger-syslog.rb', line 134

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