Class: Syslog::Logger
- Inherits:
-
Object
- Object
- Syslog::Logger
- Defined in:
- lib/syslog/logger.rb,
lib/syslog/logger/version.rb
Overview
Syslog::Logger is a Logger work-alike that logs via syslog instead of to a file. You can use Syslog::Logger to aggregate logs between multiple machines.
By default, Syslog::Logger uses the program name ‘ruby’, but this can be changed via the first argument to Syslog::Logger.new.
NOTE! You can only set the Syslog::Logger program name when you initialize Syslog::Logger for the first time. This is a limitation of the way Syslog::Logger uses syslog (and in some ways, a limitation of the way syslog(3) works). Attempts to change Syslog::Logger’s program name after the first initialization will be ignored.
Example
The following will log to syslogd on your local machine:
require 'syslog/logger'
log = Syslog::Logger.new 'my_program'
log.info 'this line will be logged via syslog(3)'
You may need to perform some syslog.conf setup first. For a BSD machine add the following lines to /etc/syslog.conf:
!my_program
*.* /var/log/my_program.log
Then touch /var/log/my_program.log and signal syslogd with a HUP (killall -HUP syslogd, on FreeBSD).
If you wish to have logs automatically roll over and archive, see the newsyslog.conf(5) and newsyslog(8) man pages.
Constant Summary collapse
- VERSION =
The version of Syslog::Logger you are using.
'2.0'
- LEVEL_MAP =
Maps Logger warning types to syslog(3) warning types.
Messages from ruby applications are not considered as critical as messages from other system daemons using syslog(3), so most messages are reduced by one level. For example, a fatal message for ruby’s Logger is considered an error for syslog(3).
{ ::Logger::UNKNOWN => Syslog::LOG_ALERT, ::Logger::FATAL => Syslog::LOG_ERR, ::Logger::ERROR => Syslog::LOG_WARNING, ::Logger::WARN => Syslog::LOG_NOTICE, ::Logger::INFO => Syslog::LOG_INFO, ::Logger::DEBUG => Syslog::LOG_DEBUG, }
Instance Attribute Summary collapse
-
#level ⇒ Object
Log level for Logger compatibility.
Class Method Summary collapse
-
.make_methods(meth) ⇒ Object
Builds a methods for level
meth
. -
.syslog ⇒ Object
Returns the internal Syslog object that is initialized when the first instance is created.
-
.syslog=(syslog) ⇒ Object
Specifies the internal Syslog object to be used.
Instance Method Summary collapse
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add.
-
#initialize(program_name = 'ruby') ⇒ Logger
constructor
Fills in variables for Logger compatibility.
-
#severity ⇒ Object
:method: debug.
Constructor Details
#initialize(program_name = 'ruby') ⇒ Logger
Fills in variables for Logger compatibility. If this is the first instance of Syslog::Logger, program_name
may be set to change the logged program name.
Due to the way syslog works, only one program name may be chosen.
146 147 148 149 150 |
# File 'lib/syslog/logger.rb', line 146 def initialize program_name = 'ruby' @level = ::Logger::DEBUG @@syslog ||= Syslog.open(program_name) end |
Instance Attribute Details
#level ⇒ Object
Log level for Logger compatibility.
137 138 139 |
# File 'lib/syslog/logger.rb', line 137 def level @level end |
Class Method Details
.make_methods(meth) ⇒ Object
Builds a methods for level meth
.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/syslog/logger.rb', line 81 def self.make_methods meth level = ::Logger.const_get(meth.upcase) eval <<-EOM, nil, __FILE__, __LINE__ + 1 def #{meth}(message = nil, &block) add(#{level}, message, &block) end def #{meth}? @level <= #{level} end EOM end |
.syslog ⇒ Object
Returns the internal Syslog object that is initialized when the first instance is created.
67 68 69 |
# File 'lib/syslog/logger.rb', line 67 def self.syslog @@syslog end |
.syslog=(syslog) ⇒ Object
Specifies the internal Syslog object to be used.
74 75 76 |
# File 'lib/syslog/logger.rb', line 74 def self.syslog= syslog @@syslog = syslog end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add. progname
is ignored.
155 156 157 158 159 160 |
# File 'lib/syslog/logger.rb', line 155 def add severity, = nil, progname = nil, &block severity ||= ::Logger::UNKNOWN @level <= severity and @@syslog.log LEVEL_MAP[severity], '%s', clean( || block.call) true end |
#severity ⇒ Object
:method: debug
Logs a message
at the debug (syslog debug) log level, or logs the message returned from the block.
130 131 132 |
# File 'lib/syslog/logger.rb', line 130 Logger::Severity::constants.each do |severity| make_methods severity.downcase end |