Sha256: 2f46a41a737900add315ad629e33ee1113205834621fb550f3f659a9800d79a0

Contents?: true

Size: 1.67 KB

Versions: 13

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literals: true

module Lumberjack
  # 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.
  class LogEntry
    attr_accessor :time, :message, :severity, :progname, :pid, :tags

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

    UNIT_OF_WORK_ID = "unit_of_work_id"

    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.nil? || tags.is_a?(Hash)
        tags
      else
        {UNIT_OF_WORK_ID => tags}
      end
    end

    def severity_label
      Severity.level_to_label(severity)
    end

    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

    def inspect
      to_s
    end

    # Deprecated - backward compatibility with 1.0 API
    def unit_of_work_id
      tags[UNIT_OF_WORK_ID] if tags
    end

    # Deprecated - backward compatibility with 1.0 API
    def unit_of_work_id=(value)
      if tags
        tags[UNIT_OF_WORK_ID] = value
      else
        @tags = {UNIT_OF_WORK_ID => value}
      end
    end

    # Return the tag with the specified name.
    def tag(name)
      tags[name.to_s] if tags
    end

    private

    def tags_to_s
      tags_string = ""
      tags&.each { |name, value| tags_string << " #{name}:#{value.inspect}" }
      tags_string
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.35 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.34 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.33 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.32 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.31 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.30 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.29 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.28 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/lumberjack-1.2.8/lib/lumberjack/log_entry.rb
lumberjack-1.2.8 lib/lumberjack/log_entry.rb