Class: Console::JavaUtilLogger::RubyFormatter
- Inherits:
-
Formatter
- Object
- Formatter
- Console::JavaUtilLogger::RubyFormatter
- 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
-
#format_string ⇒ Object
A format string to use when formatting a log record.
-
#indent ⇒ Object
Amount by which to indent lines.
-
#level_labels ⇒ Object
readonly
Level labels.
-
#msg_proc ⇒ Object
Optional proc for processing formatted msg before line wrapping.
-
#width ⇒ Object
Width at which to split lines.
Instance Method Summary collapse
-
#format(log_record) ⇒ Object
Format a log record and return a string for publishing by a log handler.
-
#initialize(format = DEFAULT_FORMAT, width = nil) ⇒ RubyFormatter
constructor
Constructs a new formatter for formatting log records according to a format string.
Constructor Details
#initialize(format = DEFAULT_FORMAT, width = nil) ⇒ RubyFormatter
Constructs a new formatter for formatting log records according to a format string.
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_string ⇒ Object
A format string to use when formatting a log record.
68 69 70 |
# File 'lib/color_console/java_util_logger.rb', line 68 def format_string @format_string end |
#indent ⇒ Object
Amount by which to indent lines
72 73 74 |
# File 'lib/color_console/java_util_logger.rb', line 72 def indent @indent end |
#level_labels ⇒ Object (readonly)
Level labels
74 75 76 |
# File 'lib/color_console/java_util_logger.rb', line 74 def level_labels @level_labels end |
#msg_proc ⇒ Object
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 |
#width ⇒ Object
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 |