Module: Waylon::Logger

Defined in:
lib/waylon/logger.rb

Overview

A simple way to abstract logging

Class Method Summary collapse

Class Method Details

.json_loggerObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/waylon/logger.rb', line 30

def self.json_logger
  a_logger = ::Logger.new($stderr)
  a_logger.level = level
  a_logger.progname = "Waylon"
  a_logger.formatter = proc do |severity, datetime, progname, msg|
    json_data = JSON.dump(
      ts: datetime,
      severity: severity.ljust(5).to_s,
      progname:,
      pid: Process.pid,
      message: msg,
      v: Waylon::Core::VERSION
    )
    "#{json_data}\n"
  end

  a_logger
end

.levelString

The log level as defined in the global Config singleton

Returns:

  • (String)

    The current log level



8
9
10
# File 'lib/waylon/logger.rb', line 8

def self.level
  Config.instance["global.log.level"] || "info"
end

.log(message, level = :info) ⇒ Object

Abstraction for sending logs to the logger at some level

Parameters:

  • level (Symbol) (defaults to: :info)

    The log level this message corresponds to

  • message (String)

    The message to log at this specified level



15
16
17
# File 'lib/waylon/logger.rb', line 15

def self.log(message, level = :info)
  logger.send(level, message)
end

.loggerLogger

Provides an easy way to access the underlying logger

Returns:

  • (Logger)

    The Logger instance



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

def self.logger
  @logger ||= json_logger
end

.logger=(logger) ⇒ Object

Parameters:

  • logger (Logger)

    The Logger instance to use



26
27
28
# File 'lib/waylon/logger.rb', line 26

def self.logger=(logger)
  @logger = logger
end