Class: Slogger::CommonLogger

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

Overview

It just exposes Ruby’s Syslog with the same API of Ruby’s standard Logger class. So you can use it in a Rails application, for instance.

For example, add the snippet below to the config/environments/developement.rb of an Rails application:

config.log_level = :info config.logger = Slogger::CommonLogger.new “rappils”, config.log_level, :local0

That’s all. The Rails application will log everything to the standard syslog.

Constant Summary collapse

SEVERITIES =
{
  :emerg   => Syslog::LOG_EMERG,
  :alert   => Syslog::LOG_ALERT,
  :err     => Syslog::LOG_ERR,
  :info    => Syslog::LOG_INFO,
  :debug   => Syslog::LOG_DEBUG,
  :warning => Syslog::LOG_WARNING,
}
BRIDGE_SEVERITIES =

Bridge between standard Ruby Logger and Syslog

{
  :unknow  => :emerg,
  :fatal   => :alert,
  :error   => :err,
  :warn    => :warning,
  :info    => :info,
  :debug   => :debug
}
FACILITIES =

Just a little sugar

::Slogger::Base::SYSLOG_FACILITIES

Constants inherited from Base

Base::SYSLOG_FACILITIES, Base::SYSLOG_SEVERITIES

Instance Attribute Summary collapse

Attributes inherited from Base

#app_name, #facility

Instance Method Summary collapse

Methods inherited from Base

#raise_argument_error_to_invalid_parameter, #raise_argument_error_to_required_parameter

Constructor Details

#initialize(app_name, severity, facility) ⇒ CommonLogger

To build a Slogger::CommonLogger instance.

app_name

The appliaction name to be logged

severity

The log severity (according to standard Ruby Logger): :unknow, :fatal,

:error, :warn, :info, or :debug. It can be changed at anytime.
facility

A typical syslog facility: :kernel, :user, :mail, :daemon, :auth,

:syslog, :lpr, :news, :uucp, :cron, :authpriv, :ftp,
:local0, :local1, :local2, :local3, :local4, :local5,
:local6, or :local7

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



60
61
62
63
# File 'lib/slogger/common_logger.rb', line 60

def initialize(app_name, severity, facility)
  super app_name, BRIDGE_SEVERITIES[severity], facility, SEVERITIES
  @formatter = ::Logger::Formatter.new
end

Instance Attribute Details

#formatterObject (readonly)

For Rails/ActiveSupport 4 compatibility



45
46
47
# File 'lib/slogger/common_logger.rb', line 45

def formatter
  @formatter
end

Instance Method Details

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



85
86
87
88
89
90
91
92
93
# File 'lib/slogger/common_logger.rb', line 85

def add(severity, message = nil, progname = nil, &block)
  (BRIDGE_SEVERITIES.keys - [:unknow]).each do |key|
    if ::Logger.const_get(key.to_s.upcase) == severity
      return log(BRIDGE_SEVERITIES[key], message, &block)
    end
  end

  log(BRIDGE_SEVERITIES[:unkown], message, &block)
end

#levelObject



73
74
75
# File 'lib/slogger/common_logger.rb', line 73

def level
  ::Logger.const_get(BRIDGE_SEVERITIES.to_a.rassoc(@severity)[0].to_s.upcase)
end

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



65
66
67
68
69
70
71
# File 'lib/slogger/common_logger.rb', line 65

def log(severity, message = nil, &block)
  if block_given? and message != nil
    super(severity, message, &block)
  else
    super(severity, (message || (block_given? && block.call) || @app_name), &nil)
  end
end

#severityObject



77
78
79
# File 'lib/slogger/common_logger.rb', line 77

def severity
  BRIDGE_SEVERITIES.to_a.rassoc(@severity)[0]
end

#severity=(value) ⇒ Object



81
82
83
# File 'lib/slogger/common_logger.rb', line 81

def severity=(value)
  super(BRIDGE_SEVERITIES[value])
end