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.
-
#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.
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_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 |
#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.
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., wrap_width) : [log_record.] 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 |