Module: NRSER::Log::Mixin

Included in:
NRSER, AbstractMethodError, NRSER::Log, Props::ClassMethods, Props::Metadata, Props::Prop, Types
Defined in:
lib/nrser/log/mixin.rb

Overview

Adaptation of SemanticLogger::Loggable mixin to use Logger instances from [].

Like SemanticLogger::Loggable adds class and instance ‘logger` and `logger=` methods that create loggers on demand and store them in the `@semantic_logger` instance variables.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Class Methods



46
47
48
49
50
51
# File 'lib/nrser/log/mixin.rb', line 46

def self.included base
  base.extend ClassMethods
  
  # Adds `.logger_measure_method`
  base.extend SemanticLogger::Loggable::ClassMethods
end

Instance Method Details

#loggerSemanticLogger::Logger

Gets the Logger for use in the instance. This will be the class logger from NRSER::Log::Mixin::ClassMethods#logger unless the instance has a ‘#create_logger` method.

The ‘#create_logger` method is expected to return a Logger instance, which will be saved in the `@semantic_logger` instance variable for future use.

That method does not need to return a different logger for every instance

  • if you just want to have a different logger for all instance with a

different level or formatter or whatever, you can save it somewhere else and always return that same instance.

If you are dealing with frozen instances make sure to call ‘#logger` before you freeze (likely in the constructor). If you don’t want to save the logger at all, just override this method itself.

This is a departure from SemanticLogger::Loggable that started because if the instance is frozen then attempting to set ‘@semantic_logger` will raise an error.

I also started ending up with classes that wanted to individually configure their loggers, so it seemed like we could take out two birds with this stone.

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nrser/log/mixin.rb', line 86

def logger
  return @semantic_logger if @semantic_logger
  
  if respond_to?( :create_logger, true )
    @semantic_logger = begin
      create_logger
    rescue Exception => error
      self.class.logger.warn \
        "Error creating instance logger",
        { instance: self.inspect },
        error
      
      self.class.logger
    end
  else
    self.class.logger
  end
end

#logger=(logger) ⇒ Object

Replace instance level logger



107
108
109
# File 'lib/nrser/log/mixin.rb', line 107

def logger= logger
  @semantic_logger = logger
end