Class: BatchKit::Logging::StdOutLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/batch-kit/logging/stdout_logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, level = :detail) ⇒ StdOutLogger

Returns a new instance of StdOutLogger.



26
27
28
29
30
31
# 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

Instance Attribute Details

#indentObject

Amount by which to indent lines



23
24
25
# File 'lib/batch-kit/logging/stdout_logger.rb', line 23

def indent
  @indent
end

#levelSymbol

Returns The current level at which logging is set.

Returns:

  • (Symbol)

    The current level at which logging is set



16
17
18
# File 'lib/batch-kit/logging/stdout_logger.rb', line 16

def level
  @level
end

#log_fileString

Returns The log file path, if any.

Returns:

  • (String)

    The log file path, if any



18
19
20
# File 'lib/batch-kit/logging/stdout_logger.rb', line 18

def log_file
  @log_file
end

#nameString (readonly)

Returns The name of this logger.

Returns:

  • (String)

    The name of this logger



14
15
16
# File 'lib/batch-kit/logging/stdout_logger.rb', line 14

def name
  @name
end

#widthObject

Width at which to split lines



21
22
23
# File 'lib/batch-kit/logging/stdout_logger.rb', line 21

def width
  @width
end

Class Method Details

.logger(name) ⇒ Object



7
8
9
10
# File 'lib/batch-kit/logging/stdout_logger.rb', line 7

def self.logger(name)
    @loggers ||= {}
    @loggers[name] ||= self.new(name)
end

Instance Method Details

#log_msg(level, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/batch-kit/logging/stdout_logger.rb', line 85

def use_console?
    unless @use_console
        @use_console = defined?(::Console)
    end
    @use_console
end