Class: Termite::SyslogLogger
- Inherits:
-
Object
- Object
- Termite::SyslogLogger
- Defined in:
- lib/termite/syslog_logger.rb
Instance Method Summary collapse
-
#initialize(socket, server_addr, server_port, transport) ⇒ SyslogLogger
constructor
A new instance of SyslogLogger.
- #send_libc(severity, syslog_message, application) ⇒ Object
- #send_message(severity, full_message, app_data, time = Time.now, data = '{}') ⇒ Object
- #send_udp(severity, syslog_string, syslog_message, application) ⇒ Object
Constructor Details
#initialize(socket, server_addr, server_port, transport) ⇒ SyslogLogger
Returns a new instance of SyslogLogger.
5 6 7 |
# File 'lib/termite/syslog_logger.rb', line 5 def initialize(socket, server_addr, server_port, transport) @socket, @server_addr, @server_port, @transport = socket, server_addr, server_port, transport end |
Instance Method Details
#send_libc(severity, syslog_message, application) ⇒ Object
41 42 43 44 45 |
# File 'lib/termite/syslog_logger.rb', line 41 def send_libc(severity, , application) Syslog.open(application, Syslog::LOG_PID | Syslog::LOG_CONS) do |s| s.send(Logger::LEVEL_SYSLOG_MAP[severity], ) end end |
#send_message(severity, full_message, app_data, time = Time.now, data = '{}') ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/termite/syslog_logger.rb', line 9 def (severity, , app_data, time=Time.now, data='{}') tid = Ecology.thread_id(::Thread.current) day = time.strftime("%b %d").sub(/0(\d)/, ' \\1') time_of_day = time.strftime("%T") hostname = Socket.gethostname application = app_data[:combined] || app_data[:app] # Convert Ruby log level to syslog severity tag = Syslog::LOG_LOCAL6 + Logger::SYSLOG_SEVERITY_MAP[Logger::LEVEL_SYSLOG_MAP[severity]] syslog_string = "<#{tag}>#{day} #{time_of_day} #{hostname} #{application} [#{Process.pid}]: [#{tid}] " # ruby_severity is the Ruby Logger severity as a symbol ruby_severity = Logger::LOGGER_LEVEL_MAP.invert[severity] .split("\n").each do |line| = "#{line} #{data}" case @transport when "UDP" send_udp(severity, syslog_string, , application) when "syscall", "libc" send_libc(severity, , application) rescue nil else send_libc(severity, , application) rescue send_udp(severity, syslog_string, , application) end end end |
#send_udp(severity, syslog_string, syslog_message, application) ⇒ Object
37 38 39 |
# File 'lib/termite/syslog_logger.rb', line 37 def send_udp(severity, syslog_string, , application) @socket.send(syslog_string + , 0, @server_addr, @server_port) rescue nil end |