Interview Questions On Java,Java EE


Enter your email address:



Add to Technorati Favorites
Google PageRank Checker
Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

Monday, October 5, 2009

How do you configure log4j with XML?

This configuration of log4j with XML consists of following files:

a.XMLLog4jExample.java
b.log4j.xml

XMLLog4jExample.java
package com.iqjava.log4j; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; public class XMLLog4jExample { static final Logger logger = Logger.getLogger(XMLLog4jExample.class); public static void main(String[] args) { DOMConfigurator.configure("log4j.xml"); logger.debug("This is a debug message"); logger.info("This is an info message"); logger.warn("This is a warn message"); logger.error("This is an error message"); logger.fatal("This is a fatal message"); } }


log4j.xml looks like:






<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="CA" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" /> </layout> </appender> <root> <level value="debug" /> <appender-ref ref="CA" /> </root> </log4j:configuration>


Here goes the output of the program:

0 [main] DEBUG com.iqjava.log4j.XMLLog4jExample - This is a debug message 2 [main] INFO com.iqjava.log4j.XMLLog4jExample - This is an info message 2 [main] WARN com.iqjava.log4j.XMLLog4jExample - This is a warn message 2 [main] ERROR com.iqjava.log4j.XMLLog4jExample - This is an error message 2 [main] FATAL com.iqjava.log4j.XMLLog4jExample - This is a fatal message


Eclipse project structure is depicted as in diagram given below:


Continue reading...

Friday, October 2, 2009

How to log different modules in different log files?

Logging messages from different modules in different log files are commonly expected in a project.One can create as many loggers specific to different modules/packages inside the log4j properties file.

As for instance, we want to log workflow related log messages into a file named workflow.log while services related logs into a file named services.log.

The log4j.properties file will describe two seperate FileAppenders and each of them is associated with individual package.The log4j.properties file as shown below depicts two FileAppenders:

log4j.rootLogger=DEBUG # WorkflowFileAppender - logs messages in the workflow.log file. log4j.appender.WorkflowFileAppender=org.apache.log4j.FileAppender log4j.appender.WorkflowFileAppender.File=workflow.log log4j.appender.WorkflowFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.WorkflowFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # ServicesFileAppender - logs messages in the services.log file. log4j.appender.ServicesFileAppender=org.apache.log4j.FileAppender log4j.appender.ServicesFileAppender.File=services.log log4j.appender.ServicesFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.ServicesFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.logger.com.iqjava.workflow=,WorkflowFileAppender log4j.logger.com.iqjava.services=,ServicesFileAppender


Here the root logger is set as 'DEBUG' which is just for information purpose, and not necessary.We have not set any logger level with individual file appenders. They both have been left blank so that rootLogger level would be inherited.We can surely change that if we want.

The code snippet contains two java files:

1. WorkflowExample.java

package com.iqjava.workflow; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class WorkflowExample { static Logger logger = Logger.getLogger(WorkflowExample.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }


2.ServicesExample.java

package com.iqjava.services; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class ServicesExample { static Logger logger = Logger.getLogger(ServicesExample.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }


If the individual java appenders are executed then output of workflow.log and services.log will look like as given below:

3.workflow.log
0 [main] DEBUG com.iqjava.workflow.WorkflowExample - Here goes the debug message 33 [main] ERROR com.iqjava.workflow.WorkflowExample - Here goes the error message 33 [main] FATAL com.iqjava.workflow.WorkflowExample - Here goes the fatal message 33 [main] INFO com.iqjava.workflow.WorkflowExample - Here goes the info message 34 [main] WARN com.iqjava.workflow.WorkflowExample - Here goes the warn message


4.services.log
0 [main] DEBUG com.iqjava.services.ServicesExample - Here goes the debug message 1 [main] ERROR com.iqjava.services.ServicesExample - Here goes the error message 1 [main] FATAL com.iqjava.services.ServicesExample - Here goes the fatal message 2 [main] INFO com.iqjava.services.ServicesExample - Here goes the info message 2 [main] WARN com.iqjava.services.ServicesExample - Here goes the warn message


If you want to change the logger level for these file appenders, you can always do that in log4j.properties file.
Continue reading...

Sunday, September 27, 2009

Log4j Configuration With properties file

Log4j configuration can be done either using an XML or a properties file externally. This makes your code independent of logging related steps to be followed in your code, so no ongoing source code modifications.

All you require to achieve that are logger, appender and layout. The logger object is used to do logging, appender decides where you want to append logging messages, to a console or to a file and finally layout decides on format, the way you want logging messages to be logged.

In the previous post, where BasicConfigurator.configure() is used, the default appender ConsoleAppender and layout PatternLayout are in use.

First of all the contents of log4j.properties file looks like:

log4j.rootLogger=ERROR, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-1r [%t] %-5p %c %x - %m%n


And now the ExampleLog4j.java file:

package com.iqjava.log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class ExampleLog4j { static final Logger logger = Logger.getLogger(ExampleLog4j.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }


To make things simpler for you, here is a snapshot from my Eclipse Galileo project:


The log4j levels follow the following order.
DEBUG
INFO
WARN
ERROR
FATAL

As the code level mentioned in .properties file is ERROR, it displays ERROR and FATAL messages only in result displayed of the code. If the code level would have been DEBUG then, all the messages would have been displayed on the console.

The result of the code looks like as given below:

0 [main] ERROR com.iqjava.log4j.ExampleLog4j - Here goes the error message 2 [main] FATAL com.iqjava.log4j.ExampleLog4j - Here goes the fatal message
Continue reading...

What is Log4j and how can it be configured?

Log4j is a Java based open source logging service by Apache which is available with no cost to the public.The latest edition at the time of writing this post is log4j 2.0 for logging services designed for Java 5 and later.

The insertion of logging comments help in better management of debugging of code specially if the application is distributed and enterprise in nature. The proper usage of logger levels namely DEBUG, INFO, WARN, ERROR and FATAL can help developers analyzing proper functioning of the code.

A simple log4j configuration is explained with a code sample given below, here important is that log4j runtime jar(log4j-1.2.15.jar) is included in the classpath of your application in order to execute the following code:


package com.iqjava.log4j; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; public class ExampleLog4j { static final Logger logger = Logger.getLogger(ExampleLog4j.class); public static void main(String[] args) { BasicConfigurator.configure(); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }


The output of the above code looks as given below:
0 [main] DEBUG com.iqjava.log4j.ExampleLog4j - Here goes the debug message 2 [main] ERROR com.iqjava.log4j.ExampleLog4j - Here goes the error message 2 [main] FATAL com.iqjava.log4j.ExampleLog4j - Here goes the fatal message 2 [main] INFO com.iqjava.log4j.ExampleLog4j - Here goes the info message 2 [main] WARN com.iqjava.log4j.ExampleLog4j - Here goes the warn message


The numbers in the beginning of the output represents time elapsed from the beginning of the execution of the code in milliseconds, thread name, logger level and then log message.

The other logging configuration using log4j is with properties or XML file.Watch out for the next post, it will be related to that.
Continue reading...
 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker