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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, time, progname, message, context, event) ⇒ 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 (#to_json)

    structured data representing the log line event, this can be anything that responds to #to_json



16
17
18
19
20
21
22
23
# File 'lib/timber/log_entry.rb', line 16

def initialize(level, time, progname, message, context, event)
  @level = level
  @time = time.utc
  @progname = progname
  @message = message
  @context = context
  @event = event
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def context
  @context
end

#eventObject (readonly)

Returns the value of attribute event.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def event
  @event
end

#levelObject (readonly)

Returns the value of attribute level.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def message
  @message
end

#prognameObject (readonly)

Returns the value of attribute progname.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def progname
  @progname
end

#timeObject (readonly)

Returns the value of attribute time.



7
8
9
# File 'lib/timber/log_entry.rb', line 7

def time
  @time
end

Instance Method Details

#as_json(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/timber/log_entry.rb', line 25

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

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

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

  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
end

#to_json(options = {}) ⇒ Object



50
51
52
# File 'lib/timber/log_entry.rb', line 50

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

#to_msgpack(*args) ⇒ Object



54
55
56
# File 'lib/timber/log_entry.rb', line 54

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