Class: Termite::SyslogLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/termite/syslog_logger.rb

Instance Method Summary collapse

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, syslog_message, application)
  Syslog.open(application, Syslog::LOG_PID | Syslog::LOG_CONS) do |s|
    s.send(Logger::LEVEL_SYSLOG_MAP[severity], syslog_message)
  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 send_message(severity, full_message, 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]

  full_message.split("\n").each do |line|
    syslog_message = "#{line} #{data}"
    case @transport
    when "UDP"
      send_udp(severity, syslog_string, syslog_message, application)
    when "syscall", "libc"
      send_libc(severity, syslog_message, application) rescue nil
    else
      send_libc(severity, syslog_message, application) rescue
        send_udp(severity, syslog_string, syslog_message, 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, syslog_message, application)
    @socket.send(syslog_string + syslog_message, 0, @server_addr, @server_port) rescue nil
end