Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,296 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,228 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Logging with log4j

 
Reply to this topicStart new topic

> Logging with log4j, useful for debugging or following code flow

AngeluS
Group Icon



post 18 Apr, 2007 - 01:59 AM
Post #1


log4j
Inserting log statements into your code is a low-tech method for debugging it.
Very useful if you are working on developing distributed applications because there you don't always have debuggers available.

With log4j it is possible to enable logging at runtime without modifying the application binary.
Logging behavior can be controlled by editing a configuration file, without touching the application binary.
If you don't want to use external files to do the logging configuration, then you can always write all of the params in the code but that means you also would have to compile every time you change something to the logging parameters.

Logging is an effective tool to help you in testing your code.

More information on log4j, you can find on:
http://logging.apache.org/log4j/docs/
http://en.wikipedia.org/wiki/Log4j

I created this example last year so that some people in our company would get the basics of log4j and would finally stop using those pesky System.out.print statements. smile.gif

Why would it be better to use logging instead of System.outs?
Using System.out is ok but it once the app is moved to production, it is no longer necessary (most of the time) to log all this information to the console. That would mean that you would have to go through the entire code and remove or comment the System.out lines.
If you use log4j and then you can enable/disable the logging in the configuration file or in the java code in one place instead of going through all the code to make the necessary changes.

A simple example where log4j is used in the code is the following:
CODE

/**
* <P>
* Description: Example of how to use Log4j with configuration in the Java file.
* </P>
* @author Kevin
* @version 02/08/2006
*/
package com.be.Actas.Samples.Log4J;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

/**
* @author Kevin
*/
public class Log4JExample {
    private static Logger logger = Logger.getLogger(Log4JExample.class); //Identifying your logger for this class.
    

    /**
     * @param args
     * @author Kevin
     */
    public static void main(String[] args) {
        logger.setLevel(Level.INFO); //Setting level of logging, there are 6 logging levels in total.
        logger.addAppender(new ConsoleAppender(new SimpleLayout())); //Say how to log.  this goes to console in simple layout.
    
        logger.info("Log4j Logging is working..."); //INFO logging statement
        logger.debug("This shouldn't be shown because logging level is too low in properties file."); //another logging statement; this one should be invisible because its logging level is lower (DEBUG) than the level we have set earlier in the code.
    }
}

Output:
The appender used in the example is writing to the console. So there is no log file used here. The format of the output is also in simple mode. It is possible to format your output, you can find a more "complex" format in the configuration file (log4j.properties) in the attached zip file.

Logging:
6 Logging levels, from high to low they are:
* FATAL
* ERROR
* WARN
* INFO
* DEBUG
* TRACE
So if you would set the level on INFO. That means that everything above info level or info level itself will be logged, the rest will be ignored.



Attached to this tutorial you will also find an Eclipse project.
In this project you will find another Java example that uses an external configuration file.

Personally I find it a huge improvement over using System.out.println to do your testing and then in the end going through your code to turn those statements off, you just change a param in the config file of log4j and no more logging. smile.gif

If you take a look at the other example and you need more information about the configuration file or the java code, let me know and I'll try to explain. Or you can always google it.
I know that example isn't as documented as it could be. If I find the time in the next coming days/weeks, I'll try to update it.


Hopefully this has clarified a bit about log4j.


NOTE: This is my first tutorial here and if I forgot to explain some things, let me know and I'll edit this post and improve it...

Cheers!

This post has been edited by AngeluS: 18 Apr, 2007 - 09:45 AM


Attached File(s)
Attached File  Log4JExample.zip ( 322.94k ) Number of downloads: 487
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

bhandari
Group Icon



post 22 Feb, 2008 - 12:22 AM
Post #2
and can you please explain, how to redirect different level of log statements to different files like I want info logs to be in x.info file and error logs in x.error files.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/6/08 09:35AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month