You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Log4j Example On Using FileAppender And ConsoleAppender Simultaneously

Log4j » On Feb 27, 2012 By Sivateja

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

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%n

log4j.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








By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

22 Responses to “Log4j Example On Using FileAppender And ConsoleAppender Simultaneously”
  1. pintu says:

    how to enable package level logging

  2. Java4s says:

    @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

  3. Sajal says:

    what is difference between debug and info.
    when we have to write that’s.

  4. Java4s says:

    @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

  5. srinivas says:

    can you providesome more tools like ANT & JUNIT

  6. Raja says:

    Can u please provide Spring-MVC

  7. pallavi says:

    Thanks for providing Log4j, the content is very nice and worthful, can you please provide ANT,CVS,JUnit and Spring MVC..

  8. Siva Krishna says:

    Hello
    Please Provide me the jasper report code

  9. Nagendra says:

    plz provide JUnit…

  10. mahendra says:

    Really superb tutorial, Thanks

  11. Kiran says:

    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.

  12. Very useful tutorial, well explained.

    Please provide Web Services & REST tutorials.

    Thanks

  13. Ramu says:

    really nice website and your explanation is very good.

  14. Abhijeet says:

    nice tutorials, i am learning too much form here….

  15. sami says:

    thanks to Java4s ,i have learned many concepts from here.Nice Log4j tutorials..

  16. baazi says:

    Your explanation is keka sir..

    superbbb….

  17. Giri Prasad says:

    Thanks to website.
    Nice tutorial, every one understand easily, can you provide Maven,JBoss,JUnit,WebServices…It’s useful to everyone.

  18. Dilip kumar says:

    Can u provide cvs tool and webservices. It is very useful to every one.

  19. Yogesh says:

    Thanks….

    Please give one example of multiple log files.

  20. Sagar Misal says:

    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.

  21. Ananth105 says:

    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!!!

  22. Anand says:

    Awesome, U made my day. Thank you very much.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.