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.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/color_console/java_util_logger.rb', line 88

def initialize(format = DEFAULT_FORMAT, width = nil)
    super()
    @format_string = format
    @width = width || Console.width || -1
    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

#msg_procObject

Optional proc for processing formatted msg before line wrapping. If a proc is set on this formatter, it will be called with the log record and message text before the message is formatted. It should return the revised message text to be logged (or nil if the message should be ignored).



80
81
82
# File 'lib/color_console/java_util_logger.rb', line 80

def msg_proc
  @msg_proc
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. If a msg_proc handler has been set on this formatter, it will get passed the log_record for pre-processing before it is formatted by this method.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/color_console/java_util_logger.rb', line 105

def format(log_record)
    msg_txt = self.formatMessage(log_record)
    msg_txt = msg_proc.call(log_record, msg_txt) if msg_proc
    return unless msg_txt

    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(msg_txt, wrap_width) : [msg_txt]
    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