Sha256: bc8f388986ce81e94958de4645c7bdbaf523304734f6b09367b0c5ad6567dffd

Contents?: true

Size: 1.61 KB

Versions: 22

Compression:

Stored size: 1.61 KB

Contents

require 'logger'

module Skylight
  module Util
    # Log both to the specified logger and STDOUT
    class AlertLogger
      def initialize(logger)
        @logger = logger
      end

      def write(*args)
        STDERR.write *args
        @logger.<<(*args)
      end

      def close
      end
    end

    module Logging
      def self.trace?
        ENV[TRACE_ENV_KEY]
      end

      if trace?
        def trace(msg, *args)
          log :debug, msg, *args
        end

        def t
          log :debug, yield
        end
      else
        def trace(*)
        end

        def t
        end
      end

      def debug(msg, *args)
        log :debug, msg, *args
      end

      def info(msg, *args)
        log :info, msg, *args
      end

      def warn(msg, *args)
        log :warn, msg, *args
      end

      def error(msg, *args)
        log :error, msg, *args
      end

      alias log_trace trace
      alias log_debug debug
      alias log_info  info
      alias log_warn  warn
      alias log_error error

      alias fmt       sprintf

      def log(level, msg, *args)
        return unless respond_to?(:config)
        return unless c = config

        if logger = c.logger
          return unless logger.respond_to?(level)

          if args.length > 0
            logger.send level, sprintf("[SKYLIGHT] [#{Skylight::VERSION}] #{msg}", *args)
          else
            logger.send level, "[SKYLIGHT] [#{Skylight::VERSION}] #{msg}"
          end
        end
      rescue Exception => e
        if ENV[TRACE_ENV_KEY]
          puts "[ERROR] #{e.message}"
          puts e.backtrace
        end
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
skylight-0.8.0.beta.3 lib/skylight/util/logging.rb
skylight-0.8.0.beta.1 lib/skylight/util/logging.rb
skylight-0.7.1 lib/skylight/util/logging.rb
skylight-0.7.0.beta.3 lib/skylight/util/logging.rb
truex-skylight-0.6.0 lib/skylight/util/logging.rb
skylight-0.7.0.beta.2 lib/skylight/util/logging.rb
skylight-0.7.0.beta.1 lib/skylight/util/logging.rb
skylight-0.6.2.beta.2 lib/skylight/util/logging.rb
skylight-0.6.1 lib/skylight/util/logging.rb
skylight-0.6.0 lib/skylight/util/logging.rb
skylight-0.6.0.beta.1 lib/skylight/util/logging.rb
skylight-0.5.2 lib/skylight/util/logging.rb
skylight-0.5.1 lib/skylight/util/logging.rb
skylight-0.5.0 lib/skylight/util/logging.rb
skylight-0.4.3 lib/skylight/util/logging.rb
skylight-0.5.0.beta1 lib/skylight/util/logging.rb
skylight-0.4.2 lib/skylight/util/logging.rb
skylight-0.4.1 lib/skylight/util/logging.rb
skylight-0.4.0 lib/skylight/util/logging.rb
skylight-0.4.0.beta2 lib/skylight/util/logging.rb