在Tomcat上用log4j对Rails 3应用程序进行日志记录的jruby-rack[英] Logging with log4j on tomcat jruby-rack for a Rails 3 application

本文是小编为大家收集整理的关于在Tomcat上用log4j对Rails 3应用程序进行日志记录的jruby-rack的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只是花了3个小时的大部分时间尝试使用Log4J记录我的Rails应用程序.我终于使它起作用了,但是我不确定我的所作所为是否正确.我尝试了各种方法,直到最后一次尝试.因此,我真的在这里寻找一些验证,也许还有一些指示和技巧 - 老实说,任何事情都将不胜感激.我将所有微弱的方法总结为以下三个尝试.我希望每次尝试都在哪里出错,即使这意味着我被撕毁了.

感谢提前的帮助!

系统规格

  • Rails 3.0
  • Windows Server 2008
  • log4j 1.2
  • tomact 6.0.29
  • Java 6

尝试1-配置tomcat以使用log4j

我基本上遵循了Apache Tomcat网站上的指南在这里.步骤是:

  1. 在$CATALINA_HOME/lib
  2. 中创建log4j.properties文件
  3. 下载并将log4j-x.y.z.jar复制到$CATALINA_HOME/lib
  4. apache tomcat Extras文件夹替换$CATALINA_HOME/bin/tomcat-juli.jar tomcat-juli.jar
  5. 复制tomcat-juli-adapters.jar从Apache Tomcat Extras文件夹中$CATALINA_HOME/lib
  6. delete $CATALINA_BASE/conf/logging.properties
  7. 开始Tomcat(作为服务)

根据指南的预期结果

我应该在我的tomcat.log文件夹中看到tomcat.log文件.

实际结果

  • 否tomcat.log
  • 看到了三个标准日志
    • jakarta_service_20101231.log
    • stderr_20101231.log
    • stdout_20101231.log

问题

  • 我至少应该看过tomcat.log文件吗?

尝试2-使用默认的tomcat记录(Commons -Logging)

  1. 恢复了上一个设置的所有更改
  2. 修改$CATALINA_BASE/conf/logging.properties通过以下操作:

    1. 在handlers行中为我的应用程序添加设置:5rails3.org.apache.juli.FileHandler
    2. 添加处理程序特定属性

      5rails3.org.apache.juli.FileHandler.level = FINE
      5rails3.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
      5rails3.org.apache.juli.FileHandler.prefix = rails3.
      
    3. 添加设施特定属性

      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
      
  3. 根据jruby-rack readme的记录部分添加以下上下文参数修改了我的web.xml(我还相应地修改了我的warbler.rb,但我选择直接更改web.xml以进行测试更快的速度).

    <context-param>
      <param-name>jruby.rack.logging</param-name>
      <param-value>commons_logging</param-value>
    </context-param>
    
  4. 重新启动tomcat

结果

  • 创建了一个日志文件(rails3.log),但是文件中没有日志信息.

尝试2A-使用现有设置

使用log4j

我决定用这个新的web.xml设置给log4j另一个旋转.

  1. 将log4j.jar复制到我的WEB-INF/lib文件夹
  2. 创建log4j.properties文件,然后将其放入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
    
  3. 重新启动tomcat

结果

与尝试2

相同

注意:我使用了log4j.logger.javax.servlet=DEBUG,因为我在jruby-rack readme中读到所有记录输出都会自动重定向到javax.servlet.ServletContext#log方法.所以我认为这会捕获它,但我显然错了.

问题

  1. 为什么不起作用?
  2. log4j不是使用commons_logging api?

尝试3-尝试SLF4J(工作)

关于为什么尝试2a不起作用的原因有些不确定,我想我自己,也许我不能将commons_logging用于jruby.rack.logging参数,因为它可能不使用commons_logging api ...(但是我仍然不确定).我将slf4j视为一种选择.我从未听说过,出于好奇,我决定查找它.经过简短的了解后,我认为它和任何人一样好,并决定按照说明在这里.

继续从尝试的设置2a:

  1. 复制slf4j-api-1.6.1.jar和slf4j-simple-1.6.1.jar进入我的WEB-INF/lib文件夹
  2. 我还将slf4j-log4j12-1.6.1.jar复制到我的WEB-INF/lib文件夹
  3. 重新启动tomcat

和中提琴!我现在有记录信息进入我的 rails3.log 文件.

所以最大的问题是:

wtf?

即使记录似乎现在正在工作,我真的不确定我所做的是否正确.因此,就像我之前说的那样,我真的或多或少都在寻找一些验证.如果有的话,我也要感谢任何指示/提示/建议.谢谢!

推荐答案

我们还进行了各种实验,最后通过SLF4J选项解决.来自Java背景,我们知道SLF4J,所以我们没有走得更远.

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

顺便说一句,使用SLF4J-LOG4JXXX.JAR

时,无需将SLF4J-SIMPLE-1.6.1.JAR复制到tomcat/lib或web-inf/lib中

其他推荐答案

由于BufferSize,您的早期测试可能显示出零字节文件.

如果您停止Tomcat时您的条目被冲入文件,那么确实是这种情况,并且您对默认的8KB缓冲区犯规.

禁用,立即进行冲洗尝试...

org.apache.juli.filehandler.buffersize = -1

在特定处理程序上...

So localhost ...

2localhost.org.apache.juli.filehandler.buffersize = -1

本文地址:https://www.itbaoku.cn/post/1574798.html

问题描述

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!

System Specs

  • 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 guide on the Apache Tomcat website here. The steps are:

  1. Create a log4j.properties file in $CATALINA_HOME/lib
  2. Download and copy the log4j-x.y.z.jar into $CATALINA_HOME/lib
  3. Replace $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from the Apache Tomcat Extras folder
  4. Copy tomcat-juli-adapters.jar from the Apache Tomcat Extras folder into $CATALINA_HOME/lib
  5. Delete $CATALINA_BASE/conf/logging.properties
  6. Start Tomcat (as a service)

Expected Results According to the Guide

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

Actual Results

  • No tomcat.log
  • Saw three of the standard logs instead
    • jakarta_service_20101231.log
    • stderr_20101231.log
    • stdout_20101231.log

Question

  • Shouldn't I have at least seen a tomcat.log file?

Attempt 2 - Use default Tomcat logging (commons-logging)

  1. Reverted all the changes from the previous setup
  2. Modified $CATALINA_BASE/conf/logging.properties by doing the following:

    1. Adding a setting for my application in the handlers line: 5rails3.org.apache.juli.FileHandler
    2. 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.
      
    3. 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
      
  3. 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>
    
  4. Restarted Tomcat

Results

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

Attempt 2A - Use Log4j with existing set up

I decided to give Log4j another whirl with this new web.xml setting.

  1. Copied the log4j.jar into my WEB-INF/lib folder
  2. Created 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
    
  3. Restarted Tomcat

Results

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.

Question

  1. Why didn't this work?
  2. Isn't Log4J using the commons_logging API?

Attempt 3 - Tried out slf4j (WORKED)

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:

  1. Copied slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar into my WEB-INF/lib folder
  2. I also copied slf4j-log4j12-1.6.1.jar into my WEB-INF/lib folder
  3. Restarted Tomcat

And VIOLA! I now have logging information going into my rails3.log file.

So the big question is:

WTF?

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

其他推荐答案

It's possible that your early tests with commons-logging were showing zero bytes files because of bufferSize.

If your entries get flushed to the file when you stop Tomcat then this is indeed the case and you are falling foul of the default 8Kb buffer.

To disable, for immediate flushing try...

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

on the specific handler...

So for localhost...

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