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:23

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
查看更多
梦寄多情
3楼-- · 2018-12-31 10:23

While setting up log4j properly is great for "real" projects you might want a quick-and-dirty solution, e.g. if you're just testing a new library.

If so a call to the static method

org.apache.log4j.BasicConfigurator.configure();

will setup basic logging to the console, and the error messages will be gone.

查看更多
情到深处是孤独
4楼-- · 2018-12-31 10:23

I've created file log4j.properties in resources folder next to hibernate.cfg.xml file and filled it with text below:

log4j.rootLogger=INFO, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n

now I got rid of warnings and errors

查看更多
余欢
5楼-- · 2018-12-31 10:23

Logging API - The Java Logging API facilitates software servicing and maintenance at customer sites by producing log reports suitable for analysis by end users, system administrators, field service engineers, and software development teams. The Logging APIs capture information such as security failures, configuration errors, performance bottlenecks, and/or bugs in the application or platform. The core package includes support for delivering plain text or XML formatted log records to memory, output streams, consoles, files, and sockets. In addition, the logging APIs are capable of interacting with logging services that already exist on the host operating system.

Package java.util.logging « Provides the classes and interfaces of the Java platform's core logging facilities.


Log4j 1.x « log4j is a popular Java-based logging utility. Log4j is an open source project based on the work of many authors. It allows the developer to control which log statements are output to a variety of locations by using Appenders [console, files, DB and email]. It is fully configurable at runtime using external configuration files.

Log4j has three main components:

  • Loggers - [OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE]
  • Appenders

  • Layouts - [PatternLayout, EnhancedPatternLayout]

Configuration files can be written in XML or in Java properties (key=value) format.

  1. log4j_External.properties « Java properties (key=value) format

The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. Set appender specific options. « log4j.appender.appenderName.option=value, For each named appender you can configure its Layout.

log4j.rootLogger=INFO, FILE, FILE_PER_SIZE, FILE_PER_DAY, CONSOLE, MySql

#log.path=./
log.path=E:/Logs

# https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
# {%-5p - [WARN ,INFO ,ERROR], %5p 0- [ WARN, INFO,ERROR]}
log.patternLayout=org.apache.log4j.PatternLayout
log.pattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n

# System.out | System.err
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=${log.patternLayout}
log4j.appender.CONSOLE.layout.ConversionPattern=${log.pattern}

# File Appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log.path}/logFile.log
#log4j:ERROR setFile(null,false) call failed. - Defaults setFile(null,true)
#log4j.appender.FILE.Append = false
log4j.appender.FILE.layout=${log.patternLayout}
log4j.appender.FILE.layout.ConversionPattern=${log.pattern}

# BackUP files for every Day.
log4j.appender.FILE_PER_DAY=org.apache.log4j.DailyRollingFileAppender
# [[ Current File ] - logRollingDayFile.log ], { [BackUPs] logRollingDayFile.log_2017-12-10, ... }
log4j.appender.FILE_PER_DAY.File=${log.path}/logRollingDayFile.log
log4j.appender.FILE_PER_DAY.DatePattern='_'yyyy-MM-dd
log4j.appender.FILE_PER_DAY.layout=${log.patternLayout}
log4j.appender.FILE_PER_DAY.layout.ConversionPattern=${log.pattern}

# BackUP files for size rotation with log cleanup.
log4j.appender.FILE_PER_SIZE=org.apache.log4j.RollingFileAppender
# [[ Current File ] - logRollingFile.log ], { [BackUPs] logRollingFile.log.1, logRollingFile.log.2}
log4j.appender.FILE_PER_SIZE.File=${log.path}/logRollingFile.log
log4j.appender.FILE_PER_SIZE.MaxFileSize=100KB
log4j.appender.FILE_PER_SIZE.MaxBackupIndex=2
log4j.appender.FILE_PER_SIZE.layout=${log.patternLayout}
log4j.appender.FILE_PER_SIZE.layout.ConversionPattern=${log.pattern}

# MySql Database - JDBCAppender
log4j.appender.MySql=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.MySql.driver=com.mysql.jdbc.Driver
log4j.appender.MySql.URL=jdbc:mysql://localhost:3306/automationlab
log4j.appender.MySql.user=root
log4j.appender.MySql.password=
log4j.appender.MySql.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.MySql.layout.ConversionPattern=INSERT INTO `logdata` VALUES ('%p', '%d{yyyy-MM-dd HH:mm:ss}', '%C', '%M', '%L', '%m');
#log4j.appender.MySql.sql=INSERT INTO `logdata` VALUES ('%p', '%d{yyyy-MM-dd HH:mm:ss}', '%C', '%M', '%L', '%m');

