Class: Syslogger
- Inherits:
-
Object
- Object
- Syslogger
- Defined in:
- lib/syslogger.rb,
lib/syslogger/version.rb
Constant Summary collapse
- 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 }.freeze
- VERSION =
'1.6.5'.freeze
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
- #current_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
- #puts(msg) ⇒ 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" }
45 46 47 48 49 50 51 52 53 |
# File 'lib/syslogger.rb', line 45 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 |_, _, _, msg| msg end end |
Instance Attribute Details
#facility ⇒ Object (readonly)
Returns the value of attribute facility.
9 10 11 |
# File 'lib/syslogger.rb', line 9 def facility @facility end |
#formatter ⇒ Object
Returns the value of attribute formatter.
10 11 12 |
# File 'lib/syslogger.rb', line 10 def formatter @formatter end |
#ident ⇒ Object
Returns the value of attribute ident.
9 10 11 |
# File 'lib/syslogger.rb', line 9 def ident @ident end |
#level ⇒ Object
Returns the value of attribute level.
9 10 11 |
# File 'lib/syslogger.rb', line 9 def level @level end |
#max_octets ⇒ Object
Returns the value of attribute max_octets.
9 10 11 |
# File 'lib/syslogger.rb', line 9 def max_octets @max_octets end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/syslogger.rb', line 9 def end |
Instance Method Details
#<<(msg) ⇒ Object
Logs a message at the Logger::INFO level.
77 78 79 |
# File 'lib/syslogger.rb', line 77 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.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/syslogger.rb', line 91 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 = || block && block.call formatted_communication = clean(formatter.call(severity, Time.now, progname, communication)) # Call Syslog syslog_add(progname, severity, mask, formatted_communication) end |
#clear_tags! ⇒ Object
138 139 140 |
# File 'lib/syslogger.rb', line 138 def .clear end |
#current_tags ⇒ Object
142 143 144 |
# File 'lib/syslogger.rb', line 142 def Thread.current[:syslogger_tagged_logging_tags] ||= [] end |
#pop_tags(size = 1) ⇒ Object
134 135 136 |
# File 'lib/syslogger.rb', line 134 def (size = 1) .pop size end |
#push_tags(*tags) ⇒ Object
128 129 130 131 132 |
# File 'lib/syslogger.rb', line 128 def (*) .flatten.reject { |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do || .concat().uniq! end end |
#puts(msg) ⇒ Object
81 82 83 |
# File 'lib/syslogger.rb', line 81 def puts(msg) add(Logger::INFO, msg) end |
#tagged(*tags) ⇒ Object
Tagging code borrowed from ActiveSupport gem
121 122 123 124 125 126 |
# File 'lib/syslogger.rb', line 121 def tagged(*) = (*) yield self ensure (.size) end |
#write(msg) ⇒ Object
Log a message at the Logger::INFO level. Useful for use with Rack::CommonLogger
72 73 74 |
# File 'lib/syslogger.rb', line 72 def write(msg) add(Logger::INFO, msg) end |