Class: Console::JavaUtilLogger::RubyFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/color_console/java_util_logger.rb

Overview

Extends java.util.logging.Formatter, adding the ability to customise the log format at runtime, and defaulting to a simpler single-line format more suitable for output to the console.

Constant Summary collapse

DEFAULT_FORMAT =

Default format pattern

'%4$-6s  %7$s%5$s'
LINE_END =

System line-ending

java.lang.String.format('%n')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = DEFAULT_FORMAT, width = nil) ⇒ RubyFormatter

Constructs a new formatter for formatting log records according to a format string.

Parameters:

  • format (defaults to: DEFAULT_FORMAT)

    The format string to use when building a String for logging.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/color_console/java_util_logger.rb', line 82

def initialize(format = DEFAULT_FORMAT, width = nil)
    super()
    @format_string = format
    @width = width || Console.width
    mark = java.lang.String.format(@format_string, Time.now,
                                   '', '', '', '!$!', nil, nil)
    @indent = mark.lines.first.index('!$!')
    @level_labels = Hash.new{ |h, k| h[k] = k }
    @level_labels[JavaUtilLogger::Level::WARNING] = 'WARN'
    @level_labels[JavaUtilLogger::Level::SEVERE] = 'ERROR'
    @level_labels[JavaUtilLogger::Level::FINEST] = 'DEBUG'
end

Instance Attribute Details

#format_stringObject

A format string to use when formatting a log record.

See Also:

  • function String.format for the format string syntax. The values passed by this formatter to String.format are: - millis The time the log event occurred - source The name of the logger that logged the record - logger_name The name of the logger that logged the record - level The level of the message - message The log message - thrown Any exception that forms part of the log record - spacer A spacer that will consist of 2 spaces if the log level is config or greater.


68
69
70
# File 'lib/color_console/java_util_logger.rb', line 68

def format_string
  @format_string
end

#indentObject

Amount by which to indent lines



72
73
74
# File 'lib/color_console/java_util_logger.rb', line 72

def indent
  @indent
end

#level_labelsObject (readonly)

Level labels



74
75
76
# File 'lib/color_console/java_util_logger.rb', line 74

def level_labels
  @level_labels
end

#widthObject

Width at which to split lines



70
71
72
# File 'lib/color_console/java_util_logger.rb', line 70

def width
  @width
end

Instance Method Details

#format(log_record) ⇒ Object

Format a log record and return a string for publishing by a log handler.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/color_console/java_util_logger.rb', line 97

def format(log_record)
    lvl = @level_labels[log_record.level]
    indent = @indent || 0
    spacer = ''
    wrap_width = @width - indent
    if log_record.level.intValue < JavaUtilLogger::Level::INFO.intValue
        spacer = '  '
        wrap_width -= 2
    end

    msg = wrap_width > 0 ? Console.wrap_text(log_record.message, wrap_width) :
        [log_record.message]
    sb = java.lang.StringBuilder.new()
    msg.each_with_index do |line, i|
        if i == 0
            fmt = java.lang.String.format(@format_string,
                                          log_record.millis,
                                          log_record.logger_name,
                                          log_record.logger_name,
                                          lvl,
                                          msg[i],
                                          log_record.thrown,
                                          spacer)
        else
            fmt = java.lang.String.format(@format_string,
                                          log_record.millis, '', '', '', msg[i], nil, spacer)
        end
        sb.append(fmt)
        sb.append(LINE_END) if @width < 0 || fmt.length < @width
    end
    sb.toString()
end