Class: Timber::LogEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/timber/log_entry.rb

Overview

Represents a new log entry into the log. This is an intermediary class between ‘Logger` and the log device that you set it up with.

Constant Summary collapse

DT_PRECISION =

:nodoc:

6.freeze
SCHEMA =
"https://raw.githubusercontent.com/timberio/log-event-json-schema/v2.0.4/schema.json".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, time, progname, message, context_snapshot, event, options = {}) ⇒ LogEntry

Creates a log entry suitable to be sent to the Timber API.

Parameters:

  • severity (Integer)

    the log level / severity

  • time (Time)

    the exact time the log message was written

  • progname (String)

    the progname scope for the log message

  • message (String)

    Human readable log message.

  • context_snapshot (Hash)

    structured data representing a snapshot of the context at the given point in time.

  • event (Timber.Event)

    structured data representing the log line event. This should be an instance of Event.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/timber/log_entry.rb', line 22

def initialize(level, time, progname, message, context_snapshot, event, options = {})
  @level = level
  @time = time.utc
  @progname = progname

  # If the message is not a string we call inspect to ensure it is a string.
  # This follows the default behavior set by ::Logger
  # See: https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L615
  @message = message.is_a?(String) ? message : message.inspect
  @tags = options[:tags]
  @time_ms = options[:time_ms]

  context_snapshot = {} if context_snapshot.nil?
  hostname = Socket.gethostname
  pid = Process.pid
  system_context = Contexts::System.new(hostname: hostname, pid: pid)
  context_snapshot[system_context.keyspace] = system_context.as_json

  @context_snapshot = context_snapshot
  @event = event
end

Instance Attribute Details

#context_snapshotObject (readonly)

Returns the value of attribute context_snapshot.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def context_snapshot
  @context_snapshot
end

#eventObject (readonly)

Returns the value of attribute event.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def event
  @event
end

#levelObject (readonly)

Returns the value of attribute level.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def message
  @message
end

#prognameObject (readonly)

Returns the value of attribute progname.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def progname
  @progname
end

#tagsObject (readonly)

Returns the value of attribute tags.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def tags
  @tags
end

#timeObject (readonly)

Returns the value of attribute time.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def time
  @time
end

#time_msObject (readonly)

Returns the value of attribute time_ms.



10
11
12
# File 'lib/timber/log_entry.rb', line 10

def time_ms
  @time_ms
end

Instance Method Details

#as_json(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/timber/log_entry.rb', line 44

def as_json(options = {})
  options ||= {}
  hash = {:level => level, :dt => formatted_dt, :message => message, :tags => tags,
    :time_ms => time_ms}

  if !event.nil?
    hash[:event] = event.as_json
  end

  if !context_snapshot.nil? && context_snapshot.length > 0
    hash[:context] = context_snapshot
  end

  hash[:"$schema"] = SCHEMA

  hash = if options[:only]
    hash.select do |key, _value|
      options[:only].include?(key)
    end
  elsif options[:except]
    hash.select do |key, _value|
      !options[:except].include?(key)
    end
  else
    hash
  end

  Util::Hash.deep_compact(hash)
end

#inspectObject



74
75
76
# File 'lib/timber/log_entry.rb', line 74

def inspect
  to_s
end

#to_json(options = {}) ⇒ Object



78
79
80
# File 'lib/timber/log_entry.rb', line 78

def to_json(options = {})
  as_json(options).to_json
end

#to_msgpack(*args) ⇒ Object



82
83
84
# File 'lib/timber/log_entry.rb', line 82

def to_msgpack(*args)
  as_json.to_msgpack(*args)
end

#to_sObject

This is used when LogEntry objects make it to a non-Timber logger.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/timber/log_entry.rb', line 87

def to_s
  log_message = message

  if !event.nil?
    event_hash = event.as_json
    event_type = event_hash.keys.first

    event_type = if event.is_a?(Events::Custom)
      "#{event_type}.#{event.type}"
    else
      "#{event_type}"
    end

    log_message = "#{message} [#{event_type}]"
  end

  log_message + "\n"
end