File: syslog-formatter.rb

package info (click to toggle)
ruby-syslog-logger 1.6.8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 120 kB
  • ctags: 88
  • sloc: ruby: 479; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 789 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
36
37
38
39
40
# Adds some default information to syslog messages.
# Log format:
#   [Time.mSec] [SeverityLabel]: message

class Logger::SyslogFormatter < Logger::Formatter
  Format = "[%s] [%5s]: %s\n"

  attr_accessor :datetime_format

  def initialize
    @datetime_format = nil
  end

  def call(severity, time, progname, msg)
    Format % [format_datetime(time), severity, msg2str(msg)]
  end

  protected

  def format_datetime(time)
    if @datetime_format.nil?
      time.strftime("%H:%M:%S.") << "%06d " % time.usec
    else
      time.strftime(@datetime_format)
    end
  end

  def msg2str(msg)
    case msg
    when ::String
      msg
    when ::Exception
      "#{ msg.message } (#{ msg.class })\n" <<
        (msg.backtrace || []).join("\n")
    else
      msg.inspect
    end
  end
   
end