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 - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.