Newsletter |
Log4j Example On Using FileAppender And ConsoleAppender Simultaneously
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
You Might Also Like
::. About the Author .:: | ||
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
Hi Java4s,
I tried the above code and working good. but i need to create log file my.txt in dynamic date folders i mean my.txt has to be in 27/06/2013 and in 28/06/2013. Please help me.
Very useful tutorial, well explained.
Please provide Web Services & REST tutorials.
Thanks
really nice website and your explanation is very good.
nice tutorials, i am learning too much form here….
thanks to Java4s ,i have learned many concepts from here.Nice Log4j tutorials..
Your explanation is keka sir..
superbbb….
Thanks to website.
Nice tutorial, every one understand easily, can you provide Maven,JBoss,JUnit,WebServices…It’s useful to everyone.
Can u provide cvs tool and webservices. It is very useful to every one.
Thanks….
Please give one example of multiple log files.
Hi ,
I need some help in below case ,
I need to filter the message which are going into log file before
printing.
Actually I am using Soap Web service with spring and with some configuration as ,
log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE
Log4j automatically log all soap request respose in log file
i just need to filter the message containt before printing in file so i can prevent password field to log in lg file.
Hi Java4s, Really Very Nice Tutorials it worth to learn here… Please help me how to clear the previous data in a text file by using log4j.properties I mean you explained in the first example by using boolean value false, so it clears the previous data… But you didn’t explain in log4j.properties file
Thanks in Advance!!!
Awesome, U made my day. Thank you very much.
Awesome , Nice and simple Explanation
Great effort !! Worth reading .. User friendly presentation … Thank you so much ….
Very useful. Thank u very much !!
I have no idea about log4j.properties file,this us first time i have seen this file,can u give some more clarity about log4j.properties file
dbt:whether we have to write those stuffs manually or it will be there in that by default
log4j.rootLogger=
log4j.appender.CONSOLE=
log4j.appender.CONSOLE.layout=
log4j.appender.CONSOLE.layout.ConversionPattern=
log4j.appender.LOGFILE=
log4j.appender.LOGFILE.File=
log4j.appender.LOGFILE.MaxFileSize=
log4j.appender.LOGFILE.layout=
log4j.appender.LOGFILE.layout.ConversionPattern=
These stuffs are to be written manually as per your choice.
Sir ji Plz provide more content regarding this, specialy How to
seperate server log and application log.If you have time plz
explain me how to seperate.
please provide complete rest webservices
I would like to thank whole java4s team for explaining each topic in very simple way unlike other tutorial.
Good explantion and please provide TUTORIAL FOR MAVAN AND JUNIT tool.
Good Explanation and please provide Tutorial for Junit Tool.