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

查看:80
本文介绍了在Rails 3应用程序的tomcat jruby-rack上使用log4j进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是花了3个小时的大部分时间来尝试用Log4j记录我的Rails应用程序.我终于让它工作了,但是我不确定我所做的是否正确.我尝试了各种方法,直到最后一次尝试都无济于事.所以我真的在这里寻找一些验证,也许还有一些指示和技巧-老实说,任何事情都将受到赞赏.我将我所有微弱的方法总结为以下三种尝试.我希望能对我每次尝试出错的地方有所启发-即使这意味着我被撕裂了.

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.

感谢您的提前帮助!

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

我基本上遵循了Apache Tomcat网站此处上的指南>.这些步骤是:

I basically followed the guide on the Apache Tomcat website here. The steps are:

  1. $CATALINA_HOME/lib
  2. 中创建一个log4j.properties文件
  3. 下载log4j-x.y.z.jar并将其复制到$CATALINA_HOME/lib
  4. 用Apache Tomcat Extras文件夹中的tomcat-juli.jar替换$CATALINA_HOME/bin/tomcat-juli.jar
  5. tomcat-juli-adapters.jar从Apache Tomcat Extras文件夹复制到$CATALINA_HOME/lib
  6. 删除$CATALINA_BASE/conf/logging.properties
  7. 启动Tomcat(作为服务)
  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)

根据指南的预期结果

我应该在$CATALINA_BASE/logs文件夹中看到一个tomcat.log文件.

Expected Results According to the Guide

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

  • 没有tomcat.log
  • 改为看到三个 standard 日志
    • jakarta_service_20101231.log
    • stderr_20101231.log
    • stdout_20101231.log
    • No tomcat.log
    • Saw three of the standard logs instead
      • jakarta_service_20101231.log
      • stderr_20101231.log
      • stdout_20101231.log
      • 我至少没有看过tomcat.log文件吗?
      • Shouldn't I have at least seen a tomcat.log file?
      1. 还原了先前设置中的所有更改
      2. 通过执行以下操作修改$CATALINA_BASE/conf/logging.properties:

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

      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.
      

    • 添加设施专用属性

    • 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
      

    • 通过根据jruby-rack README的Logging部分添加以下上下文参数来修改我的web.xml(我也相应地修改了warbler.rb,但我选择直接更改web.xml进行测试速度更快).

    • 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>
      

    • 重新启动Tomcat

    • Restarted Tomcat

      结果

      • 已创建日志文件(rails3.log),但是该文件中没有日志信息.
      • Results

        • A log file was created (rails3.log), however there was no log information in the file.
        • 我决定通过此新的web.xml设置再次给Log4j发动.

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

          1. log4j.jar复制到我的WEB-INF/lib文件夹中
          2. 创建了一个log4j.properties文件并将其放入WEB-INF/classes

          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
          

        • 重新启动Tomcat

        • Restarted Tomcat

          结果

          与尝试2相同

          Results

          Same as Attempt 2

          注意::之所以使用log4j.logger.javax.servlet=DEBUG,是因为我在jruby-rack README中读到,所有日志记录输出都会自动重定向到javax.servlet.ServletContext#log方法.所以我以为这会抓住它,但是我显然是错的.

          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.

          1. 为什么这个没用?
          2. 不是使用commons_logging API的Log4J吗?
          1. Why didn't this work?
          2. Isn't Log4J using the commons_logging API?

          尝试3-尝试使用slf4j(已完成)

          对于为什么尝试2A不起作用有些不确定,我对自己说,也许我不能对jruby.rack.logging参数使用commons_logging,因为它可能未使用commons_logging API ...(但我仍然不确定).我看到slf4j作为选项.我从未听说过它,出于好奇,我决定查找它.简要阅读了一下之后,我认为这是一个不错的选择,并决定按照这里.

          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.

          继续尝试2A的设置:

          1. slf4j-api-1.6.1.jarslf4j-simple-1.6.1.jar复制到我的WEB-INF/lib文件夹中
          2. 我也将slf4j-log4j12-1.6.1.jar复制到了WEB-INF/lib文件夹
          3. 重新启动Tomcat
          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

          还有 VIOLA !现在,我的 rails3.log 文件中有日志记录信息.

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

          所以最大的问题是:

          即使日志记录现在似乎可以正常工作,但我真的不确定我所做的是否正确.因此,就像我之前说的,我确实或多或少地在寻找一些验证.如果您有任何指点/技巧/建议,我也将不胜感激.谢谢!

          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!

          推荐答案

          我们还进行了各种实验,最后使用slf4j选项解决了.来自Java背景,我们知道slf4j,因此我们走得更远.

          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>
          

          顺便说一句,使用slf4j-log4jxxx.jar时,无需将slf4j-simple-1.6.1.jar复制到tomcat/lib或WEB-INF/lib

          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

          这篇关于在Rails 3应用程序的tomcat jruby-rack上使用log4j进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