class BatchKit::Logging::StdOutLogger

Attributes

indent[RW]

Amount by which to indent lines

level[RW]

@return [Symbol] The current level at which logging is set

log_file[R]

@return [String] The log file path, if any

name[R]

@return [String] The name of this logger

width[RW]

Width at which to split lines

Public Class Methods

logger(name) click to toggle source
# File lib/batch-kit/logging/stdout_logger.rb, line 7
def self.logger(name)
    @loggers ||= {}
    @loggers[name] ||= self.new(name)
end
new(name, level = :detail) click to toggle source
# File lib/batch-kit/logging/stdout_logger.rb, line 26
def initialize(name, level = :detail)
    @name = name
    @level = level
    @indent = 8
    @width = Console.width if use_console?
end

Public Instance Methods

log_file=(log_path, options = {}) click to toggle source
# File lib/batch-kit/logging/stdout_logger.rb, line 43
def log_file=(log_path, options = {})
    @log_file.close if @log_file
    if log_path
        append = options.fetch(:append, true)
        @log_file = File.new(log_path, append ? 'a' : 'w')
    end
end
log_msg(level, *args) click to toggle source
# File lib/batch-kit/logging/stdout_logger.rb, line 52
def log_msg(level, *args)
    return if LEVELS.index(level) > LEVELS.index(@level)
    lvl = level.to_s.upcase
    msg = args.join(' ')
    spacer = LEVELS.index(level) >= LEVELS.index(:config) ? '  ' : ''
    fmt_msg = "%-6s  %s%s" % [lvl, spacer, msg]
    if use_console?
        color = case level
        when :error then :red
        when :warning then :yellow
        when :info then :white
        when :config then :cyan
        when :detail then :light_gray
        else :dark_gray
        end

        indent = @indent || 0
        indent += 2 if indent > 0 && [:config, :detail, :trace, :debug].include?(level)

        msg = @width ? Console.wrap_text(msg, @width - indent) : [msg]
        msg = msg.each_with_index.map do |line, i|
            "%-6s  %s%s" % [[lvl][i], spacer, line]
        end.join("\n")
        Console.puts msg, color
    else
        STDOUT.puts fmt_msg
    end
    if @log_file
        @log_file.puts Time.now.strftime('[%F %T] ') + fmt_msg
    end
end
use_console?() click to toggle source
# File lib/batch-kit/logging/stdout_logger.rb, line 85
def use_console?
    unless @use_console
        @use_console = defined?(::Console)
    end
    @use_console
end