Class: LogjamAgent::SyslogLikeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/logjam_agent/syslog_like_formatter.rb

Constant Summary collapse

SEV_LABEL =
Logger::SEV_LABEL.map{|sev| "%-5s" % sev}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyslogLikeFormatter

Returns a new instance of SyslogLikeFormatter.



5
6
7
8
9
10
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 5

def initialize
  @hostname = LogjamAgent.hostname
  @app_name = "rails"
  @attributes = []
  @newline = ActiveSupport::VERSION::STRING < "4.0" ? "" : "\n"
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 12

def attributes
  @attributes
end

Instance Method Details

#call(severity, timestamp, progname, msg) ⇒ Object



32
33
34
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 32

def call(severity, timestamp, progname, msg)
  "#{format_severity(severity)} #{format_time(timestamp)}#{render_attributes}#{format_host_info(progname)}: #{format_message(msg)}#{@newline}"
end

#format_host_info(progname) ⇒ Object



37
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 37

def format_host_info(progname); ""; end

#format_message(msg) ⇒ Object



28
29
30
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 28

def format_message(msg)
  msg.strip
end

#format_severity(severity) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 16

def format_severity(severity)
  if severity.is_a?(String)
    "%-5s" % severity
  else
    SEV_LABEL[severity] || 'ALIEN'
  end
end

#format_time(timestamp) ⇒ Object



24
25
26
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 24

def format_time(timestamp)
  timestamp.strftime("%b %d %H:%M:%S.#{"%06d" % timestamp.usec}")
end

#render_attributesObject



44
45
46
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 44

def render_attributes
  @attributes.map{|key, value| " #{key}[#{value}]"}.join
end

#reset_attributesObject



56
57
58
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 56

def reset_attributes
  @attributes = []
end

#set_attribute(name, value) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/logjam_agent/syslog_like_formatter.rb', line 48

def set_attribute(name, value)
  if attribute = @attributes.detect{|n,v| n == name}
    attribute[1] = value
  else
    @attributes << [name, value]
  end
end