No appenders could be found for logger(log4j)?

前端 未结 30 2735
[愿得一人]
[愿得一人] 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: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 /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?

提交回复
热议问题