Module: SemanticLogger::Loggable
- Defined in:
- lib/semantic_logger/loggable.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#logger ⇒ Object
Also make the logger available as an instance method MixIn The class logger can be replaced using an instance specific #logger= below.
-
#logger=(logger) ⇒ Object
Set instance specific logger.
Class Method Details
.included(base) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/semantic_logger/loggable.rb', line 32 def self.included(base) base.class_eval do # Thread safe class variable initialization include SyncAttr sync_cattr_reader :logger do SemanticLogger[self] end end end |
Instance Method Details
#logger ⇒ Object
Also make the logger available as an instance method MixIn The class logger can be replaced using an instance specific #logger= below
45 46 47 |
# File 'lib/semantic_logger/loggable.rb', line 45 def logger @semantic_logger ||= self.class.logger end |
#logger=(logger) ⇒ Object
Set instance specific logger
By default instances of the class will use the class logger. Sometimes it is useful to be able to add instance specific logging data to the class name.
For example, server or host_name that the class instance is using.
Example:
require 'semantic_logger'
SemanticLogger.default_level = :debug
SemanticLogger.add_appender(STDOUT)
class MyClass
include SemanticLogger::Loggable
def self.my_name=(my_name)
# Use class level logger that only logs class name
logger.info "My name is changed to #{my_name}"
@@my_name = my_name
end
def initialize(host_name)
# Add host_name to every log entry in this logging instance
self.logger = SemanticLogger["#{self.class.name} [#{host_name}]"]
logger.info "Started server"
end
def check
logger.debug "Checking..."
end
end
MyClass.my_name = "Joe"
mine = MyClass.new('server.com')
mine.check
# Generates the following log output:
2013-04-02 15:08:42.368574 I [37279:70198560687720] MyClass – My name is changed to Joe 2013-04-02 15:08:42.369934 I [37279:70198560687720] MyClass [server.com] – Started server 2013-04-02 15:08:42.371171 D [37279:70198560687720] MyClass [server.com] – Checking…
94 95 96 |
# File 'lib/semantic_logger/loggable.rb', line 94 def logger=(logger) @semantic_logger = logger end |