lib/semantic_logger/logger.rb in semantic_logger-2.0.0 vs lib/semantic_logger/logger.rb in semantic_logger-2.1.0

- old
+ new

@@ -1,45 +1,11 @@ require 'thread_safe' -# Logger is the interface used by -# -# Logger maintains the logging name to be used for all log entries generated -# by the invoking classes or modules -# -# It is recommended to create an instance of the class for every class or -# module so that it can be uniquely identified and searched on -# -# Example, log to Logger: -# require 'logger' -# require 'semantic_logger' -# log = Logger.new(STDOUT) -# log.level = Logger::DEBUG -# -# SemanticLogger::Logger.appenders << SemanticLogger::Appender::Logger.new(log) -# -# logger = SemanticLogger::Logger.new("my.app.class") -# logger.debug("Login time", :user => 'Joe', :duration => 100, :ip_address=>'127.0.0.1') -# -# # Now log to the Logger above as well as MongoDB at the same time -# -# db = Mongodb::Connection.new['production_logging'] -# -# SemanticLogger::Logger.appenders << SemanticLogger::Appender::MongoDB.new( -# :db => db, -# :collection_size => 25.gigabytes -# ) -# ... -# # This will be logged to both the Ruby Logger and MongoDB -# logger.debug("Login time", :user => 'Mary', :duration => 230, :ip_address=>'192.168.0.1') -# +# Logger stores the class name to be used for all log messages so that every +# log message written by this instance will include the class name module SemanticLogger class Logger < Base - # Add or remove logging appenders to the thread-safe appenders Array - # Appenders will be written to in the order that they appear in this list - def self.appenders - @@appenders - end # Returns a Logger instance # # Return the logger for a specific class, supports class specific log levels # logger = SemanticLogger::Logger.new(self) @@ -51,15 +17,15 @@ # A class, module or a string with the application/class name # to be used in the logger # # level # The initial log level to start with for this logger instance - # Default: SemanticLogger::Logger.default_level + # Default: SemanticLogger.default_level # def initialize(klass, level=nil) @name = klass.is_a?(String) ? klass : klass.name - self.level = level || self.class.default_level + self.level = level || SemanticLogger.default_level end # Returns [Integer] the number of log entries that have not been written # to the appenders # @@ -69,16 +35,10 @@ # look into speeding up the appenders themselves def self.queue_size queue.size end - # DEPRECATED: Please use queue_size instead. - def self.cache_count - warn "[DEPRECATION] 'SemanticLogger::Logger.cache_count' is deprecated. Please use 'SemanticLogger::Logger.queue_size' instead." - queue_size - end - # Flush all queued log entries disk, database, etc. # All queued log messages are written and then each appender is flushed in turn def self.flush return false unless appender_thread_active? @@ -119,15 +79,25 @@ # while trying to log to the various appenders def self.logger=(logger) @@logger = logger end + # DEPRECATED See SemanticLogger.add_appender + def self.appenders + warn "[DEPRECATION] `SemanticLogger::Logger.appenders` is deprecated. Please use `SemanticLogger.add_appender` instead." + SemanticLogger.appenders + end + # DEPRECATED: Please use queue_size instead. + def self.cache_count + warn "[DEPRECATION] 'SemanticLogger::Logger.cache_count' is deprecated. Please use 'SemanticLogger::Logger.queue_size' instead." + queue_size + end + ############################################################################ protected - @@appenders = ThreadSafe::Array.new @@appender_thread = nil @@queue = Queue.new # Queue to hold messages that need to be logged to the various appenders def self.queue @@ -175,11 +145,11 @@ logger.debug "V#{VERSION} Appender thread active" begin count = 0 while message = queue.pop if message.is_a? Log - appenders.each do |appender| + SemanticLogger.appenders.each do |appender| begin appender.log(message) rescue Exception => exc logger.error "Appender thread: Failed to log to appender: #{appender.inspect}", exc end @@ -193,10 +163,10 @@ count = 0 end else case message[:command] when :flush - appenders.each do |appender| + SemanticLogger.appenders.each do |appender| begin logger.info "Appender thread: Flushing appender: #{appender.name}" appender.flush rescue Exception => exc logger.error "Appender thread: Failed to flush appender: #{appender.inspect}", exc