Class: Slogger::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/slogger/base.rb

Direct Known Subclasses

CommonLogger, Logger

Constant Summary collapse

SYSLOG_SEVERITIES =

Syslog Message Severities:

  • Emergency: system is unusable

  • Alert: action must be taken immediately

  • Critical: critical conditions

  • Error: error conditions

  • Warning: warning conditions

  • Notice: normal but significant condition

  • Informational: informational messages

  • Debug: debug-level messages

{
  :emerg   => Syslog::LOG_EMERG,
  :alert   => Syslog::LOG_ALERT,
  :crit    => Syslog::LOG_CRIT,
  :err     => Syslog::LOG_ERR,
  :warning => Syslog::LOG_WARNING,
  :notice  => Syslog::LOG_NOTICE,
  :info    => Syslog::LOG_INFO,
  :debug   => Syslog::LOG_DEBUG
}
SYSLOG_FACILITIES =

Syslog Message Facilities:

  • kernel messages

  • user-level messages

  • mail system

  • system daemons

  • security/authorization messages

  • messages generated internally by syslogd

  • line printer subsystem

  • network news subsystem

  • UUCP subsystem

  • clock daemon

  • security/authorization messages

  • FTP daemon

  • NTP subsystem

  • log audit

  • log alert

  • clock daemon (note 2)

  • local use 0 (local0)

  • local use 1 (local1)

  • local use 2 (local2)

  • local use 3 (local3)

  • local use 4 (local4)

  • local use 5 (local5)

  • local use 6 (local6)

  • local use 7 (local7)

{
  :kernel   => Syslog::LOG_KERN,
  :user     => Syslog::LOG_USER,
  :mail     => Syslog::LOG_MAIL,
  :daemon   => Syslog::LOG_DAEMON,
  :auth     => Syslog::LOG_AUTH,
  :syslog   => Syslog::LOG_SYSLOG,
  :lpr      => Syslog::LOG_LPR,
  :news     => Syslog::LOG_NEWS,
  :uucp     => Syslog::LOG_UUCP,
  :cron     => Syslog::LOG_CRON,
  :authpriv => Syslog::LOG_AUTHPRIV,
  :ftp      => Syslog::LOG_FTP,
  :local0   => Syslog::LOG_LOCAL0,
  :local1   => Syslog::LOG_LOCAL1,
  :local2   => Syslog::LOG_LOCAL2,
  :local3   => Syslog::LOG_LOCAL3,
  :local4   => Syslog::LOG_LOCAL4,
  :local5   => Syslog::LOG_LOCAL5,
  :local6   => Syslog::LOG_LOCAL6,
  :local7   => Syslog::LOG_LOCAL7
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, severity, facility, custom_severity_levels = SYSLOG_SEVERITIES) ⇒ Base

To build a Slogger::Base instance.

app_name

The appliaction name to be logged

severity

The log severity.

facility

A typical syslog facility

custom_severity_levels

To be used by children classes. It defaults to

Slogger::Base::SYSLOG_SEVERITIES.

Raises an ArgumentError if app_name, severity, or facility is nil.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/slogger/base.rb', line 93

def initialize(app_name, severity, facility, custom_severity_levels=SYSLOG_SEVERITIES)
  raise_argument_error_to_required_parameter "app_name" unless app_name
  raise_argument_error_to_required_parameter "severity" unless severity
  raise_argument_error_to_required_parameter "facility" unless facility

  raise_argument_error_to_invalid_parameter "severity", "SEVERITIES" unless custom_severity_levels[severity]
  raise_argument_error_to_invalid_parameter "facility", "FACILITIES" unless SYSLOG_FACILITIES[facility]

  @app_name = app_name
  @severity = severity
  @severity_as_int = custom_severity_levels[severity]
  @facility = facility
  @facility_as_int = SYSLOG_FACILITIES[facility]
  @custom_severity_levels = custom_severity_levels
  @mutex = Mutex.new
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



80
81
82
# File 'lib/slogger/base.rb', line 80

def app_name
  @app_name
end

#facilityObject (readonly)

Returns the value of attribute facility.



80
81
82
# File 'lib/slogger/base.rb', line 80

def facility
  @facility
end

#severityObject

Returns the value of attribute severity.



80
81
82
# File 'lib/slogger/base.rb', line 80

def severity
  @severity
end

Instance Method Details

#log(severity, message, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/slogger/base.rb', line 117

def log(severity, message, &block)
  return if SYSLOG_SEVERITIES[severity] > @severity_as_int

  if block_given?
    benchmark = Benchmark.measure &block
    message = "[time: #{benchmark.real}] #{message}"
  end
  
  @mutex.synchronize do
    Syslog.open(@app_name, Syslog::LOG_PID, @facility_as_int) { |s| s.send severity, '%s', message }
  end
end

#raise_argument_error_to_invalid_parameter(param, options) ⇒ Object

Raises:

  • (ArgumentError)


134
135
136
# File 'lib/slogger/base.rb', line 134

def raise_argument_error_to_invalid_parameter(param, options)
  raise ArgumentError, "The '#{param}' parameter is invalid. Inspect the #{options} constant to know the options."
end

#raise_argument_error_to_required_parameter(param) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
# File 'lib/slogger/base.rb', line 130

def raise_argument_error_to_required_parameter(param)
  raise ArgumentError, "The '#{param}' parameter is required."
end