Class: ConsoleLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/apimatic-core/logger/default_logger.rb

Overview

Represents a logger implementation that logs messages to the console.

Instance Method Summary collapse

Constructor Details

#initializeConsoleLogger

Initializes a new instance of DefaultLogger.



4
5
6
7
# File 'lib/apimatic-core/logger/default_logger.rb', line 4

def initialize
  @logger = ::Logger.new($stdout)
  @logger.formatter = method(:format_log_message)
end

Instance Method Details

#format_log_message(severity, _datetime, _progname, msg) ⇒ Object



31
32
33
34
35
36
# File 'lib/apimatic-core/logger/default_logger.rb', line 31

def format_log_message(severity, _datetime, _progname, msg)
  "#{
     severity.ljust(5) +
     msg
   }\n"
end

#log(level, message, params = {}) ⇒ Object

Logs a message to the console with the specified log level.

Parameters:

  • level (Symbol)

    The log level of the message.

  • message (String)

    The message to log.

  • params (Hash) (defaults to: {})

    Additional parameters to include in the log message.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/apimatic-core/logger/default_logger.rb', line 13

def log(level, message, params = {})
  formatted_message = message_with_params(message, params)
  case level
  when Logger::DEBUG
    @logger.debug(formatted_message)
  when Logger::INFO
    @logger.info(formatted_message)
  when Logger::WARN
    @logger.warn(formatted_message)
  when Logger::ERROR
    @logger.error(formatted_message)
  when Logger::FATAL
    @logger.fatal(formatted_message)
  else
    @logger.unknown(formatted_message)
  end
end

#message_with_params(message, params) ⇒ Object



38
39
40
41
42
43
# File 'lib/apimatic-core/logger/default_logger.rb', line 38

def message_with_params(message, params)
  message.gsub(/\{([\w-]+)\}/) do |match|
    key = match[1..-2]
    params[key.to_sym] || params[key.to_s]
  end
end