Class: Lumberjack::LogEntry

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

Overview

An entry in a log is a data structure that captures the log message as well as information about the system that logged the message.

Constant Summary collapse

TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S"
UNIT_OF_WORK_ID =
Deprecated.

Will be removed in version 2.0.

"unit_of_work_id"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, severity, message, progname, pid, tags) ⇒ LogEntry

Create a new log entry.

Parameters:

  • time (Time)

    The time the log entry was created.

  • severity (Integer, String)

    The severity of the log entry.

  • message (String)

    The message to log.

  • progname (String)

    The name of the program that created the log entry.

  • pid (Integer)

    The process id of the program that created the log entry.

  • tags (Hash<String, Object>)

    A hash of tags to associate with the log entry.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lumberjack/log_entry.rb', line 22

def initialize(time, severity, message, progname, pid, tags)
  @time = time
  @severity = (severity.is_a?(Integer) ? severity : Severity.label_to_level(severity))
  @message = message
  @progname = progname
  @pid = pid
  # backward compatibility with 1.0 API where the last argument was the unit of work id
  @tags = if tags.is_a?(Hash)
    compact_tags(tags)
  elsif !tags.nil?
    {UNIT_OF_WORK_ID => tags}
  end
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#pidObject

Returns the value of attribute pid.



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

def pid
  @pid
end

#prognameObject

Returns the value of attribute progname.



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

def progname
  @progname
end

#severityObject

Returns the value of attribute severity.



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

def severity
  @severity
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

Instance Method Details

#empty?Boolean

Return true if the log entry has no message and no tags.

Returns:

  • (Boolean)

    True if the log entry is empty, false otherwise.



91
92
93
# File 'lib/lumberjack/log_entry.rb', line 91

def empty?
  (message.nil? || message == "") && (tags.nil? || tags.empty?)
end

#inspectObject



44
45
46
# File 'lib/lumberjack/log_entry.rb', line 44

def inspect
  to_s
end

#nested_tagsHash

Helper method to expand the tags into a nested structure. Tags with dots in the name will be expanded into nested hashes.

Examples:

entry = Lumberjack::LogEntry.new(Time.now, Logger::INFO, "test", "app", 1500, "a.b.c" => 1, "a.b.d" => 2)
entry.nested_tags # => {"a" => {"b" => {"c" => 1, "d" => 2}}}

Returns:

  • (Hash)

    The tags expanded into a nested structure.



84
85
86
# File 'lib/lumberjack/log_entry.rb', line 84

def nested_tags
  Utils.expand_tags(tags)
end

#severity_labelObject



36
37
38
# File 'lib/lumberjack/log_entry.rb', line 36

def severity_label
  Severity.level_to_label(severity)
end

#tag(name) ⇒ Object?

Return the tag with the specified name.

Parameters:

  • name (String, Symbol)

    The tag name.

Returns:

  • (Object, nil)

    The tag value or nil if the tag does not exist.



70
71
72
73
74
# File 'lib/lumberjack/log_entry.rb', line 70

def tag(name)
  return nil if tags.nil?

  TagContext.new(tags)[name]
end

#to_sObject



40
41
42
# File 'lib/lumberjack/log_entry.rb', line 40

def to_s
  "[#{time.strftime(TIME_FORMAT)}.#{(time.usec / 1000.0).round.to_s.rjust(3, "0")} #{severity_label} #{progname}(#{pid})#{tags_to_s}] #{message}"
end

#unit_of_work_idObject

Deprecated.
  • backward compatibility with 1.0 API. Will be removed in version 2.0.



49
50
51
52
53
# File 'lib/lumberjack/log_entry.rb', line 49

def unit_of_work_id
  Lumberjack::Utils.deprecated("Lumberjack::LogEntry#unit_of_work_id", "Lumberjack::LogEntry#unit_of_work_id will be removed in version 2.0") do
    tags[UNIT_OF_WORK_ID] if tags
  end
end

#unit_of_work_id=(value) ⇒ Object

Deprecated.
  • backward compatibility with 1.0 API. Will be removed in version 2.0.



56
57
58
59
60
61
62
63
64
# File 'lib/lumberjack/log_entry.rb', line 56

def unit_of_work_id=(value)
  Lumberjack::Utils.deprecated("Lumberjack::LogEntry#unit_of_work_id=", "Lumberjack::LogEntry#unit_of_work_id= will be removed in version 2.0") do
    if tags
      tags[UNIT_OF_WORK_ID] = value
    else
      @tags = {UNIT_OF_WORK_ID => value}
    end
  end
end