Class: Sysloggly::Formatters::SimpleFormatter
- Inherits:
-
Object
- Object
- Sysloggly::Formatters::SimpleFormatter
- Defined in:
- lib/sysloggly/formatters/simple_formatter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#input_uri ⇒ Object
readonly
Returns the value of attribute input_uri.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #call(severity, datetime, progname, payload) ⇒ Object
-
#datetime_format ⇒ Object
Specifies the date/time format for this client.
- #hashify_message(msg) ⇒ Object private
-
#initialize(input_uri, opts) ⇒ SimpleFormatter
constructor
A new instance of SimpleFormatter.
Constructor Details
#initialize(input_uri, opts) ⇒ SimpleFormatter
Returns a new instance of SimpleFormatter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 9 def initialize(input_uri, opts) @input_uri = input_uri @opts = opts @hostname = opts[:hostname] || Socket.gethostname.split(".").first @progname = opts[:progname] = 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 |
Instance Attribute Details
#input_uri ⇒ Object (readonly)
Returns the value of attribute input_uri.
7 8 9 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 7 def input_uri @input_uri end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 7 def opts @opts end |
Instance Method Details
#call(severity, datetime, progname, payload) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 37 def call(severity, datetime, progname, payload) = "#{severity} [#{datetime.strftime(datetime_format)}] #{@hostname} " << MultiJson.dump((payload).merge()) << "\r\n" if ["file", "tcp"].include?(@input_uri.scheme) end |
#datetime_format ⇒ Object
Specifies the date/time format for this client
32 33 34 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 32 def datetime_format "%b %e %H:%M:%S" end |
#hashify_message(msg) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sysloggly/formatters/simple_formatter.rb', line 47 def (msg) if msg.is_a?(Hash) msg elsif msg.is_a?(Exception) { exception_class: msg.class.name, message: msg. } elsif msg.is_a?(String) begin JSON.parse(msg) rescue { message: msg } end else { message: msg.inspect } end end |