Sha256: e12f1afdfcb184a91e53c1f0dbcc51d2a0480a1e37e5632007eae7b105250cc3

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

require 'logger'

module Logster
  class Logger < ::Logger
    LOGSTER_ENV = "logster_env".freeze

    attr_accessor :store, :skip_store

    def initialize(store)
      super(nil)
      @store = store
    end

    def chain(logger)
      @chained ||= []
      @chained << logger
    end

    def add_to_chained(logger, severity, message, progname, &block)
      old = nil
      if logger.respond_to? :skip_store
        old = logger.skip_store
        logger.skip_store = @skip_store
      end

      logger.add(severity, message, progname, &block)

    ensure
      if logger.respond_to? :skip_store
        logger.skip_store = old
      end
    end

    def add(*args, &block)
      add_with_opts(*args,&block)
    end


    def add_with_opts(severity, message, progname, opts=nil, &block)
      if severity < @level
        return true
      end

      if @chained
        i = 0
        # micro optimise for logging
        while i < @chained.length
          # TODO double yielding blocks
          begin
            add_to_chained(@chained[i], severity, message, progname, &block)
          rescue => e
            # don't blow up if STDERR is somehow closed
            STDERR.puts "Failed to report message to chained logger #{e}" rescue nil
          end
          i += 1
        end
      end

      progname ||= @progname
      if message.nil?
        if block_given?
          message = yield
        else
          message = progname
          progname = @progname
        end
      end

      return if @skip_store

      opts ||= {}
      opts[:env] ||= Thread.current[LOGSTER_ENV]

      @store.report(severity, progname, message, opts)

    rescue => e
      # don't blow up if STDERR is somehow closed
      STDERR.puts "Failed to report error: #{e} #{severity} #{message} #{progname}" rescue nil
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
logster-1.2.4 lib/logster/logger.rb
logster-1.2.3 lib/logster/logger.rb
logster-1.2.2 lib/logster/logger.rb
logster-1.2.1 lib/logster/logger.rb
logster-1.2.0 lib/logster/logger.rb
logster-1.1.1 lib/logster/logger.rb
logster-1.0.1 lib/logster/logger.rb
logster-1.0.0.3.pre lib/logster/logger.rb