How to initialize log4j properly?

前端 未结 24 3022
太阳男子
太阳男子 2020-11-22 06:49

After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselec         


        
24条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 07:17

    As per Apache Log4j FAQ page:

    Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?

    This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. 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.

    Basically the warning No appenders could be found for logger means that you're using log4j logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.

    There are three ways to configure log4j: with a properties file (log4j.properties), with an XML file and through Java code (rootLogger.addAppender(new NullAppender());).

    log4j.properties

    If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.

    classpath

    Here are some command suggestions in Linux how to determine your classpath value:

    $ echo $CLASSPATH
    $ ps wuax | grep -i classpath
    $ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf /usr/share/tomcat?
    

    or from Java: System.getProperty("java.class.path").

    Log4j XML

    Below is a basic XML configuration file for log4j in XML format:

    
    
    
    
       
         
         
           
         
       
    
       
         
         
      
      
    
    

    Tomcat

    If you're using Tomcat, you may place your log4j.properties into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.

    Solr

    For the reference, Solr default log4j.properties file looks 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
    

    Why can't log4j find my properties file in a J2EE or WAR application?

    The short answer: the log4j classes and the properties file are not within the scope of the same classloader.

    Log4j only uses the default Class.forName() mechanism for loading classes. Resources are handled similarly. See the documentation for java.lang.ClassLoader for more details.

    So, if you're having problems, try loading the class or resource yourself. If you can't find it, neither will log4j. ;)


    See also:

    • Short introduction to log4j at Apache site
    • Apache: Logging Services: FAQ at Apache site

提交回复
热议问题