Skip to content
forked from lenny/log4jruby

Ruby Logger interface for log4j. Use log4j directly from JRuby. Configure logging for your ruby classes with your java.

License

Notifications You must be signed in to change notification settings

olnrao/log4jruby

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

Features

  • 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")
  bar
rescue => e
  logger.error(e)
end

DEBUG jruby.MyClass foo:17 - hello from foo
DEBUG jruby.MyClass bar:24 - hello from bar
DEBUG jruby.MyClass baz:29 - hello from baz
ERROR jruby.MyClass foo:20 - error from baz
  examples/simple.rb:30:in `baz'
  examples/simple.rb:25:in `bar'
  examples/simple.rb:18:in `foo'
  examples/simple.rb:35:in `(root)'

See more in log4jruby/examples.

Installation

gem install log4jruby

Usage

class MyClass
  enable_logger

  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 jruby.MyModule.A my_class_method:14 - hello from class method
INFO jruby.MyModule.A my_method:19 - hello from instance method

See more in log4jruby/examples. They should be runnable from the source.

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

About

Ruby Logger interface for log4j. Use log4j directly from JRuby. Configure logging for your ruby classes with your java.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 98.5%
  • Java 1.5%