Class: Lumberjack::JsonDevice

Inherits:
Device
  • Object
show all
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

Instance Method Summary collapse

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

#formatterObject

Returns the value of attribute formatter.



39
40
41
# File 'lib/lumberjack_json_device.rb', line 39

def formatter
  @formatter
end

#mappingObject

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_formatObject



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.message) unless @message_key.nil?

  tags = entry.tags
  if @custom_keys.size > 0
    tags = (tags.nil? ? {} : tags.dup)
    @custom_keys.each do |name, key|
      set_attribute(data, key, tags.delete(name.to_s))
    end
  end

  unless @tags_key.nil?
    tags ||= {}
    set_attribute(data, @tags_key, tags)
  end

  data = @formatter.format(data) if @formatter
  data
end

#flushObject



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.message.nil? || entry.message == ""
  data = entry_as_json(entry)
  json = MultiJson.dump(data)
  @device.write(json)
end