在Rails 3应用程序的tomcat jruby-rack上使用log4j进行日志记录 [英] Logging with log4j on tomcat jruby-rack for a Rails 3 application
问题描述
我只是花了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:
- 在
$CATALINA_HOME/lib
中创建一个 - 下载
log4j-x.y.z.jar
并将其复制到$CATALINA_HOME/lib
- 用Apache Tomcat Extras文件夹中的
tomcat-juli.jar
替换$CATALINA_HOME/bin/tomcat-juli.jar
- 将
tomcat-juli-adapters.jar
从Apache Tomcat Extras文件夹复制到$CATALINA_HOME/lib
- 删除
$CATALINA_BASE/conf/logging.properties
- 启动Tomcat(作为服务)
log4j.properties
文件
- Create a
log4j.properties
file in$CATALINA_HOME/lib
- Download and copy the
log4j-x.y.z.jar
into$CATALINA_HOME/lib
- Replace
$CATALINA_HOME/bin/tomcat-juli.jar
with thetomcat-juli.jar
from the Apache Tomcat Extras folder - Copy
tomcat-juli-adapters.jar
from the Apache Tomcat Extras folder into$CATALINA_HOME/lib
- Delete
$CATALINA_BASE/conf/logging.properties
- 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?
- 还原了先前设置中的所有更改
-
通过执行以下操作修改
$CATALINA_BASE/conf/logging.properties
:
- 在
handlers
行中为我的应用程序添加设置:5rails3.org.apache.juli.FileHandler
-
添加处理程序特定的属性
- Adding a setting for my application in the
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
-
-
通过根据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 theweb.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
),但是该文件中没有日志信息. - A log file was created (
rails3.log
), however there was no log information in the file. - 将
log4j.jar
复制到我的WEB-INF/lib
文件夹中 -
创建了一个
log4j.properties
文件并将其放入WEB-INF/classes
- Copied the
log4j.jar
into myWEB-INF/lib
folder Created a
log4j.properties
file and put it intoWEB-INF/classes
Results
我决定通过此新的
web.xml
设置再次给Log4j发动.I decided to give Log4j another whirl with this new
web.xml
setting.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 thejavax.servlet.ServletContext#log
method. So I thought this would capture it, but I was obviously wrong.- 为什么这个没用?
- 不是使用
commons_logging
API的Log4J吗?
- Why didn't this work?
- 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 thejruby.rack.logging
parameter because it's probably not usingcommons_logging
API... (but I was still not sure). I sawslf4j
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的设置:
- 将
slf4j-api-1.6.1.jar
和slf4j-simple-1.6.1.jar
复制到我的WEB-INF/lib
文件夹中 - 我也将
slf4j-log4j12-1.6.1.jar
复制到了WEB-INF/lib
文件夹 - 重新启动Tomcat
- Copied
slf4j-api-1.6.1.jar
andslf4j-simple-1.6.1.jar
into myWEB-INF/lib
folder - I also copied
slf4j-log4j12-1.6.1.jar
into myWEB-INF/lib
folder - 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屋!