Class: TcpSyslog

Inherits:
ActiveSupport::BufferedLogger
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  @level = LOGGER_LEVEL_MAP[options[:level]] || Logger::DEBUG
  @host = options[:host] || 'localhost'
  @port = options[:port] = '514'
  @facility = options[:facility] || Syslog::LOG_USER
  @progname = options[:progname] || 'rails'
  @buffer = Hash.new { |h,k| h[k] = [] }
  @socket = {}
  @auto_flushing = options[:auto_flushing] || 1
  @local_ip = local_ip
  @remove_ansi_colors = options[:remove_ansi_colors] || true
  return if defined? SYSLOG
  self.class.const_set :SYSLOG, true
end

Instance Attribute Details

#auto_flushingObject (readonly)

Log level for Logger compatibility.



90
91
92
# File 'lib/tcp_syslog.rb', line 90

def auto_flushing
  @auto_flushing
end

#facilityObject (readonly)

Log level for Logger compatibility.



90
91
92
# File 'lib/tcp_syslog.rb', line 90

def facility
  @facility
end

#hostObject (readonly)

Log level for Logger compatibility.



90
91
92
# File 'lib/tcp_syslog.rb', line 90

def host
  @host
end

#portObject (readonly)

Log level for Logger compatibility.



90
91
92
# File 'lib/tcp_syslog.rb', line 90

def port
  @port
end

#prognameObject (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 <<(message)
  add(Logger::UNKNOWN, message)
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, message, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  return if @level > severity
  message = clean(message || block.call)
  buffer << {:severity => severity, :body => clean(message)}
  auto_flush
  message
end

#closeObject



109
110
111
112
113
# File 'lib/tcp_syslog.rb', line 109

def close
  flush
  socket.close
  @socket[Thread.current] = nil
end

#flushObject

Flush buffered logs to Syslog



116
117
118
119
120
121
# File 'lib/tcp_syslog.rb', line 116

def flush
  buffer.each do |message|
    log(message[:severity], message[:body])
  end
  clear_buffer
end

#socketObject



123
124
125
# File 'lib/tcp_syslog.rb', line 123

def socket
  @socket[Thread.current] ||= TCPSocket.new(@host, @port)
end