简体   繁体   中英

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:

# 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. It's defined in my pom.xml and is listed in my Maven dependencies.

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"

I'm not sure exactly what I'm doing wrong. This is my first time using log4j.

You should place the log4j.properties under the

WEB-INF/classes

directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works.

Manaully Adding 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. Click OK

Your screenshot suggests this is a Maven project, so put the log4j.properties in 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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