RSS

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 “log4j.properties” 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
# log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED

# Print only messages of level WARN or above in the package
#This is use for debuging mode
log4j.logger.testlogging=DEBUG

 

#### Appender writes to console
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=%d{yyyy-MM-dd} %5p [%t] (%F:%L) - %m%n

 

#### Appender writes to a file
#log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
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");
 log.info("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] (Log4J.java:19) - This is a Trace
2012-09-11 DEBUG [main] (Log4J.java:20) - This is a Debug
2012-09-11  INFO [main] (Log4J.java:21) - This is an Info
2012-09-11  WARN [main] (Log4J.java:22) - This is a Warn
2012-09-11 ERROR [main] (Log4J.java:23) - This is an Error
2012-09-11 FATAL [main] (Log4J.java:24) - This is a Fatal

Advertisements
 
10 Comments

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?

    run:
    java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message
    at log4j.Log4J.(Log4J.java:12)
    Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.message.Message
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 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:

WordPress.com Logo

You are commenting using your WordPress.com 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: