Class: Superlogger::Logger

Inherits:
ActiveSupport::Logger
  • Object
show all
Defined in:
lib/superlogger/logger.rb

Defined Under Namespace

Classes: SimpleFormatter

Instance Method Summary collapse

Instance Method Details

#format_message(severity, time, _progname, msg) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/superlogger/logger.rb', line 5

def format_message(severity, time, _progname, msg)
  return nil if msg.blank? # Silence nil and empty msg
  return nil if is_rails_rack_logger_msg?(msg) # Silence rack logger msg

  msg = {msg: msg} unless msg.is_a?(Hash)

  h = {}
  h[:level] = severity.downcase
  h[:ts] = time.to_f
  h[:caller] = get_caller_location
  h[:session_id] = Superlogger.session_id[0..11] unless Superlogger.session_id.nil?
  h[:request_id] = Superlogger.request_id unless Superlogger.request_id.nil?
  h.merge!(msg)

  h.to_json + "\n"
end

#get_caller_locationObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/superlogger/logger.rb', line 27

def get_caller_location
  index = caller_locations(4, 1).first.label.include?('broadcast') ? 6 : 5
  location = caller_locations(index, 1).first

  # Extract filename without file extension from location.path
  # eg. superlogger/lib/superlogger/logger.rb => superlogger/logger
  file = location.path.split('/').last(2).join('/').split('.').first

  "#{file}:#{location.lineno}"
end

#is_rails_rack_logger_msg?(msg) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/superlogger/logger.rb', line 22

def is_rails_rack_logger_msg?(msg)
  return false unless msg.is_a?(String)
  msg =~ /Started (GET|POST|PUT|PATCH|DELETE)/
end