Class: Superlogger::Logger

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

Defined Under Namespace

Classes: SimpleFormatter

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

Returns a new instance of Logger.



5
6
7
8
# File 'lib/superlogger/logger.rb', line 5

def initialize(*args)
  super
  @formatter = SimpleFormatter.new
end

Instance Method Details

#format_args(args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/superlogger/logger.rb', line 39

def format_args(args)
  output = if args.is_a?(Hash)
             # Format args in key=value pair, separated by pipes
             args.map do |key, value|
               "#{key}=#{value}"
             end.join(' | ')
           else
             args.to_s
           end

  output.gsub("\n", '\\n') # Escape newlines
end

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



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/superlogger/logger.rb', line 10

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

  timestamp = time.strftime('%Y-%m-%d %H:%M:%S.%L')
  session_id = Superlogger.session_id[0..11]
  request_id = Superlogger.request_id[0..11]
  severity = severity.to_s.upcase[0]
  caller_location = get_caller_location
  args = format_args(msg)

  "#{timestamp} | #{session_id} | #{request_id} | #{severity} | #{caller_location} | #{args}\n"
end

#get_caller_locationObject



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

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
  file = location.path.split('/').last.split('.').first

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

#is_rails_rack_logger_msg?(msg) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_rails_rack_logger_msg?(msg)
  msg =~ /Started (GET|POST|PUT|PATCH|DELETE)/
end