Class: NRSER::Log::Plugin Abstract

Inherits:
Object show all
Defined in:
lib/nrser/log/plugin.rb

Overview

This class is abstract.
TODO:

Make plugins “stackable”.

Not sure if this example in particular would make any sense, but you can get the idea:

logger.notify.catcher.warn

Abstract base class that wraps a Logger and exposes the level logging methods - ‘#debug`, `#info`, etc. - routing those calls through it’s #call method and on to the logger’s method, allowing concrete subclasses to hook into those calls and provide additional functionality and logic.

Convention is

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Plugin

Instantiate a new plugin instance.

Parameters:



84
85
86
# File 'lib/nrser/log/plugin.rb', line 84

def initialize logger
  @logger = logger
end

Instance Attribute Details

#loggerNRSER::Log::Logger (readonly)

The wrapped logger instance.

Returns:



73
74
75
# File 'lib/nrser/log/plugin.rb', line 73

def logger
  @logger
end

Class Method Details

.method_nameSymbol | String

The Logger method name that will create plugin instances.

Looks for the ‘@method_name` class instance variable, and defaults to `safe_name.demodulize.underscore` if that is not found.

Returns:



61
62
63
# File 'lib/nrser/log/plugin.rb', line 61

def self.method_name
  @method_name || safe_name.demodulize.underscore
end

Instance Method Details

#call(level:, message:, payload:, exception:, metric:, &block) ⇒ Object

Note:

Though the logging methods don’t use a block at this time, it’s there for completeness and possible futures.

This is where realizing subclasses can hook into log calls.

This base implementation just calls the ‘level` method on #logger with the `args` and `block`.

Parameters:

  • level (Symbol)

    The log level of the call; one of SemanticLogger::LEVELS.

  • message (String?)

    Log message.

  • payload (Hash?)

    Map of names to values to log.

  • exception (Object?)

    An error to log. This will be an Exception in MRI, but *won’t* be in JRuby and possibly other runtimes.

  • metric (Object?)

    I don’t know what this is. I found it in the SemLog code.

  • block (Proc)

    Block to pass to the #logger‘s `level` method. Not currently used (see note above).



121
122
123
124
125
126
127
128
129
# File 'lib/nrser/log/plugin.rb', line 121

def call level:, message:, payload:, exception:, metric:, &block
  logger.send \
    level,
    message: message,
    payload: payload,
    exception: exception,
    metric: metric,
    &block
end

#to_sString

Returns Short string description of the instance.

Returns:

  • (String)

    Short string description of the instance.



142
143
144
# File 'lib/nrser/log/plugin.rb', line 142

def to_s
  "#<#{ self.class.safe_name } #{ logger }>"
end