I am trying to use Log4J in Netbeans, however I am having a very hard time understanding the tutorials. Many of them say "do this" and assume I know what they are talking about. I do not. If you would, I'd like step-by-step instructions on where to find the correct libraries for Log4J, where to put them in a project, and an example program using Log4J.
3 Answers
log4j 2.x
Similar to below, but the name of the filename needs to be log4j2.properties
. (Also note that old examples of log4j.properties configuration don't actually seem to do anything useful in Log4j 2, so make sure to copy your initial configuration from a tutorial dedicated to Log4j 2, and not the older v1.)
Thanks @bobulous
log4j 1.x
If you get log4j:WARN No appenders could be found for logger
message, then
it is most likely that you haven't included the log4j.properties file in your
project. Here's a screenshot of how you should include it in NetBeans.

- 5,436
- 2
- 35
- 44
-
Just to save others ninety minutes of irate frustration, do note that if you're using Log4j **2** then the name of the filename needs to be `log4j2.properties`. (Also note that old examples of log4j.properties configuration don't actually seem to do anything useful in Log4j 2, so make sure to copy your initial configuration from a tutorial dedicated to Log4j 2, and not the older v1.) – Bobulous Jan 18 '18 at 21:51
This very comprehensive working example for log4j2 has nearly everything, duplicated here incase the link breaks
To get a working example, create a new maven java application in netbeans (or any IDE)
New Project -> Maven -> Java Application
In your new maven project there is a pom.xml under 'project files' in netbeans, you need to add this dependency:
I tested this with log4j version 2.11.1, check for latest here
...
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
</project>
Create a src/main/resources/log4j2.properties (this exact path and name is required)
you can later customize the print layout using this as a reference
status = error
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
Create a new class: Log4j2HelloWorldExample.java
package com.howtodoinjava.log4j2.examples;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2HelloWorldExample {
private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
public static void main(String[] args) {
LOGGER.debug("Debug Message Logged !!!");
LOGGER.info("Info Message Logged !!!");
LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
}
}
When you run the class you will get his output:
2016-06-16 13:41:27 DEBUG Log4j2HelloWorldExample:12 - Debug Message Logged !!!
2016-06-16 13:41:27 INFO Log4j2HelloWorldExample:13 - Info Message Logged !!!
2016-06-16 13:41:27 ERROR Log4j2HelloWorldExample:14 - Error Message Logged !!!
java.lang.NullPointerException: NullError
at com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:14)
[classes/:?]
If the above example works stand alone, but not when you integrate it into your project there might be some dependency interference
Run mvn dependency:tree
and exclude all interfering log4j dependencies, I needed to exclude these:
<exclusion>
<!--
[INFO] | +- log4j:log4j:jar:1.2.17:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.16:compile
-->
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
To bridge existing slf4j code to my newly added log4j2, I had to include this dependency I found here
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
</dependency>
I still saw this:
log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I also needed to ensure the log4j2.property file had my OS specific line endings, I was using Cygwin to create property files and running netbeans from windows and it failed to find appenders due to the property file all being read as a single line by Windows

- 839
- 9
- 15
http://www.tutorialspoint.com/log4j/log4j_sample_program.htm -
Download the jar from here: https://logging.apache.org/log4j/1.2/download.html
Add this file in your path:
log4j.properties
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Here is the location output of the file!
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Then run this:
log4jExample.java
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}

- 2,809
- 10
- 43
- 61
-
Well I got farther than usual, but i recieved this after compiling, building, and then running the program: run: log4j:WARN No appenders could be found for logger (log4jexample.Log4jExample). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. BUILD SUCCESSFUL (total time: 0 seconds) – Oct 21 '14 at 18:20
-
I think that means you haven't added the .properties file properly in your classpath. http://stackoverflow.com/questions/1485987/log4j-properties-file-where-to-put-it?rq=1 – Adz Oct 22 '14 at 08:27
-
Well the error is gone now, but replaced with a new one: java.lang.NoClassDefFoundError. It's referring to this part of the code: static Logger log = Logger.getLogger(Log4J.class.getName()); – Oct 23 '14 at 14:37
-
-
-
change static Logger log = Logger.getLogger(Log4J.class.getName()); to static Logger log = Logger.getLogger(log4jExample.class.getName()); . And if that doesn't work it's possible that your jar file cannot be found. – Adz Oct 23 '14 at 14:47
-
Part of the problem was that some other projects I had contained similar names and were getting mixed around. I redid your example and have made sure that log4j-1.2.17.jar is in the projects libraries. In the project Source Packages log4j.properties is right next to log4jExample.java. I'm getting an error saying that the appenders can't be found so I likely have .properties in the wrong spot. – Oct 23 '14 at 15:28