Experience Directory in Software

Java,J2EE,Oracle,Linux experience and workaround tips. If you have thought at least once like "How much should I learn technologies to be good enough developer?", Yes answer is here, just read this articles.All articles are made use at real project and making clients happy!

Monday, February 11, 2008

Log4J logging software


Log4J -- well known logging library
Log4J Ver. 1.2 API

before decide to use logging, We got to do several things..
1.decide the log level policy.
It has 6 levels to be assigned.
you should define in your project, what log level represents what information.

1.fatal
2.error
3.warn
4.info
5.debug
6.trace

Surely it looks very strange if you define the unexpected system error log is assighed as debug log level.. So you'd better categorize all possible error and desired information outputed in log file and assign the log level meaningfully and acceptably..

Here is the one that we define in my current project...

All we wanted to do with Log4j is as follows.
1.Create and place log file with current date in the specified directory where is in working directory.
=> log file with current date means if run the software for 5 days everyday, we got 5 different log files. so we can easily study and analyze when a particular error occurs. (but I feel if logging information is less amount, you'd better have all in a single log file so that you can only open and study one file. If we have log files by date category in a single directory, when we access that log directory issued with "ls" command, it displays bunch number of log files with different date in it. then for looking for a specefic log file, we again have to issue other command like "ll | grep 08020" or kind of it for displaying only some logs in periodic term.
=>the reason why we wanted log files to place in the specified directory where is in working directory is we thought of future directory structure change.
our desired log file location is in the following directory.

working directory/logs/{here we have all log files}

Loggers,Appender,Layouts are key classes in Log4j software classes.

Logger class
has important methods that are listed in the following.



package org.apache.log4j;

public class Logger {

// Creation & retrieval methods:
public static Logger getRootLogger();
public static Logger getLogger(String name);

// printing methods:
public void trace(Object message);
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);

// generic printing method:
public void log(Level l, Object message);
}


You can define a prefered level to each logger but all logger inherits Root Logger whose level is assigned Leve.DEBUG by default but you can set yours yourself.
(You can find this note in log4j online manual page==>Note that by default, the root logger is assigned to Level.DEBUG.)

Appender class has responsibility of allowing a user to define where a log is outputed, to detail more, Log4J has appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons.

So In my case I need to output in files, and for the purpose of improving implementation productivity temporarily, and console. so for specifing the destinations where log is outputted we have to configure appender class to be given to a logger through calling the addAppender method on it. And more over If you wish to custermize the output format, you just need to associate a Layout object that you configure with an appender object.

But simplify all your configuration, you just write a configuration file and load it from the start point of your application where would be initialization method.

imagine this init() metho is loaded just at one instance after your application has started up.


init(){

DOMConfigurator.configure(System.getProperty("user.dir")+"\\logs\\log4j.xml");

}


We can have a configuration file in either properties or xml.
I decided to be in xml. you can refer xml configuration format example here(http://wiki.apache.org/logging-log4j/Log4jXmlFormat).

Please look at the following log4j.xml sample.

oh, How can I paste and display XML code here?!
Does Blogger prohibit displaying XML code?!
==>03.03.08 I could display XML code in blogger.All I have done was to convert <> special characters to HTML entities.

< --> &lt;
> --> &gt;
(References:Special Characters in HTML)

No comments:

The On Demand Global Workforce - oDesk