简体   繁体   English

使用Log4j找不到记录器的附加程序

[英]No appenders could be found for logger using Log4j

在此处输入图片说明 I'm running an application locally and trying to get it to output logs to a file I created. 我正在本地运行一个应用程序,并试图将其输出到我创建的文件中。 the log4j.properties file looks like this: log4j.properties文件如下所示:

# Define the root logger with appender file

log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=C:\\Users\\jkm144\\workspace\\LdapPurge\\log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

It's a Maven project. 这是一个Maven项目。 It's defined in my pom.xml and is listed in my Maven dependencies. 它在我的pom.xml中定义,并在我的Maven依赖项中列出。

I've imported three classes: 我已经导入了三个类:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.FileAppender;

and the code I'm using to define the logger is: 我用来定义记录器的代码是:

static Logger log = Logger.getLogger(LdapPurge.class.getName());

Any time I run into a log, it tells me that "log4j:WARN No appenders could be found for logger" 每当我遇到日志时,它都会告诉我“ log4j:WARN找不到记录器的附加程序”

I'm not sure exactly what I'm doing wrong. 我不确定自己在做什么错。 This is my first time using log4j. 这是我第一次使用log4j。

You should place the log4j.properties under the 您应该将log4j.properties放在

WEB-INF/classes WEB-INF /类

directory of your web-applications. Web应用程序的目录。 Log4j will find the properties file and initialize itself. Log4j将找到属性文件并对其进行初始化。 This is easy to do and it works. 这很容易做到并且有效。

Manaully Adding log4j.properties: 手动添加log4j.properties:

you can add it any where you want, when you run your project configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file. 您可以在需要的任何位置添加它,在运行项目时,通过单击以下命令配置类路径并添加log4j.properties文件的位置:运行->运行配置-> [类路径选项卡]->单击用户条目->高级->选择添加文件夹->选择log4j.properties文件的位置。 Click OK 点击确定

Your screenshot suggests this is a Maven project, so put the log4j.properties in src/main/resources . 屏幕快照表明这是一个Maven项目,因此将log4j.properties放在src/main/resources This is the Maven-standard path for resource files that need to end up on the classpath at runtime, and anything you put in there will be packaged into your application's JAR file along with the classes that were compiled from the sources in src/main/java . 这是资源文件的Maven标准路径,需要在运行时最终放在类路径上,您放入其中的所有内容都将与从src/main/java的源编译的类一起打包到应用程序的JAR文件中。 src/main/java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM