Log4j Example On Using FileAppender And ConsoleAppender Simultaneously
|
Log4j »
On Feb 27, 2012 | { 10 Comments }
|
Tweet
|
Let us see how to use FileAppender and ConsoleAppender at a time.
Files Required
- Client.java
- log4j.properties
- my.txt [ We will let the appender to write into this file ]
Directory Structure
![]() |
![]() |
Client.java
import org.apache.log4j.Logger;
public class Client {
static Logger l = Logger.getLogger(Client.class.getName());
public static void main(String[] args) {
l.debug("This is debug message");
l.info("This is info message");
l.warn("This is warn message");
l.fatal("This is fatal message");
l.error("This is error message");
System.out.println("Your logic executed successfully....");
}
}
Once we run this client program, my.txt will contains….
log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,LOGFILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%nlog4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.File=my.txt
log4j.appender.LOGFILE.MaxFileSize=1kb
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%t] %-5p %c %d{dd/MM/yyyy HH:mm:ss} – %m%n
my.txt
[main] DEBUG Client 27/02/2012 21:39:15 – This is debug message
[main] INFO Client 27/02/2012 21:39:15 – This is info message
[main] WARN Client 27/02/2012 21:39:15 – This is warn message
[main] FATAL Client 27/02/2012 21:39:15 – This is fatal message
[main] ERROR Client 27/02/2012 21:39:15 – This is error message
Output In Eclipse Console
![]() |
![]() |
|
What you are thinkig....
10 Responses to “Log4j Example On Using FileAppender And ConsoleAppender Simultaneously”
If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum




how to enable package level logging
@Pintu
If you need package level logging just added this..
# Print only messages of level WARN or above in the package com.foo
log4j.logger.com.foo=WARN
what is difference between debug and info.
when we have to write that’s.
@Sajal
INFO: Is equivalent to System.out, and can use this one for general messages, just to know the progress of the application.
Debug: Helps developer to debug application, like where to fix and all
Warn: Gives a warning about an unexpected event to the user, designates potentially harmful situations, but doesn’t stop your app. some thing like invalid email address bla..
Error: Some thing alerts about problems that prevent your app from continuing normally
can you providesome more tools like ANT & JUNIT
Can u please provide Spring-MVC
Thanks for providing Log4j, the content is very nice and worthful, can you please provide ANT,CVS,JUnit and Spring MVC..
Hello
Please Provide me the jasper report code
plz provide JUnit…
Really superb tutorial, Thanks