No appenders could be found for logger(log4j)?

前端 未结 30 2263
[愿得一人]
[愿得一人] 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 08:54

    if you work together with a lot of projects you may face a style problem.

    *you have to have one lof4j.properties file and this file is included log properties of other project.

    *Beside you can try to put log4j properties files into src path when the project is worked Linux OS, libs of other project and log4.properties files can be under one folder into a location on the classpath.

    0 讨论(0)
  • 2020-11-22 08:54

    For me, the reason was apparently different, and the error message misleading.

    With just this in my build.gradle, it would complain that slf4j was missing at the beginning of the log, but still log things, albeit in a poor format:

        compile 'log4j:log4j:1.2.17'
    

    Adding that dependency would cause the discussed "no appenders could be found" error message for me, even though I had defined them in src/main/java/log4j.properties:

        compile 'log4j:log4j:1.2.17'
        compile 'org.slf4j:slf4j-log4j12:1.7.25'
    

    Finally, adding the following dependency (which I only guessed at by copycatting it from another project) resolved the issue:

        compile 'log4j:log4j:1.2.17'
        compile 'org.slf4j:slf4j-log4j12:1.7.25'
        compile 'commons-logging:commons-logging:1.2'
    

    I don't know why, but with this it works. Any insights on that?

    0 讨论(0)
  • 2020-11-22 08:55

    Another reason this may happen (in RCP4) is that you are using multiple logging frameworks in your target file. As an example, this will occur if you use a combination of slf4j, log4j and ch.qos.logback.slf4j in your target files content tab.

    0 讨论(0)
  • 2020-11-22 08:55

    The solution on this site worked for me https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/. I now see no warnings at all from log4j

    I put this in a log4j.properties file that I put in src/main/resources

    # This sets the global logging level and specifies the appenders
    log4j.rootLogger=INFO, theConsoleAppender
    
    # settings for the console appender
    log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
    log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
    0 讨论(0)
  • 2020-11-22 08:56

    This is just a warning.

    Fixing

    This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.

    To fix that, simply create/copy log4j.properties or log4j.xml into your a location on the classpath (usually the same as the jar files).

    Optionally set java option: -Dlog4j.configuration=file:///path/to/log4j.properties.

    log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.

    Debugging

    For debugging, you may try to use -Dlog4j.debug=true parameter.

    Configuration of log4j.properties

    Sample configuration of log4j.properties:

    # Set root logger level to DEBUG and its only appender to A1.
    log4j.rootLogger=DEBUG, A1
    
    # A1 is set to be a ConsoleAppender.
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    
    # A1 uses PatternLayout.
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
    # Print only messages of level WARN or above in the package com.foo.
    log4j.logger.com.foo=WARN
    

    Here is another configuration file that uses multiple appenders:

    log4j.rootLogger=debug, stdout, R
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=example.log
    
    log4j.appender.R.MaxFileSize=100KB
    # Keep one backup file
    log4j.appender.R.MaxBackupIndex=1
    
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    

    Apache Solr

    If using Solr, copy <solr>/example/resources/log4j.properties into a location on the classpath.

    Sample configuration of log4j.properties from Solr goes like:

    #  Logging level
    solr.log=logs/
    log4j.rootLogger=INFO, file, CONSOLE
    
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
    
    #- size rotation with log cleanup.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.MaxFileSize=4MB
    log4j.appender.file.MaxBackupIndex=9
    
    #- File to log to and log format
    log4j.appender.file.File=${solr.log}/solr.log
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
    
    log4j.logger.org.apache.zookeeper=WARN
    log4j.logger.org.apache.hadoop=WARN
    
    # set to INFO to enable infostream log messages
    log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
    

    See also:

    • Short introduction to log4j: Default Initialization Procedure
    • Why can't log4j find my properties in a J2EE or WAR application?
    0 讨论(0)
  • 2020-11-22 08:58

    As explained earlier there are 2 approaches

    First one is to just add this line to your main method:

    BasicConfigurator.configure();
    

    Second approach is to add this standard log4j.properties file to your classpath:

    While taking second approach you need to make sure you initialize the file properly, Eg.

    Properties props = new Properties();
    props.load(new FileInputStream("log4j property file path"));
    props.setProperty("log4j.appender.File.File", "Folder where you want to store log files/" + "File Name");
    

    Make sure you create required folder to store log files.

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