Class: Telekinesis::Logging::RubyLoggerHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/telekinesis/logging/ruby_logger_handler.rb

Overview

A java logging Handler that delegates to a Ruby logger. The name of the j.u.l. logger is used as the progname argument to Logger.add.

The translation between j.u.l. serverity levels and Ruby Logger levels isn’t exact.

Constant Summary collapse

SEVERITY =
{
  # NOTE: There's no Java equivalent of FATAL.
  Level::SEVERE => Logger::ERROR,
  Level::WARNING => Logger::WARN,
  Level::INFO => Logger::INFO,
  Level::CONFIG => Logger::INFO,
  Level::FINE=> Logger::DEBUG,
  Level::FINER=> Logger::DEBUG,
  Level::FINEST=> Logger::DEBUG,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(logger) ⇒ Object

NOTE: Since this class overrides a Java class, we have to use the Java constructor and set the logger after instantiation. (Overriding in JRuby is weird). Use this method to create a new logger that delegates to the passed logger.



16
17
18
19
20
# File 'lib/telekinesis/logging/ruby_logger_handler.rb', line 16

def self.create(logger)
  new.tap do |s|
    s.set_logger(logger)
  end
end

Instance Method Details

#closeObject



37
38
39
# File 'lib/telekinesis/logging/ruby_logger_handler.rb', line 37

def close
  @logger.close
end

#flushObject

Ruby’s logger has no flush method.



42
# File 'lib/telekinesis/logging/ruby_logger_handler.rb', line 42

def flush; end

#publish(log_record) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/telekinesis/logging/ruby_logger_handler.rb', line 44

def publish(log_record)
  message = if log_record.thrown.nil?
    log_record.message
  else
    "#{log_record.message}: #{log_record.thrown}"
  end
  @logger.add(SEVERITY[log_record.level], message, log_record.logger_name)
end

#set_logger(l) ⇒ Object



33
34
35
# File 'lib/telekinesis/logging/ruby_logger_handler.rb', line 33

def set_logger(l)
  @logger = l
end