Class: BarkestCore::LogEntry
- Inherits:
-
Object
- Object
- BarkestCore::LogEntry
- 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
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#app_version ⇒ Object
readonly
Returns the value of attribute app_version.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#process_id ⇒ Object
readonly
Returns the value of attribute process_id.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
-
.read_log(log_file = nil) ⇒ Object
Reads a log file consisting of JSON records.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
:nodoc:.
-
#index ⇒ Object
Gets the index in the log file.
-
#initialize(*args) ⇒ LogEntry
constructor
Creates a LogEntry.
-
#inspect ⇒ Object
:nodoc:.
-
#level_id ⇒ Object
Gets the level as a numeric ID.
-
#to_s ⇒ Object
:nodoc:.
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_name ⇒ Object (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_version ⇒ Object (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 |
#level ⇒ Object (readonly)
Returns the value of attribute level.
13 14 15 |
# File 'app/models/barkest_core/log_entry.rb', line 13 def level @level end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
13 14 15 |
# File 'app/models/barkest_core/log_entry.rb', line 13 def @message end |
#process_id ⇒ Object (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 |
#time ⇒ Object (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 |
#index ⇒ Object
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 |
#inspect ⇒ Object
: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})] #{.length > 32 ? ([0...32] + '...') : }>" end |
#level_id ⇒ Object
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_s ⇒ Object
: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})] #{}" end |