Class: TcpSyslog
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- TcpSyslog
- Includes:
- Logger::Severity
- Defined in:
- lib/tcp_syslog.rb
Overview
TcpSyslog is used are a dead-simple replacement for syslog ruby libs. None of them is able to send logs to a remote server, and even less in TCP.
Example:
For rails (2.X) :
config.logger = TcpSyslog.new(host => 'localhost')
For more info about Syslog protocol, please refer to the RFC: www.faqs.org/rfcs/rfc3164.html
Parts taken frm SyslogLogger gem and ActiveSupport
Constant Summary collapse
- LOGGER_MAP =
Maps Logger warning types to syslog(3) warning types.
{ :unknown => Syslog::LOG_ALERT, :fatal => Syslog::LOG_CRIT, :error => Syslog::LOG_ERR, :warn => Syslog::LOG_WARNING, :info => Syslog::LOG_INFO, :debug => Syslog::LOG_DEBUG }
- LOGGER_LEVEL_MAP =
Maps Logger log levels to their values so we can silence.
{}
- LEVEL_LOGGER_MAP =
Maps Logger log level values to syslog log levels.
{}
Instance Attribute Summary collapse
-
#auto_flushing ⇒ Object
readonly
Log level for Logger compatibility.
-
#facility ⇒ Object
readonly
Log level for Logger compatibility.
-
#host ⇒ Object
readonly
Log level for Logger compatibility.
-
#port ⇒ Object
readonly
Log level for Logger compatibility.
-
#progname ⇒ Object
readonly
Log level for Logger compatibility.
Instance Method Summary collapse
-
#<<(message) ⇒ Object
In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN.
-
#add(severity, message, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add.
- #close ⇒ Object
-
#flush ⇒ Object
Flush buffered logs to Syslog.
-
#initialize(options = {}) ⇒ TcpSyslog
constructor
Usage : *
options
: A hash with the following options **host
: defaults to ‘localhost’ **port
: defaults to ‘514’ **facility
: defaults to user **progname
: defaults to ‘rails’ **auto_flushing
: number of messages to buffer before flushing. - #socket ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ TcpSyslog
Usage :
-
options
: A hash with the following options
** host
: defaults to ‘localhost’ ** port
: defaults to ‘514’ ** facility
: defaults to user ** progname
: defaults to ‘rails’ ** auto_flushing
: number of messages to buffer before flushing
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tcp_syslog.rb', line 73 def initialize( = {}) @level = LOGGER_LEVEL_MAP[[:level]] || Logger::DEBUG @host = [:host] || 'localhost' @port = [:port] = '514' @facility = [:facility] || Syslog::LOG_USER @progname = [:progname] || 'rails' @buffer = Hash.new { |h,k| h[k] = [] } @socket = {} @auto_flushing = [:auto_flushing] || 1 @local_ip = local_ip @remove_ansi_colors = [:remove_ansi_colors] || true return if defined? SYSLOG self.class.const_set :SYSLOG, true end |
Instance Attribute Details
#auto_flushing ⇒ Object (readonly)
Log level for Logger compatibility.
90 91 92 |
# File 'lib/tcp_syslog.rb', line 90 def auto_flushing @auto_flushing end |
#facility ⇒ Object (readonly)
Log level for Logger compatibility.
90 91 92 |
# File 'lib/tcp_syslog.rb', line 90 def facility @facility end |
#host ⇒ Object (readonly)
Log level for Logger compatibility.
90 91 92 |
# File 'lib/tcp_syslog.rb', line 90 def host @host end |
#port ⇒ Object (readonly)
Log level for Logger compatibility.
90 91 92 |
# File 'lib/tcp_syslog.rb', line 90 def port @port end |
#progname ⇒ Object (readonly)
Log level for Logger compatibility.
90 91 92 |
# File 'lib/tcp_syslog.rb', line 90 def progname @progname end |
Instance Method Details
#<<(message) ⇒ Object
In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN
105 106 107 |
# File 'lib/tcp_syslog.rb', line 105 def <<() add(Logger::UNKNOWN, ) end |
#add(severity, message, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add.
94 95 96 97 98 99 100 101 |
# File 'lib/tcp_syslog.rb', line 94 def add(severity, , progname = nil, &block) severity ||= Logger::UNKNOWN return if @level > severity = clean( || block.call) buffer << {:severity => severity, :body => clean()} auto_flush end |
#close ⇒ Object
109 110 111 112 113 |
# File 'lib/tcp_syslog.rb', line 109 def close flush socket.close @socket[Thread.current] = nil end |
#flush ⇒ Object
Flush buffered logs to Syslog
116 117 118 119 120 121 |
# File 'lib/tcp_syslog.rb', line 116 def flush buffer.each do || log([:severity], [:body]) end clear_buffer end |
#socket ⇒ Object
123 124 125 |
# File 'lib/tcp_syslog.rb', line 123 def socket @socket[Thread.current] ||= TCPSocket.new(@host, @port) end |