Module: Sysloggly::Client::InstanceMethods

Included in:
Filelog, Syslog
Defined in:
lib/sysloggly/client.rb

Instance Method Summary collapse

Instance Method Details

#datetime_formatObject

Specifies the date/time format for this client



37
38
39
# File 'lib/sysloggly/client.rb', line 37

def datetime_format
  "%b %e %H:%M:%S"
end

#formatterObject

Generate a syslog compat message See RFC3164 4.1.1 - 4.1.3



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sysloggly/client.rb', line 78

def formatter
  proc do |severity, datetime, progname, msg|
    processid = Process.pid
    message = "<#{pri(severity)}>#{datetime.strftime(datetime_format)} #{@hostname} "

    # Include process ID in progname/log tag - RFC3164 § 5.3
    message << "#{@progname || progname || $0}[#{processid}]: "

    # Only log JSON to Syslog
    message << MultiJson.dump(hashify_message(msg).merge(@custom_options))

    if ['file', 'tcp'].include?(@input_uri.scheme )
      message << "\r\n"
    end
    message
  end
end

#hashify_message(msg) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sysloggly/client.rb', line 96

def hashify_message(msg)
  if msg.is_a?(Hash)
    msg
  elsif msg.is_a?(Exception)
    { exception_class: msg.class.name, message: msg.message }
  elsif msg.is_a?(String)
    begin
      JSON.parse(msg)
    rescue
      { message: msg }
    end
  else
    { message: msg.inspect }
  end
end

#pri(severity) ⇒ Object

Syslog specific PRI calculation. See RFC3164 4.1.1



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sysloggly/client.rb', line 60

def pri(severity)
  severity_value = case severity
                   when "FATAL"
                     0
                   when "ERROR"
                     3
                   when "WARN"
                     4
                   when "INFO"
                     6
                   when "DEBUG"
                     7
                   end
  (@facility << 3) + severity_value
end

#setup_optionsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sysloggly/client.rb', line 41

def setup_options
  @hostname = opts[:hostname] || Socket.gethostname.split('.').first
  @progname = opts[:progname]
  @custom_options = opts.except(:hostname, :progname)

  if ['udp', 'tcp'].include?(@input_uri.scheme) && !@input_uri.path.empty?
    if @facility = @input_uri.path.split('/')[1]
      @facility = @facility.to_i
      unless @facility <= 23 && @facility >= 0
        raise Sysloggly::UnknownFacility.new(@facility.to_s)
      end
    end
  else
    @facility = 23
  end
end