Class: Syslogger
- Inherits:
-
Object
- Object
- Syslogger
- Defined in:
- lib/syslogger.rb
Constant Summary collapse
- VERSION =
"1.6.3"- MUTEX =
Mutex.new
- MAPPING =
{ Logger::DEBUG => Syslog::LOG_DEBUG, Logger::INFO => Syslog::LOG_INFO, Logger::WARN => Syslog::LOG_WARNING, Logger::ERROR => Syslog::LOG_ERR, Logger::FATAL => Syslog::LOG_CRIT, Logger::UNKNOWN => Syslog::LOG_ALERT }
Instance Attribute Summary collapse
-
#facility ⇒ Object
readonly
Returns the value of attribute facility.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#ident ⇒ Object
Returns the value of attribute ident.
-
#level ⇒ Object
Returns the value of attribute level.
-
#max_octets ⇒ Object
Returns the value of attribute max_octets.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
Logs a message at the Logger::INFO level.
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Low level method to add a message.
- #clear_tags! ⇒ Object
-
#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ Syslogger
constructor
- Initializes default options for the logger
ident -
the name of your program [default=$0].
- Initializes default options for the logger
- #pop_tags(size = 1) ⇒ Object
- #push_tags(*tags) ⇒ Object
-
#tagged(*tags) ⇒ Object
Tagging code borrowed from ActiveSupport gem.
-
#write(msg) ⇒ Object
Log a message at the Logger::INFO level.
Constructor Details
#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ Syslogger
Initializes default options for the logger
ident-
the name of your program [default=$0].
options-
syslog options [default=
Syslog::LOG_PID | Syslog::LOG_CONS]. Correct values are:LOG_CONS : writes the on the console if an error occurs when sending the ; LOG_NDELAY : no delay before sending the ; LOG_PERROR : will also be written on STDERR; LOG_PID : adds the process number to the (just after the program name) facility-
the syslog facility [default=nil] Correct values include:
Syslog::LOG_DAEMON Syslog::LOG_USER Syslog::LOG_SYSLOG Syslog::LOG_LOCAL2 Syslog::LOG_NEWS etc.
Usage:
logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
logger.level = Logger::INFO # use Logger levels
logger.warn "warning message"
logger.debug "debug message"
logger.info "my_subapp" { "Some lazily computed message" }
47 48 49 50 51 52 53 54 55 |
# File 'lib/syslogger.rb', line 47 def initialize(ident = $0, = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) @ident = ident = || (Syslog::LOG_PID | Syslog::LOG_CONS) @facility = facility @level = Logger::INFO @formatter = proc do |severity, datetime, progname, msg| msg end end |
Instance Attribute Details
#facility ⇒ Object (readonly)
Returns the value of attribute facility.
11 12 13 |
# File 'lib/syslogger.rb', line 11 def facility @facility end |
#formatter ⇒ Object
Returns the value of attribute formatter.
12 13 14 |
# File 'lib/syslogger.rb', line 12 def formatter @formatter end |
#ident ⇒ Object
Returns the value of attribute ident.
11 12 13 |
# File 'lib/syslogger.rb', line 11 def ident @ident end |
#level ⇒ Object
Returns the value of attribute level.
11 12 13 |
# File 'lib/syslogger.rb', line 11 def level @level end |
#max_octets ⇒ Object
Returns the value of attribute max_octets.
11 12 13 |
# File 'lib/syslogger.rb', line 11 def max_octets @max_octets end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/syslogger.rb', line 11 def end |
Instance Method Details
#<<(msg) ⇒ Object
Logs a message at the Logger::INFO level.
79 80 81 |
# File 'lib/syslogger.rb', line 79 def <<(msg) add(Logger::INFO, msg) end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Low level method to add a message.
severity-
the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
message-
the message string. If nil, the method will call the block and use the result as the message string. If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.
progname-
optionally, overwrite the program name that appears in the log message.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/syslogger.rb', line 89 def add(severity, = nil, progname = nil, &block) if .nil? && block.nil? && !progname.nil? , progname = progname, nil end progname ||= @ident mask = Syslog::LOG_UPTO(MAPPING[@level]) communication = clean( || block && block.call) formatted_communication = formatter.call([severity], Time.now, progname, communication) MUTEX.synchronize do Syslog.open(progname, , @facility) do |s| s.mask = mask if self.max_octets buffer = "#{tags_text}" formatted_communication.bytes do |byte| buffer.concat(byte) # if the last byte we added is potentially part of an escape, we'll go ahead and add another byte if buffer.bytesize >= self.max_octets && !['%'.ord,'\\'.ord].include?(byte) s.log(MAPPING[severity],buffer) buffer = "" end end s.log(MAPPING[severity],buffer) unless buffer.empty? else s.log(MAPPING[severity],"#{tags_text}#{formatted_communication}") end end end end |
#clear_tags! ⇒ Object
159 160 161 |
# File 'lib/syslogger.rb', line 159 def .clear end |
#pop_tags(size = 1) ⇒ Object
155 156 157 |
# File 'lib/syslogger.rb', line 155 def (size = 1) .pop size end |
#push_tags(*tags) ⇒ Object
149 150 151 152 153 |
# File 'lib/syslogger.rb', line 149 def (*) .flatten.reject{ |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do || .concat end end |
#tagged(*tags) ⇒ Object
Tagging code borrowed from ActiveSupport gem
142 143 144 145 146 147 |
# File 'lib/syslogger.rb', line 142 def tagged(*) = (*) yield self ensure (.size) end |
#write(msg) ⇒ Object
Log a message at the Logger::INFO level. Useful for use with Rack::CommonLogger
74 75 76 |
# File 'lib/syslogger.rb', line 74 def write(msg) add(Logger::INFO, msg) end |