Sha256: 733e597db5bb118f2caed7ab133b1c3a4a65d7b2bc0493bd7ff7a3c66938b022

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

# -*- coding: utf-8 -*-
require 'logger'

module Lanes

    class MultiDestinationLogger
        def initialize
            @targets = [STDOUT]
            @targets.push(
                File.open("log/#{Lanes.config.environment}.log", "a")
            ) if File.writable?("log")
            Lanes.config.get(:environment) do
                @targets.each{|t| t.sync = ! Lanes.env.production? }
            end
        end

        def write(*args)
            @targets.each {|t| t.write(*args)}
        end

        def close
            @targets.each(&:close)
        end

    end

    class << self
        def logger
            @logger ||= _create_logger
        end

        def logger=( logger )
            @logger = logger
        end

        def silence_logs( &block )
            old_logger = Lanes.logger
            begin
                Lanes.logger=Logger.new( StringIO.new )
                yield
            ensure
                Lanes.logger=old_logger
            end
        end

        def logger_debug( output )
            logger.debug '⚡ '*40
            logger.debug '⚡ ' + output
            logger.debug '⚡ '*40
        end

        private

        def _create_logger
            if defined?(::Rails)
                Rails.logger
            else
                if Lanes.env.production?
                    dest = if FileTest.writable?("log/production.log")
                               "log/production.log"
                           else
                               STDOUT
                           end
                    logger = ::Logger.new(dest)
                    logger.level = ::Logger::WARN
                    logger
                else
                    logger = ::Logger.new MultiDestinationLogger.new
                    logger.level = ::Logger::DEBUG
                    logger
                end
            end
        end


    end

    Lanes.config.get(:environment) do | env |
        self.logger=nil # it'll be re-opened on next write
    end


end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lanes-0.7.0 lib/lanes/logger.rb
lanes-0.6.1 lib/lanes/logger.rb
lanes-0.6.0 lib/lanes/logger.rb