Logging in from log4j to tomcat jruby-rack for Rails 3 application

I spent most of 3 hours trying to start registering Rails applications using Log4j. I finally got his job, but I'm not sure what I did is right. I tried different methods without any benefit until the very last attempt. So I'm really looking for some confirmation here, maybe some pointers and tips - everything will be appreciated fairly. I have summarized all my weak methods on three attempts below. I hope for some enlightenment about where I did wrong with every attempt - even if it means I'm torn.

Thanks for the help in advance!

System characteristics

  • Rails 3.0
  • Windows Server 2008
  • Log4j 1.2
  • Tomact 6.0.29
  • Java 6

Attempt 1 - Configured Tomcat to use Log4J

I basically followed the tutorial on the Apache Tomcat website here . Steps:

  • Create a log4j.properties file in $CATALINA_HOME/lib
  • Download and copy log4j-xyzjar to $CATALINA_HOME/lib
  • Replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from the Apache Tomcat Extras folder
  • Copy tomcat-juli-adapters.jar from the Apache Tomcat Extras folder to $CATALINA_HOME/lib
  • Delete $CATALINA_BASE/conf/logging.properties
  • Launch Tomcat (as a service)

Expected Results According to Management

I should have seen the tomcat.log file in my $CATALINA_BASE/logs folder.

Actual Results

  • No tomcat.log
  • Instead, select three standard magazines
    • jakarta_service_20101231.log
    • stderr_20101231.log
    • stdout_20101231.log

Question

  • Should I at least see the tomcat.log file?

Attempt 2 - Use Tomcat default logging (recording in normal mode)

  • Reverted all changes to the previous setting
  • Changed $CATALINA_BASE/conf/logging.properties by doing the following:

    • Adding a parameter for my application to the handlers line: 5rails3.org.apache.juli.FileHandler
    • Adding custom handler 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 special object 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 according to the jruby-rack README Log section (I also changed my warbler.rb, but I decided to change web.xml directly to test things faster).

     <context-param> <param-name>jruby.rack.logging</param-name> <param-value>commons_logging</param-value> </context-param> 
  • Restart Tomcat

results

  • A log file ( rails3.log ) was created, but there was no log in the file.

Attempt 2A - Use Log4j with existing setup

I decided to give Log4j another whirlwind with this new web.xml parameter.

  • I copied log4j.jar to the WEB-INF/lib folder
  • Created a log4j.properties file and placed it in 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 
  • Restart Tomcat

results

Same as attempt 2

NOTE: I used log4j.logger.javax.servlet=DEBUG because I read in jruby-rack README that all log output is automatically redirected to the javax.servlet.ServletContext#log method. So I thought it would take him, but I was obviously mistaken.

Question

  • Why didn't it work?
  • Does Log4J work with commons_logging API?

Attempt 3 - Tried slf4j (WORKED)

A slightly vague question about why the 2A attempt does not work, I thought to myself, maybe I can’t use commons_logging for the jruby.rack.logging parameter because it probably doesn’t use the commons_logging API ... (but I'm still not was sure). I saw slf4j as an option. I never heard of this and out of curiosity, I decided to watch it. After reading this briefly, I thought it was as good as everyone, and decided to try it in accordance with the instructions here .

Continuing the installation of attempt 2A:

  • Copied slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar to the WEB-INF/lib folder
  • I also copied slf4j-log4j12-1.6.1.jar to the WEB-INF/lib folder
  • Restarted Tomcat

And VIOLA ! Now I am writing information to the rails3.log file.

So the big question is:

WTF?

Even though registration now works, I'm really not sure if I did the right thing. So, as I said earlier, I really was looking for some validation more or less. I would also appreciate any guidance / advice / recommendations if you have any. Thanks!

+7
source share
2 answers

We also conducted various experiments and finally installed the slf4j variant. Based on the Java background, we knew slf4j, so we did not go any further.

 <context-param> <param-name>jruby.rack.logging</param-name> <param-value>slf4j</param-value> </context-param> 

btw, no need to copy slf4j-simple-1.6.1.jar to tomcat / lib or WEB-INF / lib when using slf4j-log4jxxx.jar

+2
source

It is possible that your early Commons logging tests showed files with zero byte due to bufferSize.

If your records get to the file when you stop Tomcat, then this is true, and you drop the default 8Kb buffer.

To shut off, try rinsing immediately ...

org.apache.juli.FileHandler.bufferSize = -1

on a specific handler ...

So for localhost ...

2localhost.org.apache.juli.FileHandler.bufferSize = -1

0
source

All Articles