Sha256: c46ced38c05970ee7899f022bfaa4e1a3125345e8535b78c6ce9481714228b4d

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

# Logger class variable mix-in
#
#   Lazy initialize and a logger class variable with instance accessor
#
#   By including this mix-in into any class it will define a class level logger
#   and make it accessible via instance methods
#
# Example
#
#  require 'semantic_logger'
#
#  class ExternalSupplier
#    # Lazy load 'logger' class variable on first use
#    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
        # Thread safe class variable initialization
        include SyncAttr

        sync_cattr_reader :logger do
          SemanticLogger::Logger.new(self)
        end
      end
    end

    # Also make the logger available as an instance method MixIn
    def logger
      self.class.logger
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
semantic_logger-1.0.1 lib/semantic_logger/loggable.rb
semantic_logger-1.0.0 lib/semantic_logger/loggable.rb
semantic_logger-0.11.4 lib/semantic_logger/loggable.rb
semantic_logger-0.11.3 lib/semantic_logger/loggable.rb
semantic_logger-0.11.2 lib/semantic_logger/loggable.rb
semantic_logger-0.11.1 lib/semantic_logger/loggable.rb
semantic_logger-0.11.0 lib/semantic_logger/loggable.rb