[java] Please initialize the log4j system properly warning

Here is the error message -

log4j:WARN No appenders could be found for logger (SerialPortUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Here is the invocation in the SerialPortUtil class -

private static final Logger log = Logger.getLogger(SerialPortUtil.class.getSimpleName());
.
.
.
log.info("Serial port " + port.getName() + " is available");

Here is the content of my log4j.properties file -

log4j.rootLogger=DebugAppender

#Debug logging
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=activityLog.log
log4j.appender.DebugAppender.MaxFileSize=200KB
log4j.appender.DebugAppender.MaxBackupIndex=5
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d{DATE} %t - %m%n

And the property file is on the classpath.

Everything looks right to me, so what's going on? Shouldn't the DEBUG threshold also capture INFO logging?

This question is related to java log4j

The answer is


What worked for me is to create a log4j properties file (you can find many examples over the net) and place it in properties folder in your project directory (create this folder if not exicts). The right click on the folder and Build Path->Use as Source Folder.

Hope it helps!


Try doing something like the following in main:

public static void main(String[] args) {
     PropertyConfigurator.configure(args[0]);
    //... your code

you need to tell log4j what its configuration should be.


If you are encountering the error/warning when you're running a program that uses log4j, the solution is to add a log4j.properties that can be seen by the class loader. Normally it's in the "src" folder of your Java project: Add the following contents to the file

log4j.rootLogger=INFO, 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{ABSOLUTE} %5p %c{1}:%L - %m%n

From the link in the error message:

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. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?.

The configuration file cannot be found. Are you using xml or a property file??

Also, use logback!


To add to @Gevorg, if you have a similar situation running Spark locally, place the log4j.properties file in the folder named resources under the main folder.


The fix for me was to put "log4j.properties" into the "src" folder. It did not work in my package folder it has to be one up at the very root of source. Adding it to the build path which then moves it to the "Reference Libraries" also did not work.

To repeat: put log4j.properties at the root of your src folder.

my log4j.properties files has the following content which works on the latest version of Spring Tool Suite which is based on Eclipse.

# Configure logging for testing: optionally with log file
#log4j.rootLogger=INFO, stdout
log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%

just configure your log4j property file path in beginning of main method: e.g.: PropertyConfigurator.configure("D:\files\log4j.properties");


Add the code

BasicConfigurator.configure();

in your static main class as below..

Note: add " \hadoop-2.7.1\share\hadoop\common\lib\commons-logging-1.1.3.jar & \hadoop-2.7.1\share\hadoop\common\lib\log4j-1.2.17.jar " as the external references

import org.apache.log4j.BasicConfigurator;

public class ViewCountDriver extends Configured implements Tool{

    public static void main(String[]args) throws Exception{

        BasicConfigurator.configure(); 

        int exitcode = ToolRunner.run(new ViewCountDriver(), args);
        System.exit(exitcode); 
    }
}

None of answered method solve the problem which log4j.properties file is not found for non-maven jsf web project in NetBeans. So the answer is:

  1. Create a folder named resources in project root folder (outermost folder).
  2. Right click project in projects explorer in left menu, select properties.
  3. Open Sources in Categories, add that folder in Source Package Folders.
  4. Open Run in Categories and add this to VM options: Dlog4j.configuration=resources/log4j.properties
  5. Click Ok, build and deploy project as you like, that's it.

I wrote special pattern in log4j file to check whether log4j is used my file:

# Root Logger Option
log4j.rootLogger=INFO, console

# Redirect Log Messages To Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} xxxx (%F:%L) - %m%n

I checked it because if you use BasicConfigurator.configure(); in your code in log4j use predefined pattern.


This work for me:

public static void main(String[] args) {

        Properties props = new Properties();
        props.load(new FileInputStream("src/log4j.properties"));
        PropertyConfigurator.configure(props);