How to initialize log4j properly?

前端 未结 24 3014
太阳男子
太阳男子 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 06:53

    Log4j by default looks for a file called log4j.properties or log4j.xml on the classpath.

    You can control which file it uses to initialize itself by setting system properties as described here (Look for the "Default Initialization Procedure" section).

    For example:

    java -Dlog4j.configuration=customName ....
    

    Will cause log4j to look for a file called customName on the classpath.

    If you are having problems I find it helpful to turn on the log4j.debug:

    -Dlog4j.debug
    

    It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

    The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page:

    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
    
    0 讨论(0)
  • 2020-11-22 06:54
    import org.apache.log4j.BasicConfigurator;
    

    Call this method

    BasicConfigurator.configure();
    
    0 讨论(0)
  • 2020-11-22 06:56

    For testing, a quick-dirty way including setting log level:

    org.apache.log4j.BasicConfigurator.configure();
    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.WARN);
    
    // set to Level.DEBUG for full, or Level.OFF..
    
    0 讨论(0)
  • 2020-11-22 06:56

    Maven solution:

    I came across all the same issues as above, and for a maven solution I used 2 dependencies. This configuration is only meant for quick testing if you want a simple project to be using a logger, with a standard configuration. I can imagine you want to make a configuration file later on if you need more information and or finetune your own logging levels.

        <properties>
            <slf4jVersion>1.7.28</slf4jVersion>
        </properties>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4jVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>${slf4jVersion}</version>
            </dependency>
    
    0 讨论(0)
  • 2020-11-22 06:57

    This is an alternative way using .yaml

    Logic Structure:

    Configuration:
        Properties:
        Appenders:
        Loggers:
    

    Sample:

    Configutation:
        name: Default
    
        Properties:
            Property:
                name: log-path
                value: "logs"
    
        Appenders:
    
            Console:
            name: Console_Appender
            target: SYSTEM_OUT
            PatternLayout:
                pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
    
           File:
              name: File_Appender
              fileName: ${log-path}/logfile.log
              PatternLayout:
                pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
    
        Loggers:
    
            Root:
                level: debug
                AppenderRef:
                  - ref: Console_Appender
    
            Logger:
                - name: <package>.<subpackage>.<subsubpackage>.<...>
                  level: debug
                  AppenderRef:
                     - ref: File_Appender
                     level: error             
    

    Ref: LOG4J 2 CONFIGURATION: USING YAML

    0 讨论(0)
  • 2020-11-22 06:58

    If you just get rid of everything (e.g. if you are in tests)

    org.apache.log4j.BasicConfigurator.configure(new NullAppender());
    
    0 讨论(0)
提交回复
热议问题