Class: MqLogger::RabbitLogger
- Inherits:
-
Logger
- Object
- Logger
- MqLogger::RabbitLogger
- Defined in:
- lib/mq_logger.rb
Defined Under Namespace
Modules: Level
Constant Summary collapse
- LOGSTASH_EVENT_FIELDS =
%w(@timestamp @tags @type @source @source_host @fields @message).freeze
- HOST =
Socket.gethostname
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #format_message(severity, time, progname, message) ⇒ Object
-
#initialize(queue, route, source = nil) ⇒ RabbitLogger
constructor
A new instance of RabbitLogger.
Constructor Details
#initialize(queue, route, source = nil) ⇒ RabbitLogger
Returns a new instance of RabbitLogger.
51 52 53 54 55 |
# File 'lib/mq_logger.rb', line 51 def initialize(queue, route, source=nil) @client = ::MqLogger::MQClient.new(queue, route) @source = source super end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
46 47 48 |
# File 'lib/mq_logger.rb', line 46 def client @client end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mq_logger.rb', line 57 def add(severity, = nil, progname = nil, &block) severity ||= UNKNOWN if severity < @level return true end progname ||= @progname if .nil? if block_given? = yield else = progname progname = @progname end end @client.write( (format_severity(severity), Time.now, progname, )) true end |
#format_message(severity, time, progname, message) ⇒ Object
76 77 78 79 80 81 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 107 108 109 110 111 112 113 114 115 |
# File 'lib/mq_logger.rb', line 76 def (severity, time, progname, ) data = if data.is_a?(String) && data[0] == '{' data = (JSON.parse() rescue nil) || end event = case data when LogStash::Event data.clone when Hash event_data = { "@tags" => [], "@fields" => {}, "@timestamp" => time } LOGSTASH_EVENT_FIELDS.each do |field_name| if field_data = data.delete(field_name) event_data[field_name] = field_data end end event_data["@fields"].merge!(data) event_data["@timestamp"] = time LogStash::Event.new(event_data) when String LogStash::Event.new("@message" => data, "@timestamp" => time) end event['severity'] ||= severity #event.type = progname if event.source == 'unknown' event.source = @source elsif !event.source event.source = HOST end if !event.source_host event.source_host = HOST end event end |