我的代码工作正常,如果我右键单击服务器并在服务器上运行运行,但当我尝试在tomcat上部署并尝试运行它时,它给我以下错误
log4j.properties文件:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,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
错误:
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
pom.xml:
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
答案 0 :(得分:12)
问题在于以下两行:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,stdout
您只能指定一个记录器级别作为第一个参数(在本例中为INFO),然后接下来的两个参数将被视为用于该记录器的appender名称(在本例中为error和stdout)。由于不存在名为error的appender,因此您收到了您已报告的错误。
检查出来(https://logging.apache.org/log4j/1.2/manual.html),其中显示了一个将记录器分配给两个看起来很像代码的不同appender的示例。希望这有助于解释为什么log4j在您的应用程序中寻找名为error的appender。
这是另一个使用多个appender的配置文件。
log4j.rootLogger = debug,stdout,R
log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t](%F:%L) - %m%n
log4j.appender.R = org.apache.log4j.RollingFileAppender进行 log4j.appender.R.File = example.log
log4j.appender.R.MaxFileSize = 100KB
log4j.appender.R.layout = org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern =%p%t%c - %m%n