Class: Warren::LogTagger

Inherits:
Object
  • Object
show all
Defined in:
lib/warren/log_tagger.rb

Overview

Applies a tag to any messages sent to the logger.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:, tag:) ⇒ LogTagger

Create a new log tagger, which applies a tag to all messages before forwarding them on to the logger

Parameters:

  • logger (Logger)

    A ruby Logger, or compatible interface

  • tag (String)

    The tag to apply to each message



13
14
15
16
# File 'lib/warren/log_tagger.rb', line 13

def initialize(logger:, tag:)
  @logger = logger
  @tag = tag
end

Class Method Details

.level(name) ⇒ Void

Define ‘name` methods which forward on to the similarly named method on logger, with the tag applied

Parameters:

  • name (Symbol)

    The method to define

Returns:

  • (Void)


25
26
27
28
29
30
31
32
33
# File 'lib/warren/log_tagger.rb', line 25

def self.level(name)
  define_method(name) do |arg = nil, &block|
    if block
      @logger.public_send(name, arg) { tag(block.call) }
    else
      @logger.public_send(name, tag(arg))
    end
  end
end

Instance Method Details

#debug(message) ⇒ Object

Forwards message on to logger with #tag prefix See Logger::debug



38
# File 'lib/warren/log_tagger.rb', line 38

level :debug

#error(message) ⇒ Object

Forwards message on to logger with #tag prefix See Logger::error



50
# File 'lib/warren/log_tagger.rb', line 50

level :error

#info(message) ⇒ Object

Forwards message on to logger with #tag prefix See Logger::info



42
# File 'lib/warren/log_tagger.rb', line 42

level :info

#warn(message) ⇒ Object

Forwards message on to logger with #tag prefix See Logger::warn



46
# File 'lib/warren/log_tagger.rb', line 46

level :warn