Sha256: 933fd91499812a6bb0027f0273ad17a2fb53147edef7c7a26a9e00dc65d7cd21

Contents?: true

Size: 1.13 KB

Versions: 1

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::Attribute
#
#    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 Attribute

    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

1 entries across 1 versions & 1 rubygems

Version Path
semantic_logger-0.10.0 lib/semantic_logger/attribute.rb