Class: LogjamAgent::SyslogLikeFormatter
- Inherits:
-
Object
- Object
- LogjamAgent::SyslogLikeFormatter
- Defined in:
- lib/logjam_agent/syslog_like_formatter.rb
Constant Summary collapse
- SEV_LABEL =
Logger::SEV_LABEL.map{|sev| "%-5s" % sev}
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #call(severity, timestamp, progname, msg) ⇒ Object
- #format_host_info(progname) ⇒ Object
- #format_message(msg) ⇒ Object
- #format_severity(severity) ⇒ Object
- #format_time(timestamp) ⇒ Object
-
#initialize ⇒ SyslogLikeFormatter
constructor
A new instance of SyslogLikeFormatter.
- #render_attributes ⇒ Object
- #reset_attributes ⇒ Object
- #set_attribute(name, value) ⇒ Object
Constructor Details
#initialize ⇒ SyslogLikeFormatter
Returns a new instance of SyslogLikeFormatter.
5 6 7 8 9 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 5 def initialize @hostname = LogjamAgent.hostname @app_name = "rails" @newline = ActiveSupport::VERSION::STRING < "4.0" ? "" : "\n" end |
Instance Method Details
#attributes ⇒ Object
15 16 17 18 19 20 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 15 def attributes unless attributes = Thread.current.thread_variable_get(:__logjam_formatter_attributes__) attributes = Thread.current.thread_variable_set(:__logjam_formatter_attributes__, []) end attributes end |
#attributes=(attributes) ⇒ Object
11 12 13 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 11 def attributes=(attributes) Thread.current.thread_variable_set(:__logjam_formatter_attributes__, attributes) end |
#call(severity, timestamp, progname, msg) ⇒ Object
40 41 42 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 40 def call(severity, , progname, msg) "#{format_severity(severity)} #{format_time()}#{render_attributes}#{format_host_info(progname)}: #{(msg)}#{@newline}" end |
#format_host_info(progname) ⇒ Object
45 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 45 def format_host_info(progname); ""; end |
#format_message(msg) ⇒ Object
36 37 38 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 36 def (msg) msg.strip end |
#format_severity(severity) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 24 def format_severity(severity) if severity.is_a?(String) "%-5s" % severity else SEV_LABEL[severity] || 'ALIEN' end end |
#format_time(timestamp) ⇒ Object
32 33 34 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 32 def format_time() .strftime("%b %d %H:%M:%S.#{"%06d" % .usec}") end |
#render_attributes ⇒ Object
52 53 54 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 52 def render_attributes attributes.map{|key, value| " #{key}[#{value}]"}.join end |
#reset_attributes ⇒ Object
64 65 66 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 64 def reset_attributes self.attributes = [] end |
#set_attribute(name, value) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 56 def set_attribute(name, value) if attribute = attributes.detect{|n,v| n == name} attribute[1] = value else attributes << [name, value] end end |