Sha256: 60b0e9f9210bb0ed75a759777649fbb325d2a65a05ddc9ec4425cce6b2e37065

Contents?: true

Size: 914 Bytes

Versions: 1

Compression:

Stored size: 914 Bytes

Contents

require 'java'
module Slf4r
  class LoggerFacade

    protected

    def format(exception)
      (": #{exception.message}:\n\t#{exception.backtrace.join("\n\t") if exception.backtrace }") if exception
    end

    public

    attr_reader :name

    attr_reader :logger
    
    def initialize(name)
      @name = name
      @logger = org.slf4j.LoggerFactory.getLogger(name.to_s.sub(/^::/, '').gsub(/::/, '.'))
    end

    [:debug, :info, :warn, :error].each do |level|
      class_eval <<-CODE
    def #{level}?
      @logger.is_#{level}_enabled
    end

    def #{level}(msg = nil, exception = nil)
      if(@logger.is_#{level}_enabled)
        msg, exception = yield if block_given?        
        if(exception.type == NativeException)
          @logger.#{level}(msg, exception.cause)
        else
          @logger.#{level}("\#{msg}\#{format(exception)}")
        end
      end
    end
CODE
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slf4r-0.3.0 lib/slf4r/java_logger.rb