Sha256: 5b9bbb51189b2bfc62e08a9db293f48ec8be7f946a60931119f4ce2de068ee73

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module Telekinesis
  module Logging
    java_import java.util.logging.Level
    java_import java.util.logging.Handler

    # 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.
    class RubyLoggerHandler < Handler
      # 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.
      def self.create(logger)
        new.tap do |s|
          s.set_logger(logger)
        end
      end

      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,
      }

      def set_logger(l)
        @logger = l
      end

      def close
        @logger.close
      end

      # Ruby's logger has no flush method.
      def flush; end

      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
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
telekinesis-3.2.1-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-3.2.0-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-3.1.1-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-3.1.0-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-3.0.0-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-2.0.1-java lib/telekinesis/logging/ruby_logger_handler.rb
telekinesis-2.0.0-java lib/telekinesis/logging/ruby_logger_handler.rb