How to initialize log4j properly?

2018-12-31 09:57发布

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 (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

It seems this means a configuration file is missing. Where should this config file be located and what is a good start content?

I'm using plain java for developing a desktop application. So no webserver etc...

20条回答
只靠听说
2楼-- · 2018-12-31 10:32

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.

查看更多
弹指情弦暗扣
3楼-- · 2018-12-31 10:35

My log4j got fixed by below property file:

## direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.rootLogger=debug, stdout
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=./logs/test.log
log4j.appender.file.threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug,file
查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 10:36

Find a log4j.properties or log4j.xml online that has a root appender, and put it on your classpath.

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.rootLogger=debug, stdout

will log to the console. I prefer logging to a file so you can investigate afterwards.

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=test.log
log4j.appender.file.threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug,file

although for verbose logging applications 100KB usually needs to be increased to 1MB or 10MB, especially for debug.

Personally I set up multiple loggers, and set the root logger to warn or error level instead of debug.

查看更多
只靠听说
5楼-- · 2018-12-31 10:36

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

查看更多
何处买醉
6楼-- · 2018-12-31 10:39

Another way to do it without putting the property file on the classpath, is to set the property from the java code directly. Here is the sample code.

public class Log4JSample {

public static void main(String[] args) {
    Properties properties=new Properties();
    properties.setProperty("log4j.rootLogger","TRACE,stdout,MyFile");
    properties.setProperty("log4j.rootCategory","TRACE");

    properties.setProperty("log4j.appender.stdout",     "org.apache.log4j.ConsoleAppender");
    properties.setProperty("log4j.appender.stdout.layout",  "org.apache.log4j.PatternLayout");
    properties.setProperty("log4j.appender.stdout.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");

    properties.setProperty("log4j.appender.MyFile", "org.apache.log4j.RollingFileAppender");
    properties.setProperty("log4j.appender.MyFile.File", "my_example.log");
    properties.setProperty("log4j.appender.MyFile.MaxFileSize", "100KB");
    properties.setProperty("log4j.appender.MyFile.MaxBackupIndex", "1");
    properties.setProperty("log4j.appender.MyFile.layout",  "org.apache.log4j.PatternLayout");
    properties.setProperty("log4j.appender.MyFile.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");

    PropertyConfigurator.configure(properties);

    Logger logger = Logger.getLogger("MyFile");

    logger.fatal("This is a FATAL message.");
    logger.error("This is an ERROR message.");
    logger.warn("This is a WARN message.");
    logger.info("This is an INFO message.");
    logger.debug("This is a DEBUG message.");
    logger.trace("This is a TRACE message.");
}

}

查看更多
只若初见
7楼-- · 2018-12-31 10:40

Simply, create log4j.properties under src/main/assembly folder. Depending on if you want log messages to be shown in the console or in the file you modify your file. The following is going to show your messages in the console.

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
查看更多
登录 后发表回答