Java tomcat中的Log4j不显示日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20281450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:49:16  来源:igfitidea点击:

Log4j in tomcat not showing logs

javatomcatlogging

提问by Bilbo Baggins

This is my log4j.properties.

这是我的log4j.properties

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}\MyLog\PmcDemo.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I am using tomcat 6.0, in my application I have used Logger from log4j yet I don't see any output on server console or in the log file. My application is using struts2 as front end, Spring framework as middle layer and hibernate as the end layer. I don't see my application logging how can I enable it in tomcat 6?

我正在使用 tomcat 6.0,在我的应用程序中,我使用了 log4j 中的 Logger,但在服务器控制台或日志文件中没有看到任何输出。我的应用程序使用 struts2 作为前端,Spring 框架作为中间层,hibernate 作为端层。我没有看到我的应用程序日志记录如何在 tomcat 6 中启用它?

采纳答案by cowls

You need to switch the backslashes for forward slashes:

您需要将反斜杠切换为正斜杠:

${catalina.home}/MyLog/PmcDemo.log

or to escape them

或逃避他们

${catalina.home}\MyLog\PmcDemo.log

If that doesn't help, let us know the structure of your project and where the log4j.properties file is stored.

如果这没有帮助,请告诉我们您项目的结构以及 log4j.properties 文件的存储位置。

回答by Vipul Paralikar

Try this steps,

试试这个步骤,

If running Tomcat 6.x:

如果运行 Tomcat 6.x:

 1. If you have not already done so, modify the <<TOMCAT_HOME>>/conf/catalina.properties file so that the shared classloader mechanism work the same as Tomcat 5.x.
 2. To do this, verify that the entry beginning with shared.loader= reads shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar If running Tomcat 5.x or higher:

If running Tomcat 5.x or higher:

如果运行 Tomcat 5.x 或更高版本:

 3. If it does not already exist, create a "shared/classes" directory under <<TOMCAT_HOME>>.

 4. If it does not already exist, create a "shared/lib" directory under <<TOMCAT_HOME>>.
 5. Copy log4j-###.jar into <<TOMCAT_HOME>>/shared/lib.

    **Note:** Any specific version of log4j-###.jar should work. You can download the stable log4j version 1.2 installation from http://logging.apache.org/log4j/1.2/download.html

 6. Copy a log4j.properties file into <<TOMCAT>>/shared/classes.

Example
To get a log file named "initiate.log" to show up in the <<TOMCAT_HOME>>/logsdirectory, an initial version of log4j.properties file is:

示例
要在<<TOMCAT_HOME>>/logs目录中显示名为“initiate.log”的日志文件,log4j.properties 文件的初始版本为:

    log4j.rootLogger=ERROR, R

    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=${catalina.home}/logs/initiate.log

    log4j.appender.R.MaxFileSize=1000KB
    log4j.appender.R.MaxBackupIndex=5

    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} 5-5p %c{2} - %m %n

    log4j.logger.org.springframework=ERROR
    log4j.logger.org.springframework.web.servlet.mvc=ERROR

    #set to DEBUG to see hibernate and connection-pool logging output
    log4j.logger.org.hibernate=ERROR
    log4j.logger.org.apache.commons.dbcp=ERROR

    #set to DEBUG to see Initiate-specific logging output
    log4j.logger.com.initiatesystems=DEBUG

    #set to DEBUG to set Initiate-specific verbose logging output
    log4j.logger.verbose=ERROR

Quoted from: http://pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.datatrust.doc%2Ftopics%2Ft_datatrust_configuring_log4j_logging_apachetomcat.html

引自:http: //pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.datatrust.doc%2Ftopics% 2Ft_datatrust_configuring_log4j_logging_apachetomcat.html