Reputation:
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.
Upvotes: 8
Views: 23038
Reputation: 868
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
Upvotes: 3
Reputation: 5554
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
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.
Upvotes: 8
Reputation: 2849
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");
}
}
Upvotes: 0