简体   繁体   中英

log4j:WARN No appenders could be found for logger. Log4j 1.2.17 jar

I am getting this error

log4j:WARN No appenders could be found for logger (com.company.Main).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

This is my Main

package com.company;

import org.apache.log4j.Logger;

public class Main {
    final static Logger logger = Logger.getLogger(Main.class);
    public static void main(String[] args) {

        if(logger.isDebugEnabled()){
            logger.debug("This is debug");
        }

        //logs an error message with parameter
        logger.error("This is error");
    }
}

While this is my log.properties file

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

# Redirect log messages to console
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

# Redirect log messages to a log file, support file rolling.
log4j.appender.file                               = org.apache.log4j.RollingFileAppender
log4j.appender.file.File                          = C:\\log4j-application.log
log4j.appender.file.MaxFileSize                   = 5MB
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

and this is my file structure.

I am in learning stage of Log4J and trying to implement a dummy project to understand it deeply.

You have to add your ressources directory to your classpath. After that it should work.

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