lib/lorekeeper/simple_logger.rb in lorekeeper-1.6.2 vs lib/lorekeeper/simple_logger.rb in lorekeeper-1.7.0
- old
+ new
@@ -1,24 +1,30 @@
# frozen_string_literal: true
-# The comment above will make all strings in a current file frozen
+
require 'lorekeeper/fast_logger'
module Lorekeeper
# Simple logger provides a logger which outputs messages in a colorized simple text format.
class SimpleLogger < FastLogger
# From http://misc.flogisoft.com/bash/tip_colors_and_formatting
# 39: default for the theme
# 33: yellow
# 31: red
# 37: light gray
+
+ COLOR_DEFAULT = '39'
+ COLOR_YELLOW = '33'
+ COLOR_RED = '31'
+ COLOR_LIGHT_GRAY = '37'
+
SEVERITY_TO_COLOR_MAP = {
- DEBUG => '39'.freeze,
- INFO => DEBUG,
- WARN => '33'.freeze,
- ERROR => '31'.freeze,
- FATAL => ERROR,
- UNKNOWN => '37'.freeze
+ DEBUG => COLOR_DEFAULT,
+ INFO => COLOR_DEFAULT,
+ WARN => COLOR_YELLOW,
+ ERROR => COLOR_RED,
+ FATAL => COLOR_RED,
+ UNKNOWN => COLOR_LIGHT_GRAY
}.freeze
# \e[colorm sets a color \e[0m resets all properties
def log_data(severity, message)
color = SEVERITY_TO_COLOR_MAP[severity]
@@ -35,18 +41,19 @@
return true if METHOD_SEVERITY_MAP[method_name] < @level
log_data(METHOD_SEVERITY_MAP[method_name], "#{message_param}, data: #{data}")
end
end
- def exception(exception, custom_message = nil, custom_data = nil)
+ def exception(exception, custom_message = nil, custom_data = nil, level = :error)
+ log_level = METHOD_SEVERITY_MAP[level] || ERROR
+
if exception.is_a?(Exception)
backtrace = exception.backtrace || []
message = custom_message || exception.message
- error_with_data("#{exception.class}: #{message}", backtrace.join("\n"))
+ log_data(log_level, "#{exception.class}: #{exception.message}; #{message}, data: #{backtrace.join("\n")}")
else
- log_data(:warning, 'Logger exception called without exception class.')
+ log_data(METHOD_SEVERITY_MAP[:warn], 'Logger exception called without exception class.')
error_with_data("#{exception.class}: #{exception.inspect} #{custom_message}", custom_data)
end
end
end
-
end