Git Product home page Git Product logo

log4jruby's Introduction

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

log4jruby's People

Contributors

lenny avatar olnrao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.