Class: NRSER::Log::Plugin Abstract
Overview
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
Instance Attribute Summary collapse
-
#logger ⇒ NRSER::Log::Logger
readonly
The wrapped logger instance.
Class Method Summary collapse
-
.method_name ⇒ Symbol | String
The Logger method name that will create plugin instances.
Instance Method Summary collapse
-
#call(level:, message:, payload:, exception:, metric:, &block) ⇒ Object
This is where realizing subclasses can hook into log calls.
-
#initialize(logger) ⇒ Plugin
constructor
Instantiate a new plugin instance.
-
#to_s ⇒ String
Short string description of the instance.
Constructor Details
#initialize(logger) ⇒ Plugin
Instantiate a new plugin instance.
84 85 86 |
# File 'lib/nrser/log/plugin.rb', line 84 def initialize logger @logger = logger end |
Instance Attribute Details
#logger ⇒ NRSER::Log::Logger (readonly)
The wrapped logger instance.
73 74 75 |
# File 'lib/nrser/log/plugin.rb', line 73 def logger @logger end |
Class Method Details
.method_name ⇒ Symbol | 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.
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
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`.
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: , payload: payload, exception: exception, metric: metric, &block end |
#to_s ⇒ String
Returns 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 |