Log4jruby

Description:

Log4jruby is a thin wrapper around the Log4j Logger. It is geared more toward those who are using JRuby to integrate with and build on top of Java code that uses Log4j. The Log4jruby::Logger provides an interface much like the standard ruby Logger. Logging is configured via traditional Log4j methods.

  • Filename, line number, and method name are available(if tracing is on) to your appender layout via MDC.

  • Exceptions can be logged directly and are output with backtraces. Java Exceptions(i.e. NativeExceptions) are logged with full java backtrace(including nested exceptions).

  • Logging config for your ruby code can be added to your existing configuration. Ruby logger names are mapped to dot separated names prefixed with .jruby

    log4j.appender.Ruby=org.apache.log4j.ConsoleAppender
    log4j.appender.Ruby.layout=org.apache.log4j.PatternLayout
    log4j.appender.Ruby.layout.ConversionPattern=%5p %.50X{fileName} %X{methodName}:%X{lineNumber} - %m%n
    
    log4j.logger.jruby=info,Ruby
    log4j.logger.jruby.MyClass=debug
    

Examples

def foo
  logger.debug("hello from foo")
  begin
    bar
  rescue => e
    logger.error(e)
  end
end

DEBUG jruby.MyClass foo:15 - hello from foo
DEBUG jruby.MyClass bar:24 - hello from bar
DEBUG jruby.MyClass baz:29 - hello from baz
ERROR jruby.MyClass foo:19 - error from baz
  examples/simple.rb:30:in `baz'
  examples/simple.rb:25:in `bar'
  examples/simple.rb:17:in `foo'
  examples/simple.rb:35

Mix It In

class MyClass include Log4jruby::LoggerForClass

class << self def my_class_method logger.info(“hello from class method”) end end

def my_method logger.info(“hello from instance method”) end end INFO examples/logger_per_class.rb my_class_method:11 - hello from class method INFO examples/logger_per_class.rb my_method:16 - hello from instance method See more in log4jruby/examples. They should be runnable from the source.

Installation

> gem install log4jruby

or

> git clone git://github.com/lenny/log4jruby.git

Usage

The primary use case(i.e. mine) for this library is that you are already up and running with Log4j and now you want to use it for your Ruby code too. There is not much help here for configuration. In our environment, we deploy Rails apps that call and extend Java code into Tomcat as WARs and use a log4j.properties to configure. For the most part, all there is to using log4jruby is making sure the log4j jar is required and that Log4j is at least minimally configured(e.g. log4j.properties in the CLASSPATH). The examples should give you the idea.

Rails

First try to add the following to one of your config/environment* files.

require 'log4jruby'
config.logger = Log4jruby::Logger.get('MyApp')

Depending on your runtime environment and how it differs between development and production that may not work for you due to CLASSPATH issues. Alternatively you may add the following to one of your config/initializers/* files.

require 'log4jruby/rails'
Log4jruby::Rails.configure do |c|
   c.logger_name = 'MyApp'
end