Sha256: 2fc6e872ddc08b50d7dab75ac0845e823632e49876ca041379beeccbe33dc649

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# Logger class variable mix-in
#
#   Lazy initialize a logger class variable with instance accessor
#
#   By including this mix-in into any class it will define a class level logger
#   and also make it accessible via instance methods
#
# Example
#
#  require 'semantic_logger'
#  SemanticLogger.default_level = :debug
#  SemanticLogger.add_appender(STDOUT)
#
#  class ExternalSupplier
#    # Create class and instance logger methods
#    include SemanticLogger::Loggable
#
#    def call_supplier(amount, name)
#      logger.debug "Calculating with amount", { amount: amount, name: name }
#
#      # Measure and log on completion how long the call took to the external supplier
#      logger.benchmark_info "Calling external interface" do
#        # Code to call the external supplier ...
#      end
#    end
#  end
module SemanticLogger
  module Loggable

    def self.included(base)
      base.class_eval do
        # Returns [SemanticLogger::Logger] class level logger
        def self.logger
          @semantic_logger ||= SemanticLogger[self]
        end

        # Returns [SemanticLogger::Logger] instance level logger
        def logger
          @semantic_logger ||= self.class.logger
        end

        # Replace instance level logger
        def logger=(logger)
          @semantic_logger = logger
        end
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
semantic_logger-3.0.1 lib/semantic_logger/loggable.rb
semantic_logger-3.0.0 lib/semantic_logger/loggable.rb
semantic_logger-2.21.0 lib/semantic_logger/loggable.rb
semantic_logger-2.20.0 lib/semantic_logger/loggable.rb