Class: Utility::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/utility/logger.rb

Constant Summary collapse

SUPPORTED_LOG_LEVELS =
%i[fatal error warn info debug].freeze
MAX_SHORT_MESSAGE_LENGTH =
1000.freeze

Class Method Summary collapse

Class Method Details

.abbreviated_message(message) ⇒ Object



61
62
63
# File 'lib/utility/logger.rb', line 61

def abbreviated_message(message)
  message.gsub(/\s+/, ' ').strip.truncate(MAX_SHORT_MESSAGE_LENGTH)
end

.error_with_backtrace(message: nil, exception: nil, prog_name: nil) ⇒ Object



47
48
49
50
51
# File 'lib/utility/logger.rb', line 47

def error_with_backtrace(message: nil, exception: nil, prog_name: nil)
  logger.error(prog_name) { message } if message
  logger.error exception.message if exception
  logger.error exception.backtrace.join("\n") if exception
end

.generate_trace_idObject



57
58
59
# File 'lib/utility/logger.rb', line 57

def generate_trace_id
  SecureRandom.uuid
end

.level=(log_level) ⇒ Object



21
22
23
# File 'lib/utility/logger.rb', line 21

def level=(log_level)
  logger.level = log_level
end

.log_stacktrace(stacktrace) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/utility/logger.rb', line 39

def log_stacktrace(stacktrace)
  if logger.is_a?(EcsLogging::Logger)
    logger.error(nil, extra_ecs_fields.merge(:error => { :stack_trace => stacktrace }))
  else
    logger.error(stacktrace)
  end
end

.loggerObject



25
26
27
# File 'lib/utility/logger.rb', line 25

def logger
  @logger ||= defined?(::Settings) && ::Settings[:ecs_logging] ? EcsLogging::Logger.new(STDOUT) : ::Logger.new(STDOUT)
end

.new_lineObject



53
54
55
# File 'lib/utility/logger.rb', line 53

def new_line
  logger.info("\n")
end