Class: BarkestCore::LogEntry

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations, Comparable
Defined in:
app/models/barkest_core/log_entry.rb

Overview

Reads a log line from a JSON log file.

Constant Summary collapse

SEVERITY_LIST =
%w(DEBUG INFO WARN ERROR FATAL)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LogEntry

Creates a LogEntry.

The args can either be a JSON string or a Hash.



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

def initialize(*args)
  args.each do |arg|
    if arg.is_a?(String)
      arg = JSON.parse(arg).symbolize_keys rescue nil
    end
    if arg.is_a?(Hash)
      arg.each do |k,v|
        k = k.to_sym
        v = case k
              when :level
                v.to_sym
              when :time
                Time.parse(v)
              when :process_id
                v.to_i
              else
                v
            end
        instance_variable_set(:"@#{k}", v)
      end
    end
  end
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def app_name
  @app_name
end

#app_versionObject (readonly)

Returns the value of attribute app_version.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def app_version
  @app_version
end

#levelObject (readonly)

Returns the value of attribute level.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def message
  @message
end

#process_idObject (readonly)

Returns the value of attribute process_id.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def process_id
  @process_id
end

#timeObject (readonly)

Returns the value of attribute time.



13
14
15
# File 'app/models/barkest_core/log_entry.rb', line 13

def time
  @time
end

Class Method Details

.read_log(log_file = nil) ⇒ Object

Reads a log file consisting of JSON records.

If no log file is specified, the default log file is assumed.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/barkest_core/log_entry.rb', line 85

def self.read_log(log_file = nil)
  log_file ||= Rails.root.join('log', "#{Rails.env}.log")

  ret = []

  if File.exist?(log_file)
    File.foreach(log_file, "\n").with_index do |line, index|
      line = JSON.parse(line) rescue nil
      ret << LogEntry.new(line.symbolize_keys.merge(index: index)) if line.is_a?(Hash)
    end
  end

  ret
end

Instance Method Details

#<=>(other) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
# File 'app/models/barkest_core/log_entry.rb', line 72

def <=>(other)
  return 1 unless other.is_a?(LogEntry)
  if index == other.index
    time <=> other.time
  else
    index <=> other.index
  end
end

#indexObject

Gets the index in the log file.



51
52
53
# File 'app/models/barkest_core/log_entry.rb', line 51

def index
  @index ||= 0
end

#inspectObject

:nodoc:



62
63
64
# File 'app/models/barkest_core/log_entry.rb', line 62

def inspect
  "#<#{self.class.name} #{level} #{time} [#{app_name} #{app_version} (#{process_id})] #{message.length > 32 ? (message[0...32] + '...') : message}>"
end

#level_idObject

Gets the level as a numeric ID.



57
58
59
# File 'app/models/barkest_core/log_entry.rb', line 57

def level_id
  @level_id ||= SEVERITY_LIST.index(level.to_s.upcase) || 5
end

#to_sObject

:nodoc:



67
68
69
# File 'app/models/barkest_core/log_entry.rb', line 67

def to_s
  "#{level} #{time} [#{app_name} #{app_version} (#{process_id})] #{message}"
end