简体   繁体   中英

log4j doesn't read its log4j.properties file

In a Maven project I've put the log4j.properties file in src\\main\\resources catalog which seems to be the usual place to put it in. However it does not appear to read it or there is a mistake I can't see as my logger doesn't print DEBUG level messages (it does print INFO level messages though) and doesn't create the logs.log file either. Here's the log4j.properties file I'm using:

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

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/.../src/main/resources/META-INF/logs.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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've also tried to pass the path to the log4j.properties file in my run configuration but it didn't work either: -Dlog4j.configuration=file:D:/.../src/main/resources/log4j.properties This project is launched through a different bound project so I used the entire file path which might be incorrect as none of the examples I've seen had full paths provided.

The server we're using to run the app is Wildfly 8.1.0 which has it's own log4j logger so maybe that's interfering somehow?

Here's the part of my interceptor which should create logs:

final static Logger logger = Logger.getLogger(RestInterceptor.class);
...    
if(e instanceof ApplicationException) {
        logger.debug(e.getMessage(), e);
        //TODO remove these when done testing
        logger.debug("debug is working");
        logger.info("info is working");
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

However only "info is working" is being printed.

Are you sure src\\main\\resources is on your buildpath?

Right-click on your project -> Java Build path [Source Tab]

Make sure the resource folder is on the buildpath. Also make sure the Excluded pattern = '**'

Alright so I managed to kind of fix the problem. It was caused by Wildfly's logger interfering with mine. I've added <use-deployment-logging-config value="false"/> line in standalone.xml But it was not enough. After digging some more I've also found and added the following line <add-logging-api-dependencies value="false"/> and it finally started working. While I'm not fully satisfied with this solution I guess it's still better than nothing and maybe it's worth sharing.

So in short, add this in your standalone.xml and it should resolve logging conflicts if you're using Wildfly 8:

<subsystem xmlns="urn:jboss:domain:logging:2.0"> <add-logging-api-dependencies value="false"/> <use-deployment-logging-config value="false"/> ... </subsystem>

update spring version. it works for me.

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