Sha256: b9b5761ed575c4aecc9071c413d69d3dcbccbcc6b490b195b77c53f499da03f0

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 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(severity, message, progname, &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

      @store.report(severity, progname, message, {
        env: Thread.current[LOGSTER_ENV]
      })

    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

12 entries across 12 versions & 1 rubygems

Version Path
logster-1.0.0.0.pre lib/logster/logger.rb
logster-0.9.9 lib/logster/logger.rb
logster-0.8.4.8.pre lib/logster/logger.rb
logster-0.8.4.7.pre lib/logster/logger.rb
logster-0.8.4.6.pre lib/logster/logger.rb
logster-0.8.4.5.pre lib/logster/logger.rb
logster-0.8.4.4.pre lib/logster/logger.rb
logster-0.8.4.3.pre lib/logster/logger.rb
logster-0.8.4.2.pre lib/logster/logger.rb
logster-0.8.4.1.pre lib/logster/logger.rb
logster-0.8.4.pre lib/logster/logger.rb
logster-0.8.3 lib/logster/logger.rb