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 =
'%4$-6s %5$s%n'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = DEFAULT_FORMAT) ⇒ 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.



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

def initialize(format = DEFAULT_FORMAT)
    super()
    @format_string = format
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.


63
64
65
# File 'lib/color_console/java_util_logger.rb', line 63

def format_string
  @format_string
end

Instance Method Details

#format(log_record) ⇒ Object

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



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

def format(log_record)
    lvl = case log_record.level
          when JavaUtilLogger::Level::WARNING then 'WARN'
          when JavaUtilLogger::Level::SEVERE then 'ERROR'
          when JavaUtilLogger::Level::FINEST then 'DEBUG'
          else log_record.level
          end
    java.lang.String.format(@format_string,
                            log_record.millis,
                            log_record.logger_name,
                            log_record.logger_name,
                            lvl,
                            log_record.message,
                            log_record.thrown)
end