Class: BufferedRemoteSyslogLogger::TcpLogger
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- BufferedRemoteSyslogLogger::TcpLogger
- Includes:
- Logger::Severity
- Defined in:
- lib/buffered_remote_syslog_logger/tcp_logger.rb
Defined Under Namespace
Classes: SocketTimeoutError
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 }
- SOCKET_SEND_RETRY_ATTEMPTS =
2- 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.
{}
- OPTION_DEFAULTS =
{ :level => Logger::DEBUG, :host => '127.0.0.1', :port => '514', :facility => Syslog::LOG_USER, :program_name => 'buffered_remote_syslog_logger', :auto_flushing => 1, :remove_ansi_colors => true }
Instance Attribute Summary collapse
-
#auto_flushing ⇒ Object
Returns the value of attribute auto_flushing.
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#facility ⇒ Object
Returns the value of attribute facility.
-
#host ⇒ Object
Returns the value of attribute host.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#level ⇒ Object
Returns the value of attribute level.
-
#port ⇒ Object
Returns the value of attribute port.
-
#program_name ⇒ Object
Returns the value of attribute program_name.
-
#remove_ansi_colors ⇒ Object
Returns the value of attribute remove_ansi_colors.
-
#socket ⇒ Object
Returns the value of attribute socket.
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.
- #auto_flush ⇒ Object
- #close ⇒ Object
-
#flush ⇒ Object
Flush buffered logs to Syslog.
-
#initialize(options = {}) ⇒ TcpLogger
constructor
Usage : *
options: A hash with the following options **level: severity level, defaults to debug **host: syslog daemon host, defaults to ‘localhost’ **port: syslog daemon port, defaults to ‘514’ **facility: used syslog facility, defaults to log_user **program_name: name of the application, defaults to ‘OPTION_DEFAULTS’ **auto_flushing: number of messages to buffer before flushing, defaults to 1 (always flush right away) **remove_ansi_colors: boolean to determine, whether or not to remove ansi colors, defaults to true.
Constructor Details
#initialize(options = {}) ⇒ TcpLogger
Usage :
-
options: A hash with the following options
** level : severity level, defaults to debug ** host : syslog daemon host, defaults to ‘localhost’ ** port : syslog daemon port, defaults to ‘514’ ** facility : used syslog facility, defaults to log_user ** program_name : name of the application, defaults to ‘OPTION_DEFAULTS’ ** auto_flushing : number of messages to buffer before flushing, defaults to 1 (always flush right away) ** remove_ansi_colors : boolean to determine, whether or not to remove ansi colors, defaults to true
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 95 def initialize( = {}) @buffer = [] @socket = {} = OPTION_DEFAULTS.merge(Hash[.map{ |key, value| [key.to_sym, value] }]) [:host, :port, :facility, :program_name, :auto_flushing, :remove_ansi_colors].each do |key| self.public_send("#{key}=", [key]) end @level = LEVEL_LOGGER_MAP[[:level]] @hostname = `hostname` return if defined? SYSLOG self.class.const_set :SYSLOG, true end |
Instance Attribute Details
#auto_flushing ⇒ Object
Returns the value of attribute auto_flushing.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def auto_flushing @auto_flushing end |
#buffer ⇒ Object
Returns the value of attribute buffer.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def buffer @buffer end |
#facility ⇒ Object
Returns the value of attribute facility.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def facility @facility end |
#host ⇒ Object
Returns the value of attribute host.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def host @host end |
#hostname ⇒ Object
Returns the value of attribute hostname.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def hostname @hostname end |
#level ⇒ Object
Returns the value of attribute level.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def level @level end |
#port ⇒ Object
Returns the value of attribute port.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def port @port end |
#program_name ⇒ Object
Returns the value of attribute program_name.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def program_name @program_name end |
#remove_ansi_colors ⇒ Object
Returns the value of attribute remove_ansi_colors.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def remove_ansi_colors @remove_ansi_colors end |
#socket ⇒ Object
Returns the value of attribute socket.
83 84 85 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 83 def socket @socket end |
Instance Method Details
#<<(message) ⇒ Object
In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN
132 133 134 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 132 def <<() add(Logger::UNKNOWN, ) end |
#add(severity, message, progname = nil, &block) ⇒ Object
Almost duplicates Logger#add.
114 115 116 117 118 119 120 121 122 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 114 def add(severity, , progname = nil, &block) severity ||= Logger::UNKNOWN return if @level < severity = clean( || block.call) @buffer << {:severity => severity, :body => clean()} auto_flush end |
#auto_flush ⇒ Object
125 126 127 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 125 def auto_flush flush if 0 == @auto_flushing || @auto_flushing <= @buffer.count end |
#close ⇒ Object
137 138 139 140 141 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 137 def close # flush # socket.close # @socket[Thread.current] = nil end |
#flush ⇒ Object
Flush buffered logs to Syslog
145 146 147 148 149 150 151 |
# File 'lib/buffered_remote_syslog_logger/tcp_logger.rb', line 145 def flush @buffer.each do || log([:severity], [:body]) end @buffer.clear nil end |