Sha256: 6a323fc448296ac770349353694ede0a85698c90b27806526aaf2cf61e2ba00d

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# typed: false

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'singleton'
require 'logger'
require 'sqreen/logger'

module Sqreen
  class DeferredLogger
    include Singleton

    def initialize
      @buffer = StringIO.new
      @logger = ::Logger.new(@buffer)
    end

    def debug?
      true
    end

    def debug(msg = nil, &block)
      @logger.debug(msg, &block)
    end

    def info(msg = nil, &block)
      @logger.info(msg, &block)
    end

    def warn(msg = nil, &block)
      @logger.warn(msg, &block)
    end

    def error(msg = nil, &block)
      @logger.error(msg, &block)
    end

    def fatal(msg = nil, &block)
      @logger.error(msg, &block)
    end

    def add(severity, msg = nil, &block)
      send(Sqreen::Logger::SEVERITY_TO_METHOD[severity], msg, &block)
    end

    def formatter=(value)
      @logger.formatter = value
    end

    def flush_to(logger)
      logger.instance_eval { @logdev }.write(read).tap { reset }
    end

    private

    def read
      @buffer.rewind
      @buffer.read
    end

    def reset
      buffer = StringIO.new
      logger = ::Logger.new(buffer)
      logger.formatter = @logger.formatter
      @buffer, @logger = buffer, logger
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sqreen-1.20.3 lib/sqreen/deferred_logger.rb
sqreen-1.20.2 lib/sqreen/deferred_logger.rb