Sha256: 203df223c9b33db5d8fe6e31cde36368afda40ef50124e5febe6aebff3d2263e

Contents?: true

Size: 1.2 KB

Versions: 12

Compression:

Stored size: 1.2 KB

Contents

require 'phut'

module Trema
  # The default logger.
  class Logger
    def initialize(name)
      @name = name
      @logger = {}.tap do |list|
        list[:file] = create_file_logger
        list[:stdout] = create_stdout_logger
      end
    end

    def fatal(message)
      output :fatal, message
    end

    def error(message)
      output :error, message
    end

    def warn(message)
      output :warn, message
    end

    def info(message)
      output :info, message
    end

    def debug(message)
      output :debug, message
    end

    def level=(level)
      @logger.values.each { |each| each.__send__ :level=, level }
    end

    def level
      @logger[:file].level
    end

    private

    def output(message_type, message)
      @logger.values.each { |each| each.__send__ message_type, message }
      message
    end

    def create_file_logger
      ::Logger.new("#{Phut.log_dir}/#{@name}.log").tap do |logger|
        logger.level = ::Logger::INFO
      end
    end

    def create_stdout_logger
      $stdout.sync = true
      ::Logger.new($stdout).tap do |logger|
        logger.formatter = proc { |_sev, _dtm, _name, msg| msg + "\n" }
        logger.level = ::Logger::INFO
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
trema-0.9.0 lib/trema/logger.rb
trema-0.8.4 lib/trema/logger.rb
trema-0.8.3 lib/trema/logger.rb
trema-0.8.2 lib/trema/logger.rb
trema-0.8.1 lib/trema/logger.rb
trema-0.8.0 lib/trema/logger.rb
trema-0.7.1 lib/trema/logger.rb
trema-0.7.0 lib/trema/logger.rb
trema-0.6.0 lib/trema/logger.rb
trema-0.5.1 lib/trema/logger.rb
trema-0.5.0 lib/trema/logger.rb
trema-0.4.8 lib/trema/logger.rb