Class: Logglier::Client::Syslog

Inherits:
Object
  • Object
show all
Includes:
InstanceMethods
Defined in:
lib/logglier/client/syslog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InstanceMethods

#masher, #masherize_key, #massage_message, #setup_input_uri

Constructor Details

#initialize(opts = {}) ⇒ Syslog



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
41
# File 'lib/logglier/client/syslog.rb', line 15

def initialize(opts={})
  setup_input_uri(opts)

  case @input_uri.scheme
  when 'udp'
    @syslog = UDPSocket.new()
    @syslog.connect(@input_uri.host, @input_uri.port)
  when 'tcp'
    @syslog = TCPSocket.new(@input_uri.host, @input_uri.port)
    @syslog.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1 )
    @syslog.setsockopt( Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
  end

  unless @input_uri.path.empty?
    if @facility = @input_uri.path.split('/')[1]
      @facility = @facility.to_i
      unless @facility <= 23 && @facility >= 0
        raise Logglier::UnknownFacility.new(@facility.to_s)
      end
    end
  else
    @facility = 16
  end

  @format = opts[:format]
  @hostname = opts[:hostname] || Socket.gethostname.split('.').first
end

Instance Attribute Details

#facilityObject (readonly)

Returns the value of attribute facility.



13
14
15
# File 'lib/logglier/client/syslog.rb', line 13

def facility
  @facility
end

#formatObject (readonly)

Returns the value of attribute format.



13
14
15
# File 'lib/logglier/client/syslog.rb', line 13

def format
  @format
end

#input_uriObject (readonly)

Returns the value of attribute input_uri.



13
14
15
# File 'lib/logglier/client/syslog.rb', line 13

def input_uri
  @input_uri
end

#syslogObject (readonly)

Returns the value of attribute syslog.



13
14
15
# File 'lib/logglier/client/syslog.rb', line 13

def syslog
  @syslog
end

Instance Method Details

#closeObject

Required by Logger::LogDevice



53
54
55
# File 'lib/logglier/client/syslog.rb', line 53

def close
  @syslog.close
end

#datetime_formatObject

Specifies the date/time format for this client



58
59
60
# File 'lib/logglier/client/syslog.rb', line 58

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

#formatterObject

Generate a syslog compat message See RFC3164 4.1.1 - 4.1.3



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/logglier/client/syslog.rb', line 82

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
    if progname
      message << "#{progname}[#{processid}]: "
    else
      message << "#{$0}[#{processid}]: "
    end

    # Support logging JSON to Syslog
    if @format == :json && msg.is_a?(Hash)
      message << MultiJson.dump(msg)
    else
      message << massage_message(msg,severity,processid)
    end

    if @input_uri.scheme == 'tcp'
      message << "\r\n"
    end
    message
  end
end

#pri(severity) ⇒ Object

Syslog specific PRI calculation. See RFC3164 4.1.1



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logglier/client/syslog.rb', line 64

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

#write(message) ⇒ Object

Required by Logger::LogDevice



44
45
46
47
48
49
50
# File 'lib/logglier/client/syslog.rb', line 44

def write(message)
  begin
    @syslog.send(message,0)
  rescue Timeout::Error => e
    $stderr.puts "WARNING: Timeout::Error posting message: #{message}"
  end
end