Module: Zapp::Logger::Base

Included in:
Zapp::Logger
Defined in:
lib/zapp/logger/base.rb

Overview

Base contains all the logging functionality and is included both as class and instance methods of Zap::Logger This allows logging without creating new instances, while allowing Ractors to create their own instances for thread safety

Constant Summary collapse

LEVELS =
{ TRACE: 0, DEBUG: 1, INFO: 2, WARN: 3, ERROR: 4 }.freeze
FROZEN_ENV =
ENV.map { |k, v| [k.freeze, v.freeze] }
.to_h.freeze
OUT_IO_MUTEX_KEY =

The hash key in Ractor.current that stores the mutex for writing to output

"ZAPP_LOGGER_OUT_IO_MUTEX"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelObject



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

def level
  @level ||= begin
    log_level = FROZEN_ENV["LOG_LEVEL"]

    if log_level == "" || log_level.nil?
      LEVELS[:DEBUG]
    else
      resolved_level = LEVELS[log_level.upcase.to_sym]

      if resolved_level.nil?
        raise(
          Zapp::ZappError,
          "Invalid log level '#{log_level.upcase}', must be one of [#{LEVELS.keys.join(', ')}]"
        )
      end

      resolved_level
    end
  end
end

#prefix=(value) ⇒ Object

Sets the attribute prefix

Parameters:

  • value

    the value to set the attribute prefix to.



9
10
11
# File 'lib/zapp/logger/base.rb', line 9

def prefix=(value)
  @prefix = value
end

Instance Method Details

#debug(msg) ⇒ Object



21
# File 'lib/zapp/logger/base.rb', line 21

def debug(msg) = log("DEBUG", msg)

#error(msg) ⇒ Object



27
# File 'lib/zapp/logger/base.rb', line 27

def error(msg) = log("ERROR", msg)

#flushObject



56
57
58
59
60
61
62
# File 'lib/zapp/logger/base.rb', line 56

def flush
  writing_thread_pool.wait_for_termination(0.1)

  out_io_mutex.synchronize do
    out.flush
  end
end

#info(msg) ⇒ Object



23
# File 'lib/zapp/logger/base.rb', line 23

def info(msg) = log("INFO", msg)

#log(current_level, msg, **_tags) ⇒ Object



50
51
52
53
54
# File 'lib/zapp/logger/base.rb', line 50

def log(current_level, msg, **_tags)
  return unless level <= LEVELS[current_level.to_sym]

  write("--- #{prefix} [#{current_level}] #{msg}\n")
end

#out=(new_out) ⇒ Object

Parameters:

  • new_out (IO)


65
66
67
# File 'lib/zapp/logger/base.rb', line 65

def out=(new_out)
  @out = new_out
end

#trace(msg) ⇒ Object



19
# File 'lib/zapp/logger/base.rb', line 19

def trace(msg) = log("TRACE", msg)

#warn(msg) ⇒ Object



25
# File 'lib/zapp/logger/base.rb', line 25

def warn(msg) = log("WARN", msg)