Thursday, September 24, 2009

Configure Log4j with JRuby on Rails for JBoss

Its very easy to use any Java library from you JRuby code. In one of my JRuby on Rails project I had to use two different logger for logging purpose. One is the Rails default logger for all jruby code and other is log4j logger that is for my java codes and libraries. To use both of the logger I made following changes.

At first I added mylog4j.properties and log4j-1.2.15.jar in my project's lib folder. By default the ruby log file goes to log folder. If we want to keep this log file in this default folder then we don't need to change anything. A sample mylog4j.properties file content is given here:

log4j.rootLogger=DEBUG, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%d{ISO8601}] %-5p (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/opt/jboss/server/default/log/myapp_rails.log


log4j.appender.R.MaxFileSize=10000KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}] %-5p (%F:%L) - %m%n

log4j.logger.org.apache.commons.digester=WARN
log4j.logger.org.apache.commons.beanutils=WARN
log4j.logger.org.apache.commons.validator=WARN
log4j.logger.org.apache.commons.httpclient=DEBUG

log4j.logger.org.springframework=DEBUG

In my case I developed a JRuby on Rails application that was deployed in JBoss server. Thats why I decided not to use the default log folder of Rails application for log files rather use the JBoss log folder to store the log files.
For that reason I need to know the log folder path of JBoss in my environment.rb file and then change the log file path that will be used by Rails. You can get the JBoss log folder path by:

java.lang.System.getProperty('jboss.server.log.dir')

So the corresponding changes in environment.rb file that I made are:

jboss_server_log_dir = java.lang.System.getProperty('jboss.server.log.dir')
if jboss_server_log_dir
config.log_path = File.join(jboss_server_log_dir,'frengine_admin.log') if (jboss_server_log_dir && !jboss_server_log_dir.strip.empty?)
end

config.logger = Logger.new(config.log_path, 50, 1048576)

And to specify the same log folder in the mylog4j.properties file I used following line:

log4j.appender.R.File=${jboss.server.log.dir}/myapp_java.log

Now you will be able to see two different log file in the JBoss log folder. If you want to use same log file for all kind of logging use single file name.

1 comment:

Bradley said...

I have a question. I am running jruby/rails locally with an ESB (mule)

By default, I see Mule logs coming from log4j in my stdout of the webrick server logs. If I change config.log_path, it redirects the rails logs to the new file, but I still see my log4j logs in the normal stdout.

I wasn't able to adjust the level of this logging. I don't see how your log4j.properties is actually loaded and I'm wondering how to get mine to be loaded. Just putting it in the lib directory had no affect. It defaults to WARN and I really want it in debug. Any thoughts?