# Direct log events[Messages] to MongoDB Collection - MongoDbAppender
log.mongoDB.hostname=loalhost
log.mongoDB.userName=Yash777
log.mongoDB.password=Yash@123
log.mongoDB.DB=MyLogDB
log.mongoDB.Collection=Logs

log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
log4j.appender.MongoDB.hostname=${log.mongoDB.hostname}
log4j.appender.MongoDB.userName=${log.mongoDB.userName}
log4j.appender.MongoDB.password=${log.mongoDB.password}
log4j.appender.MongoDB.port=27017
log4j.appender.MongoDB.databaseName=${log.mongoDB.DB}
log4j.appender.MongoDB.collectionName=${log.mongoDB.Collection}
log4j.appender.MongoDB.writeConcern=FSYNCED

MySQL Table structure for table logdata

CREATE TABLE IF NOT EXISTS `logdata` (
  `Logger_Level` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
  `DataTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `ClassName` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `MethodName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `LineNumber` int(10) NOT NULL,
  `Message` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  1. log4j_External.xml « XML log4j:configuration with public DTD file
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration PUBLIC
  "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="false">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <param name="target" value="System.out" />
        <param name="threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n" />
        </layout>
    </appender>

    <appender name="FILE" class="org.apache.log4j.FileAppender">
        <param name="file" value="E:/Logs/logFile.log" />
        <param name="append" value="false" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n" />
        </layout>
    </appender>

    <appender name="FILE_PER_SIZE" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="E:/Logs/logRollingFile.log" />
        <param name="immediateFlush" value="true"/>
        <param name="maxFileSize" value="100KB" />
        <param name="maxBackupIndex" value="2"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n" />
        </layout>
    </appender>

    <appender name="FILE_PER_DAY" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="E:/Logs/logRollingDayFile.log" />
        <param name="datePattern" value="'_'yyyy-MM-dd" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n"/>
        </layout>
    </appender>

    <root>
        <priority value="info" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
        <appender-ref ref="FILE_PER_SIZE" />
        <appender-ref ref="FILE_PER_DAY" />
    </root>
</log4j:configuration>

  1. Log4j Configuration from the URL in Java program:

In order to specify a custom configuration with an external file, the used class must implement the Configurator interface.

when default configuration files "log4j.properties", "log4j.xml" are not available

public class LogFiles {
    // Define a static logger variable so that it references the Logger instance named "LogFiles".
    static final Logger log = Logger.getLogger( LogFiles.class );

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        System.out.println("CONFIGURATION_FILE « "+LogManager.DEFAULT_CONFIGURATION_FILE);
        System.out.println("DEFAULT_XML_CONFIGURATION_FILE = 'log4j.xml' « Default access modifier");

        String fileName = //"";
                //"log4j_External.xml";
                "log4j_External.properties";
        String configurationFile = System.getProperty("user.dir")+"/src/" + fileName;

        if( fileName.contains(".xml") ) {
            DOMConfigurator.configure( configurationFile );
            log.info("Extension *.xml");
        } else if ( fileName.contains(".properties") ) {
            PropertyConfigurator.configure( configurationFile );
            log.info("Extension *.properties");
        } else {
            DailyRollingFileAppender dailyRollingAppender = new DailyRollingFileAppender();
            dailyRollingAppender.setFile("E:/Logs/logRollingDayFile.log");
            dailyRollingAppender.setDatePattern("'_'yyyy-MM-dd");

            PatternLayout layout = new PatternLayout();
            layout.setConversionPattern( "%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} %C{1}:%12.20M:%L - %m %n" );
            dailyRollingAppender.setLayout(layout);

            dailyRollingAppender.activateOptions();

            Logger rootLogger = Logger.getRootLogger();
            rootLogger.setLevel(Level.DEBUG);
            rootLogger.addAppender(dailyRollingAppender);

            log.info("Configuring from Java Class.");
        }

        log.info("Console.Message.");
        method2();
        methodException(0);
    }

    static void method2() {
        log.info("method2 - Console.Message.");
    }
    static void methodException(int b) {
        try {
            int a = 10/b;
            System.out.println("Result : "+ a);
            log.info("Result : "+ a);
        } catch (Exception ex) { // ArithmeticException: / by zero
            log.error(String.format("\n\tException occurred: %s", stackTraceToString(ex)));
        }
    }
    public static String stackTraceToString(Exception ex) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        return sw.toString();
    }
}
查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 10:30

To enable -Dlog4j.debug, I go to System, Advanced system settings, Environment variables and set system variable _JAVA_OPTIONS to -Dlog4j.debug.

查看更多
泪湿衣
7楼-- · 2018-12-31 10:32

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

org.apache.log4j.BasicConfigurator.configure(new NullAppender());
查看更多
登录 后发表回答