Log4j with NetBeans IDE

11 Sep

I think this post will help you to configure the log4j in Netbeans IDE

First you have to create the Java Project “Log4j”
Then you have to put the “” file in to your src folder “This should be located in root”

#### Use Three appenders,
#stdout - is used for write to console
#R - is used for write to file
log4j.rootLogger=debug, stdout, R
# Print only messages of priority WARN or higher for your category
# Specifically inherit the priority level

# Print only messages of level WARN or above in the package
#This is use for debuging mode


#### Appender writes to console
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd} %5p [%t] (%F:%L) - %m%n


#### Appender writes to a file
# Control the maximum log file size
# Archive log files (one backup file here)
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd} %5p [%t] (%F:%L) - %m%n
#log4j.appender.R.layout.ConversionPattern=%n%p - %m

Finally you have to take this “log4j-1.2.17.jar” file and add in to the class path

Now you done all configuration lets try to use it

package log4j;

import org.apache.log4j.Logger;

 * @author dinuka
public class Log4J {

//initializing the logger
static Logger log = Logger.getLogger(Log4J.class.getName());

 * @param args the command line arguments
 public static void main(String[] args) {
 //logging in different levels
 log.trace("This is a Trace");
 log.debug("This is a Debug");"This is an Info");
 log.warn("This is a Warn");
 log.error("This is an Error");
 log.fatal("This is a Fatal");


Output file

2012-09-11 TRACE [main] ( - This is a Trace
2012-09-11 DEBUG [main] ( - This is a Debug
2012-09-11  INFO [main] ( - This is an Info
2012-09-11  WARN [main] ( - This is a Warn
2012-09-11 ERROR [main] ( - This is an Error
2012-09-11 FATAL [main] ( - This is a Fatal


Posted by on September 11, 2012 in java


Tags: , ,

10 responses to “Log4j with NetBeans IDE

  1. Jerry

    September 19, 2013 at 4:20 pm

    This is excellent. thanks.

  2. oleksa borodie

    July 8, 2014 at 12:53 pm

    thanks a lot, it is very useful for beginner like me

  3. Prashanth

    January 14, 2015 at 9:14 am

    hi DINUKA MALALANAYAKE i tried this solution thank you for that but after a creating of jar file it is not running well

  4. brajesh

    January 9, 2016 at 5:22 am

    i was create same as above screenshot but in java file it give the error and not configure my netbeans until pls hepl me

  5. ashys

    March 17, 2016 at 10:30 am

    i’m gettin followoing error

    java.lang.NoClassDefFoundError: org/apache/log4j/Level

  6. Robert Paris

    April 29, 2016 at 3:29 pm

    I get the following when I run the application in NetBeans 8.1. I have the jar files in the CLASSPATH. I am using Log4j.2.5. Thoughts to troubleshoot?

    java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message
    at log4j.Log4J.(
    Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.message.Message
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    … 1 more
    Exception in thread “main” C:\Users\parisr\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

  7. Chinwendu Ochonma

    May 20, 2016 at 1:57 pm

    Tested with a sample application, it was fine. But when i deployed, it only created the file and nothing was written to the file, kindly assist.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: