Sha256: 5d6801285acccea11177dc2072e933101f67c3fd8e44e20cea8e2a04323d31f2

Contents?: true

Size: 994 Bytes

Versions: 6

Compression:

Stored size: 994 Bytes

Contents

require 'socket'
require 'logger'

module LogjamAgent
  class SyslogLikeFormatter
    def initialize
      @hostname = Socket.gethostname.split('.').first
      @app_name = "rails"
    end

    attr_accessor :extra_attributes

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

    def format_severity(severity)
      SEV_LABEL[severity] || 'ALIEN'
    end

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

    def format_msg(msg)
      "#{msg}".sub(/^[\s\n]+/, '').sub(/[\s\n]+$/,"\n")
    end

    def call(severity, timestamp, progname, msg)
      "#{format_severity(severity)} #{format_time(timestamp)} #{@hostname} #{progname||@app_name}[#{$$}]#{render_extra_attributes}: #{format_msg(msg)}"
    end

    def render_extra_attributes
      (self.extra_attributes||[]).map{|key, value| " #{key}[#{value}]"}
    end

    def add_extra_attributes(attributes)
      (self.extra_attributes ||= []).concat(attributes)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
logjam_agent-0.1.1 lib/logjam_agent/syslog_like_formatter.rb
logjam_agent-0.1.0 lib/logjam_agent/syslog_like_formatter.rb
logjam_agent-0.0.4 lib/logjam_agent/syslog_like_formatter.rb
logjam_agent-0.0.3 lib/logjam_agent/syslog_like_formatter.rb
logjam_agent-0.0.2 lib/logjam_agent/syslog_like_formatter.rb
logjam_agent-0.0.1 lib/logjam_agent/syslog_like_formatter.rb