Class: Lorekeeper::JSONLogger

Inherits:
FastLogger show all
Defined in:
lib/lorekeeper/json_logger.rb

Overview

The JSONLogger provides a logger which will output messages in JSON format

Constant Summary

Constants inherited from FastLogger

FastLogger::LOGGING_METHODS, FastLogger::METHOD_SEVERITY_MAP

Instance Attribute Summary

Attributes inherited from FastLogger

#formatter, #level

Instance Method Summary collapse

Methods inherited from FastLogger

#add, #debug?, #error?, #fatal?, #info?, #silence_logger, #warn?

Constructor Details

#initialize(file) ⇒ JSONLogger

Returns a new instance of JSONLogger.



10
11
12
13
14
# File 'lib/lorekeeper/json_logger.rb', line 10

def initialize(file)
  reset_state
  @base_fields = { MESSAGE => '', TIMESTAMP => '' }
  super(file)
end

Instance Method Details

#add_fields(fields) ⇒ Object



53
54
55
56
# File 'lib/lorekeeper/json_logger.rb', line 53

def add_fields(fields)
  remove_invalid_fields(fields)
  state.fetch(:base_fields).merge!(fields)
end

#add_thread_unsafe_fields(fields) ⇒ Object



40
41
42
43
44
# File 'lib/lorekeeper/json_logger.rb', line 40

def add_thread_unsafe_fields(fields)
  remove_invalid_fields(fields)
  @base_fields.merge!(fields)
  reset_state # Forcing to recreate the thread safe information
end

#current_fields ⇒ Object



16
17
18
# File 'lib/lorekeeper/json_logger.rb', line 16

def current_fields
  state[:base_fields]
end

#exception(exception, custom_message = nil, custom_data = nil) ⇒ Object

We will output backtrace twice. Once inside the stack so it can be parsed by software And the other inside the message so it is readable to humans

Parameters:

  • instance of a class inheriting from Exception



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lorekeeper/json_logger.rb', line 67

def exception(exception, custom_message = nil, custom_data = nil)
  if exception.is_a?(Exception)
    backtrace = exception.backtrace || []
    exception_fields = {
      'exception' => "#{exception.class}: #{exception.message}",
      'stack' => backtrace
    }
    exception_fields['data'] = custom_data if custom_data

    message = custom_message || exception.message
    with_extra_fields(exception_fields) { log_data(:error, message) }
  else
    log_data(:warning, 'Logger exception called without exception class.')
    error_with_data("#{exception.class}: #{exception.inspect} #{custom_message}", custom_data)
  end
end

#inspect ⇒ Object



84
85
86
# File 'lib/lorekeeper/json_logger.rb', line 84

def inspect
  "Lorekeeper JSON logger. IO: #{@file.inspect}"
end

#remove_fields(fields) ⇒ Object



58
59
60
61
62
# File 'lib/lorekeeper/json_logger.rb', line 58

def remove_fields(fields)
  [*fields].each do |field|
    state.fetch(:base_fields).delete(field)
  end
end

#remove_thread_unsafe_fields(fields) ⇒ Object



46
47
48
49
50
51
# File 'lib/lorekeeper/json_logger.rb', line 46

def remove_thread_unsafe_fields(fields)
  [*fields].each do |field|
    @base_fields.delete(field)
  end
  reset_state
end

#reset_state ⇒ Object



24
25
26
# File 'lib/lorekeeper/json_logger.rb', line 24

def reset_state
  Thread.current[THREAD_KEY] = nil
end

#state ⇒ Object



20
21
22
# File 'lib/lorekeeper/json_logger.rb', line 20

def state
  Thread.current[THREAD_KEY] ||= { base_fields: @base_fields.dup, extra_fields: {} }
end