Class: Lumberjack::JsonDevice
- Inherits:
-
Device
- Object
- Device
- Lumberjack::JsonDevice
- Defined in:
- lib/lumberjack_json_device.rb
Overview
This Lumberjack device logs output to another device as JSON formatted text with one document per line.
The mapping parameter can be used to define the JSON data structure. To define the structure pass in a hash with key indicating the log entry field and the value indicating the JSON document key.
The standard entry fields are mapped with the following keys:
-
:time
-
:severity
-
:progname
-
:pid
-
:message
-
:tags
Any additional keys will be pulled from the tags. If any of the standard keys are missing or have a nil mapping, the entry field will not be included in the JSON output.
You can create a nested JSON structure by specifying an array as the JSON key.
Constant Summary collapse
- DEFAULT_MAPPING =
{ time: true, severity: true, progname: true, pid: true, message: true, tags: true }.freeze
- DEFAULT_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S.%6N%z"
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#mapping ⇒ Object
Returns the value of attribute mapping.
Instance Method Summary collapse
- #datetime_format ⇒ Object
-
#datetime_format=(format) ⇒ Object
Set the datetime format for the log timestamp.
-
#entry_as_json(entry) ⇒ Object
Convert a Lumberjack::LogEntry to a Hash using the specified field mapping.
- #flush ⇒ Object
-
#initialize(stream_or_device, mapping: DEFAULT_MAPPING, formatter: nil, datetime_format: nil) ⇒ JsonDevice
constructor
A new instance of JsonDevice.
- #map(field_mapping) ⇒ Object
- #write(entry) ⇒ Object
Constructor Details
#initialize(stream_or_device, mapping: DEFAULT_MAPPING, formatter: nil, datetime_format: nil) ⇒ JsonDevice
Returns a new instance of JsonDevice.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lumberjack_json_device.rb', line 42 def initialize(stream_or_device, mapping: DEFAULT_MAPPING, formatter: nil, datetime_format: nil) @mutex = Mutex.new if stream_or_device.is_a?(Device) @device = stream_or_device else @device = Writer.new(stream_or_device) end self.mapping = mapping if formatter @formatter = formatter else @formatter = default_formatter datetime_format = DEFAULT_TIME_FORMAT if datetime_format.nil? end add_datetime_formatter!(datetime_format) unless datetime_format.nil? end |
Instance Attribute Details
#formatter ⇒ Object
Returns the value of attribute formatter.
39 40 41 |
# File 'lib/lumberjack_json_device.rb', line 39 def formatter @formatter end |
#mapping ⇒ Object
Returns the value of attribute mapping.
40 41 42 |
# File 'lib/lumberjack_json_device.rb', line 40 def mapping @mapping end |
Instance Method Details
#datetime_format ⇒ Object
73 74 75 |
# File 'lib/lumberjack_json_device.rb', line 73 def datetime_format @datetime_format end |
#datetime_format=(format) ⇒ Object
Set the datetime format for the log timestamp.
78 79 80 |
# File 'lib/lumberjack_json_device.rb', line 78 def datetime_format=(format) add_datetime_formatter!(format) end |
#entry_as_json(entry) ⇒ Object
Convert a Lumberjack::LogEntry to a Hash using the specified field mapping.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/lumberjack_json_device.rb', line 115 def entry_as_json(entry) data = {} set_attribute(data, @time_key, entry.time) unless @time_key.nil? set_attribute(data, @severity_key, entry.severity_label) unless @severity_key.nil? set_attribute(data, @progname_key, entry.progname) unless @progname_key.nil? set_attribute(data, @pid_key, entry.pid) unless @pid_key.nil? set_attribute(data, @message_key, entry.) unless @message_key.nil? = entry. if @custom_keys.size > 0 = (.nil? ? {} : .dup) @custom_keys.each do |name, key| set_attribute(data, key, .delete(name.to_s)) end end unless @tags_key.nil? ||= {} set_attribute(data, @tags_key, ) end data = @formatter.format(data) if @formatter data end |
#flush ⇒ Object
69 70 71 |
# File 'lib/lumberjack_json_device.rb', line 69 def flush @device.flush end |
#map(field_mapping) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/lumberjack_json_device.rb', line 106 def map(field_mapping) new_mapping = {} field_mapping.each do |key, value| new_mapping[key.to_sym] = value end self.mapping = mapping.merge(new_mapping) end |
#write(entry) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/lumberjack_json_device.rb', line 62 def write(entry) return if entry..nil? || entry. == "" data = entry_as_json(entry) json = MultiJson.dump(data) @device.write(json) end |