I just spent the better part of 3 hours trying to get my Rails application logging with Log4j. I've finally got it working, but I'm not sure if what I did is correct. I tried various methods to no avail until my very last attempt. So I'm really looking for some validation here, perhaps some pointers and tips as well -- anything would be appreciated to be honest. I've summarized all my feeble methods into three attempts below. I'm hoping for some enlightenment on where I went wrong with each attempt -- even if it means I get ripped up.
Thanks for the help in advance!
I basically followed the guide on the Apache Tomcat website here. The steps are:
log4j.properties
file in $CATALINA_HOME/lib
log4j-x.y.z.jar
into $CATALINA_HOME/lib
$CATALINA_HOME/bin/tomcat-juli.jar
with the tomcat-juli.jar
from the Apache Tomcat Extras foldertomcat-juli-adapters.jar
from the Apache Tomcat Extras folder into $CATALINA_HOME/lib
$CATALINA_BASE/conf/logging.properties
I should have seen a tomcat.log
file in my $CATALINA_BASE/logs
folder.
jakarta_service_20101231.log
stderr_20101231.log
stdout_20101231.log
tomcat.log
file?Modified $CATALINA_BASE/conf/logging.properties
by doing the following:
handlers
line: 5rails3.org.apache.juli.FileHandler
Adding Handler specific properties
5rails3.org.apache.juli.FileHandler.level = FINE
5rails3.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
5rails3.org.apache.juli.FileHandler.prefix = rails3.
Adding Facility specific properties
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].handlers = 4host-manager.org.apache.juli.FileHandler
Modified my web.xml
by adding the following context parameter as per the Logging section of the jruby-rack README (I also modified my warbler.rb accordingly, but I opted to change the web.xml
directly to test things faster).
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>commons_logging</param-value>
</context-param>
Restarted Tomcat
rails3.log
), however there was no log information in the file.I decided to give Log4j another whirl with this new web.xml
setting.
log4j.jar
into my WEB-INF/lib
folderCreated a log4j.properties
file and put it into WEB-INF/classes
log4j.rootLogger=INFO, R
log4j.logger.javax.servlet=DEBUG
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/rails3.log
log4j.appender.R.MaxFileSize=5036KB
log4j.appender.R.MaxBackupIndex=4
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n
Restarted Tomcat
Same as Attempt 2
NOTE: I used log4j.logger.javax.servlet=DEBUG
because I read in the jruby-rack README that all logging output is automatically redirected to the javax.servlet.ServletContext#log
method. So I thought this would capture it, but I was obviously wrong.
commons_logging
API?A bit uncertain as to why Attempt 2A didn't work, I thought to myself, maybe I can't use commons_logging
for the jruby.rack.logging
parameter because it's probably not using commons_logging
API... (but I was still not sure). I saw slf4j
as an option. I have never heard of it and out of curiosity, I decided to look it up. After reading about it briefly, I thought it was as good of a shot as any and decided to try it out following the instructions here.
Continuing from the setup of Attempt 2A:
slf4j-api-1.6.1.jar
and slf4j-simple-1.6.1.jar
into my WEB-INF/lib
folderslf4j-log4j12-1.6.1.jar
into my WEB-INF/lib
folder And VIOLA! I now have logging information going into my rails3.log file.
So the big question is:
Even though logging seems to be working now, I'm really not sure if what I did is right. So like I said earlier, I'm really looking for some validation more or less. I'd also appreciate any pointers/tips/advice if you have any. Thanks!
We also did various experiments and finally settled with slf4j option. Coming from Java background we knew slf4j, so we didn't go further.
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>slf4j</param-value>
</context-param>
btw, there is no need to copy slf4j-simple-1.6.1.jar into tomcat/lib or WEB-INF/lib when using slf4j-log4jxxx.jar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With