No appenders could be found for logger(log4j)?

前端 未结 30 2265
[愿得一人]
[愿得一人] 2020-11-22 08:16

I have put log4j to my buildpath, but I get the following message when I run my application:

log4j:WARN No appenders could be found for logger (dao.hsqlmanag         


        
相关标签:
30条回答
  • 2020-11-22 09:09

    If you are using Eclipse and this problem appeared out of nowhere after everything worked fine beforehand, try going to Project - Clean - Clean.

    0 讨论(0)
  • 2020-11-22 09:10

    I faced the same problem when I use log4j2. My problem is caused by using wrong dependent library:

    <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <scope>runtime</scope>
        </dependency>
    

    Instead, I should use:

    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <scope>runtime</scope>
        </dependency>
    

    In my case, I have a log4j2.xml defined in my "resources" directory, and specified to use it by:

    System.setProperty("log4j.configurationFile", "log4j2.xml");
    
    0 讨论(0)
  • 2020-11-22 09:10

    Maybe add the relevent project contains log4j in java build path, I add mahout_h2o into it when I met this problem in a mahout project using eclipse, it works!

    0 讨论(0)
  • 2020-11-22 09:11

    You use the Logger in your code to log a message. The Appender is a Object appended to a Logger to write the message to a specific target. There are FileAppender to write to text-files or the ConsoleAppender to write to the Console. You need to show your code of the Logger and Appender setup for more help.

    please read the tutorial for a better understanding of the interaction of Logger and Appender.

    0 讨论(0)
  • 2020-11-22 09:13

    I get the same error. Here the problem which leads to this error message:

    I create some objects which use the Logger before I configure the log4j:

    Logger.getLogger(Lang.class.getName()).debug("Loading language: " + filename);
    

    Solution: Configure the log4j at the beginning in the main method:

    PropertyConfigurator.configure(xmlLog4JConfigFile); 
    // or BasicConfigurator.configure(); if you dont have a config file
    
    0 讨论(0)
  • 2020-11-22 09:14

    Log4J display this warning message when Log4j Java code is searching to create a first log line in your program.

    At this moment, Log4j make 2 things

    1. it search to find log4j.properties file
    2. it search to instantiate the appender define in log4j.properties

    If log4J doesn't find log4j.properties file or if appender declared in log4j.rootlogger are not defined elsewhere in log4j.properties file the warning message is displayed.

    CAUTION: the content of Properties file must be correct.

    The following content is NOT correct

    log4j.rootLogger=file
    
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.ConversionPattern=%m%n
    log4j.appender.FILE.ImmediateFlush=true
    log4j.appender.FILE.Threshold=debug
    log4j.appender.FILE.Append=false
    

    because file appender is declared in LOWER-CASE in log4j.rootlogger statement and defined in log4j.appender statement using UPPER-CASE !

    A correct file would be

    log4j.rootLogger=FILE
    
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.ConversionPattern=%m%n
    log4j.appender.FILE.ImmediateFlush=true
    log4j.appender.FILE.Threshold=debug
    log4j.appender.FILE.Append=false
    

    If MAVEN is used, you must put log4j.properties files in src/main/resources AND start a MAVEN build.

    Log4j.properties file is then copied in target/classes folder.

    Log4J use the log4j.properties file that it found in target/classes !

    0 讨论(0)
提交回复
热议问题