Class: TcpSyslog

Inherits:
ActiveSupport::BufferedLogger
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/tcp_syslog.rb

Overview

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 ** ssl : defaults to nil *** cert : “/path/to/cert” *** key : “/path/to/key” *** +ca_file : “/path/to/ca_file”



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tcp_syslog.rb', line 77

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
  @ssl = options[:ssl]
  return if defined? SYSLOG
  self.class.const_set :SYSLOG, true
end

Instance Attribute Details

#auto_flushingObject (readonly)

Log level for Logger compatibility.



95
96
97
# File 'lib/tcp_syslog.rb', line 95

def auto_flushing
  @auto_flushing
end

#facilityObject (readonly)

Log level for Logger compatibility.



95
96
97
# File 'lib/tcp_syslog.rb', line 95

def facility
  @facility
end

#hostObject (readonly)

Log level for Logger compatibility.



95
96
97
# File 'lib/tcp_syslog.rb', line 95

def host
  @host
end

#portObject (readonly)

Log level for Logger compatibility.



95
96
97
# File 'lib/tcp_syslog.rb', line 95

def port
  @port
end

#prognameObject (readonly)

Log level for Logger compatibility.



95
96
97
# File 'lib/tcp_syslog.rb', line 95

def progname
  @progname
end

Instance Method Details

#<<(message) ⇒ Object

In Logger, this dumps the raw message; the closest equivalent would be Logger::UNKNOWN



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

def <<(message)
  add(Logger::UNKNOWN, message)
end

#add(severity, message, progname = nil, &block) ⇒ Object

Almost duplicates Logger#add.



99
100
101
102
103
104
105
106
# File 'lib/tcp_syslog.rb', line 99

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



114
115
116
117
118
# File 'lib/tcp_syslog.rb', line 114

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

#flushObject

Flush buffered logs to Syslog



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

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

#socketObject



128
129
130
# File 'lib/tcp_syslog.rb', line 128

def socket
  @socket[Thread.current] ||= new_socket
end

#ssl?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/tcp_syslog.rb', line 132

def ssl?
  !@ssl.nil?
end