Sha256: 19e0861088760a2a9a7ea013ae55508ed4649295260c3638e66c81da33ddc319

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

# Wrapper appender
#
#   Wraps the Rails log, log4r, or Ruby Logger with the SemanticLogger API's
#
module SemanticLogger
  module Appender
    class Wrapper < SemanticLogger::Appender::Base
      attr_reader :logger

      # Create a Logger or Rails Logger appender instance
      #
      # Ruby Logger
      #    require 'logger'
      #    require 'semantic_logger'
      #    ruby_logger = Logger.new(STDOUT)
      #    SemanticLogger.add_appender(ruby_logger)
      #    logger =  SemanticLogger['test']
      #    logger.info('Hello World', :some => :payload)
      #
      # Enhance the Rails Logger
      #    # Add the Rails logger to the list of appenders
      #    SemanticLogger.add_appender(Rails.logger)
      #    Rails.logger = SemanticLogger['Rails']
      #
      #    # Make ActiveRecord logging include its class name in every log entry
      #    ActiveRecord::Base.logger = SemanticLogger['ActiveRecord']
      #
      # Note: Since the log level is controlled by setting the Ruby or Rails logger directly
      #   the level is ignored for this appender
      def initialize(logger, filter=nil, &block)
        raise "logger cannot be null when initiailizing the SemanticLogging::Appender::Wrapper" unless logger
        @logger = logger

        # Set the formatter to the supplied block
        @formatter = block || self.default_formatter
        super(nil, filter, &block)
      end

      # Pass log calls to the underlying Rails, log4j or Ruby logger
      #  trace entries are mapped to debug since :trace is not supported by the
      #  Ruby or Rails Loggers
      def log(log)
        @logger.send(log.level == :trace ? :debug : log.level, @formatter.call(log))
      end

      # Flush all pending logs to disk.
      #  Waits for all sent documents to be writted to disk
      def flush
        @logger.flush if @logger.respond_to?(:flush)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
semantic_logger-2.9.0 lib/semantic_logger/appender/wrapper.rb
semantic_logger-2.8.0 lib/semantic_logger/appender/wrapper.rb