class Warren::LogTagger

Applies a tag to any messages sent to the logger.

Public Class Methods

level(name) click to toggle source

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

@param name [Symbol] The method to define

@return [Void]

# 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
new(logger:, tag:) click to toggle source

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

@param logger [Logger] A ruby Logger, or compatible interface @param tag [String] The tag to apply to each message

# File lib/warren/log_tagger.rb, line 13
def initialize(logger:, tag:)
  @logger = logger
  @tag = tag
end

Private Instance Methods

tag(message) click to toggle source
# File lib/warren/log_tagger.rb, line 54
def tag(message)
  "#{@tag}: #{message}"
end