| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
How to use Log4j
Description :
PatternLayout More...
|
Written By : Amit
Title :
Using NDC and Log4j
Description :
NDC Push with Log4j More...
|
Written By : Amit
Title :
Log4j Example
Description :
Log4j Tutorial More...
|
Written By : guddu
Title :
Apache Commons Logging
Description :
with Log4j Example More...
|
Written By : ISHTEK
Title :
Log4j Example Configuration
Description :
Applying Levels Logger More...
|
| Tags/Keywords : Log4j Example, Log4j, Example,PatternLayout, Code, Tutorial, Article Author : Amit Date (Year/Month/Date): 2009-01-26
Logging Solution using Java Technology - Using Apache Log4j solution | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
Using Log4j with different types of logging mechanism
such as Console, File:
Using Log4j to log messages to the log file.
Let us create a class and a main class and use Log4j to write out
messages in a predefined layout.
Logging is very important for tracing and knowing how system
is working in deployment/production environment. In case of any defects
logged by customer/end user, one of the initial way to find out the
problem, where it occurred is by analysing log file.
Note: This example is using Apache Log4j version 1.1.3
This class is implementing the BasicConfigurator
TestFileLog.java
import org.apache.log4j.FileAppender;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PatternLayout;
public class TestFileLog {
static Category logCategory = Category
.getInstance(TestFileLog.class);
static String test = null;
public TestFileLog()
{
try{
PatternLayout patternLayout = new PatternLayout
("%d [%t] %-5p %c - %m%n");
FileAppender fileAppender = new FileAppender
(patternLayout,"TestLog.log",true);
BasicConfigurator.configure(fileAppender);
analyse();
}catch(Exception ex) {
}
}
public void analyse() {
logCategory.info("Entering application.");
String[] str = new String[10];
try{
for(int i=0;i<=str.length;i++)
str[i] = "Count:"+i;
}catch(Exception ex) {
logCategory.error(ex.toString());
} finally {
logCategory.info("Exiting Application.");
}
}
public static void main(String[] args) {
new TestFileLog();
}
}
Final result of this program is as follows:
TestLog.log
2007-09-01 14:34:14,421 [main] INFO TestFileLog - Entering application.
2007-09-01 14:34:14,421 [main] ERROR TestFileLog -
java.lang.ArrayIndexOutOfBoundsException: 10
2007-09-01 14:34:14,421 [main] INFO TestFileLog - Exiting Application.
Another way of doing this is by using a properties file and reporting
the log onto the console screen.
log.properties
-----------------------------------------------------------------------
log4j.rootCategory=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
-----------------------------------------------------------------------
The above mentioned properties file is used by the following java
program,
TestFileLog.java
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
public class TestFileLog {
static Category logCategory = Category
.getInstance(TestFileLog.class);
static String test = null;
public TestFileLog() {
try{
PropertyConfigurator.configure("log.properties");
analyse();
}catch(Exception ex) {
}
}
public void analyse() {
logCategory.info("Entering application.");
String[] str = new String[10];
try{
for(int i=0;i<=str.length;i++)
str[i] = "Count:"+i;
}catch(Exception ex) {
logCategory.error(ex.toString());
} finally {
logCategory.info("Exiting Application.");
}
}
public static void main(String[] args) {
new TestFileLog();
}
}
The output for this, comes out to be,
2007-09-01 14:37:53,328 [main] INFO TestFileLog - Entering application.
2007-09-01 14:37:53,328 [main] ERROR TestFileLog -
java.lang.ArrayIndexOutOfBoundsException: 10
2007-09-01 14:37:53,328 [main] INFO TestFileLog - Exiting Application.
|
All contents of this site are copyrighted by the Author, Contact Author
Reproduction of any of these material from this site, in any form,
is strictly prohibited.
|
|
|
Commented By -> Guddu | I guess you are using Log4j version former than 1.2, as you have used
Category in your example code.
As per Javadoc from Apache Log4j 1.2.x, Category class is deprecated,
and one should use org.apache.log4j.Logger class instead.
So I have written a simple example to show how to use Logger for
logging purpose.
Log4j.properties should be modified to use rootLogger,
instead of rootCategory, as show below:
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
|
And an example code for testing this as follows:
import org.apache.log4j.*;
public class TestLog4j
{
private static Logger logger = Logger.getLogger(TestLog4j.class);
public TestLog4j() {
testLogging();
}
public void testLogging() {
logger.debug("starting testLogging method....");
for(int i=0;i<10;i++) {
logger.info("counting work ..."+i);
}
logger.debug("closing testLogging method....");
}
public static void main(String[] args)
{
new TestLog4j();
}
}
|
After compiling and running this test code, following is the output
opn console that I observed :
2009-04-12 21:33:58,843 [main] DEBUG TestLog4j - starting testLogging method....
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...0
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...1
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...2
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...3
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...4
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...5
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...6
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...7
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...8
2009-04-12 21:33:58,875 [main] INFO TestLog4j - counting work ...9
2009-04-12 21:33:58,875 [main] DEBUG TestLog4j - closing testLogging method....
|
|
| | Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Apache Log4j >>> How to use Log4j >>> PatternLayout |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|