Sha256: e7764e75cf29f6806f67a9cb977c5328d4752c2a4c66a80511804b2c6177b794

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'webrick'

module Intranet
  # The default logger for the Intranet. It is based on +WEBrick::BasicLog+ but adds timestamp and
  # colors to the messages.
  class Logger < WEBrick::BasicLog
    COLOR = {
      FATAL => "\033[1;37;41m",
      ERROR => "\033[0;31m",
      WARN => "\033[0;33m",
      INFO => "\033[0;36m",
      DEBUG => "\033[0;35m"
    }.freeze
    private_constant :COLOR

    # Initializes a new logger for $stderr that outputs messages at +level+ or higher.
    # +level+ can be modified later (attribute accessor).
    # @param level The initial log level.
    def initialize(level = INFO)
      super($stderr, level)
    end

    # Logs a message at a given level if it is above the current log level.
    # @param level The level of the message
    # @param msg [String] The message
    def log(level, msg)
      super(level, COLOR[level] + Time.now.strftime('[%Y-%m-%d %H:%M:%S] ') + msg + "\033[0m")
    end

    # Logs an object that responds to +to_s+ at level INFO.
    # @param obj [Object] The object to log.
    def <<(obj)
      info(obj.to_s.chomp)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
intranet-core-2.2.0 lib/intranet/logger.rb
intranet-core-2.1.4 lib/intranet/logger.rb
intranet-core-2.1.2 lib/intranet/logger.rb
intranet-core-2.1.1 lib/intranet/logger.rb
intranet-core-2.1.0 lib/intranet/logger.rb
intranet-core-2.0.0 lib/intranet/logger.rb
intranet-core-1.2.0 lib/intranet/logger.rb
intranet-core-1.1.1 lib/intranet/logger.rb
intranet-core-1.0.2 lib/intranet/logger.rb
intranet-core-1.0.1 lib/intranet/logger.rb
intranet-core-1.0.0 lib/intranet/logger.rb