Newsletter

Log4j Example On Using FileAppender And ConsoleAppender Simultaneously

Log4j » on Feb 27, 2012 { 32 Comments } 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

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

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

32 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. 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. Thanks to website.
    Nice tutorial, every one understand easily, can you provide Maven,JBoss,JUnit,WebServices…It’s useful to everyone.

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

  23. Ajay says:

    Awesome , Nice and simple Explanation

  24. Priyanka says:

    Great effort !! Worth reading .. User friendly presentation … Thank you so much ….

  25. Farzana says:

    Very useful. Thank u very much !!

  26. Yuvashree says:

    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=

  27. Rakesh bhagat says:

    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.

  28. please provide complete rest webservices

  29. Akash Gupta says:

    I would like to thank whole java4s team for explaining each topic in very simple way unlike other tutorial.

  30. murali says:

    Good explantion and please provide TUTORIAL FOR MAVAN AND JUNIT tool.

  31. Sasikanth says:

    Good Explanation and please provide Tutorial for Junit Tool.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.