Class: Logglier::Client::Syslog
- Inherits:
-
Object
- Object
- Logglier::Client::Syslog
- Includes:
- InstanceMethods
- Defined in:
- lib/logglier/client/syslog.rb
Instance Attribute Summary collapse
-
#facility ⇒ Object
readonly
Returns the value of attribute facility.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#input_uri ⇒ Object
readonly
Returns the value of attribute input_uri.
-
#syslog ⇒ Object
readonly
Returns the value of attribute syslog.
Instance Method Summary collapse
-
#close ⇒ Object
Required by Logger::LogDevice.
-
#datetime_format ⇒ Object
Specifies the date/time format for this client.
-
#formatter ⇒ Object
Generate a syslog compat message See RFC3164 4.1.1 - 4.1.3.
-
#initialize(opts = {}) ⇒ Syslog
constructor
A new instance of Syslog.
-
#pri(severity) ⇒ Object
Syslog specific PRI calculation.
-
#write(message) ⇒ Object
Required by Logger::LogDevice.
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
#facility ⇒ Object (readonly)
Returns the value of attribute facility.
13 14 15 |
# File 'lib/logglier/client/syslog.rb', line 13 def facility @facility end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
13 14 15 |
# File 'lib/logglier/client/syslog.rb', line 13 def format @format end |
#input_uri ⇒ Object (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 |
#syslog ⇒ Object (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
#close ⇒ Object
Required by Logger::LogDevice
53 54 55 |
# File 'lib/logglier/client/syslog.rb', line 53 def close @syslog.close end |
#datetime_format ⇒ Object
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 |
#formatter ⇒ Object
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 = "<#{pri(severity)}>#{datetime.strftime(datetime_format)} #{@hostname} " # Include process ID in progname/log tag - RFC3164 § 5.3 if progname << "#{progname}[#{processid}]: " else << "#{$0}[#{processid}]: " end # Support logging JSON to Syslog if @format == :json && msg.is_a?(Hash) << MultiJson.dump(msg) else << (msg,severity,processid) end if @input_uri.scheme == 'tcp' << "\r\n" end 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() begin @syslog.send(,0) rescue Timeout::Error => e $stderr.puts "WARNING: Timeout::Error posting message: #{}" end end |