log4j.properties example

I can’t find many log4j.properties examples, here are a few log4j.properties examples that are used in my project, just for sharing.

1. Output to Console

All logging will be redirected to your console.

log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

3. Output to File

All logging will be redirected to your specified log file.

log4j.properties
# Root logger option
log4j.rootLogger=INFO, file
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
 
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
 
log4j.appender.file.File=C:\\logigng.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

3. Output to Console and File

All logging will be redirected to both log file and console.

log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Note
If you prefer XML, please refer to this log4j.xml example.

References

  1. Log4j manual
  2. Log4j PatternLayout manual
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: Learn More()

  • Pingback: Blue Coaster33()

  • Pingback: | ??????()

  • http://abaqueiro.blogspot.com Alfonso Baqueiro

    Te rifas bien pinche chinito. Thanks a lot.

  • Joseph

    I am using Log4j for selenium script, when I am running the script in eclipse it is properly giving the logs, but I am running the script in ANT not getting any log. can you please help me?

  • Farrukh

    Any idea why I am getting below warning on my glassfish console
    SEVERE: log4j:WARN No appenders could be found for logger (org.displaytag.properties.TableProperties).

    I have log4j in my classes folder

    log4j.rootLogger=DEBUG, testAppender
    log4j.appender.testAppender=com.webmethods.sc.logging.log4j.WmDailyFileAppender
    log4j.appender.testAppender.file=log/test1.log
    log4j.appender.testAppender.datePattern=’.’yyyy-MM-dd_HH_mm
    log4j.appender.testAppender.Append=false
    log4j.appender.testAppender.layout=com.webmethods.sc.log4j.PatternLayout
    log4j.appender.testAppender.layout.ConversionPattern=%m%n

    #Prevent internal log4j DEBUG messages from polluting the output.
    log4j.logger.org.apache.log4j.PropertyConfigurator=INFO
    log4j.logger.org.apache.log4j.config.PropertySetter=INFO
    log4j.logger.org.apache.log4j.FileAppender=INFO

  • deep

    What is Console in terms of Linux.

    I am facing a weird situation wherein the required spring framework logs are getting logged in Eclipse console but not in File appender files in windows. Some other logs gets logged. its only spring related which are not logged in File but gets logged in eclipse Console.

    In my linux production box, I can similarly see that in the Files the spring logs are not populated. So I want to check how about console logs.
    Searched around quite a bit but need to unserstand where the console logs re present in Linux/Unix box.

  • ankit

    thanks

  • http://irvingsyeb.jigsy.com/entries/general/weight-loss-success-is-within-your-reach jigsy.com

    When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment.
    Is there any way you can remove me from that service? Thanks a lot!

  • http://www.ppireclaimco.co.uk ppi reclaim

    I know this website offers quality dependent posts and additional stuff, is there any other web site which offers these kinds of stuff in quality?

  • http://codezuke.com prateek

    Hi
    I am using no. of jars as dependencies in project. How can i redirect the logging of some jars in a file and others in some different files? Please suggest.

  • Michael

    Thanks, your examples helped me a lot.

  • Raghunadh Parlapalli

    This is the best site to get the instant help on Java

  • harry

    explain me abt log4j properties?

  • Kevin Sheppard

    Thank you so much for sharing this! It really help me.

  • http://nitinsurana.com Nitin

    hello mkyong,

    Thanks for this information. Actually I’m also logging to both stdout & file.
    But I want to redirect stdout & stderr to some file, not to console!
    How can I achieve this ?

    • http://[email protected] Ankur Bedi

      Its simple actually , even without logger :

      PrintStream psconsoleOut = new PrintStream(System.out);
      PrintStream psconsoleOutErr = new PrintStream(System.err);

      FileOutputStream fos = new FileOutputStream(new File(“console.txt”));
      PrintStream psfile = new PrintStream(fos);
      System.setOut(psfile);
      System.setErr(psfile);

      All System.out and System.err will go directly to console.txt
      when finally done , set it back :

      System.setOut(psconsoleOut);
      System.setErr(psconsoleOutErr);

      • Venkat

        NIce……

  • boo

    It’s not working.

    • http://[email protected] Ankur Bedi

      Then you have not done something right .

    • http://www.ahsmarthome.com/Shownews.asp?id=730 website

      Yesterday, while I was at work, my cousin stole
      my iPad and tested to see if it can survive a twenty five foot
      drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views.

      I know this is completely off topic but I had
      to share it with someone!

  • BiH Java Developer

    tnx

  • satish

    Sir good morning,i have problem with jdbc code with log4j i.e:
    when particular operation is executed in DATA BASE TABLE i want to print log messages to a particular log file daily sir can u tell how do it.

    • http://[email protected] Ankur Bedi

      Have you even used log4j ?

  • Pingback: log4j Tutorial « Slon’s Blog()

  • Vishal

    Hi,

    can anyone explain the meaning of below statement?

    log4j.appender.file.MaxBackupIndex=20

    Thanks,

    • http://[email protected] Ankur Bedi

      Apply your mind . The answer is already in the line you posted .

      • bernd

        Wow, you’re a real help. Great tip.

      • Anon

        Very helpful, not. Why do you even bother …

      • 1337

        and what it mean?

  • Mark

    Thanks for the quick and simple example. And congrats for showing up #1 on Google for “log4j.properties example file”

    Mark

  • http://www.autoescuelabajocoste.com Autoescuela en Sevilla

    Thank you Mkyong, I was looking for something similar in order to use with Log4QT.(I think is the same), and I can’t find any esasy example!

    Thanks again.

  • Anoumous

    Hi,
    I liked your blog.
    I have a query – is there any way to log the different methods of a class into different files? (same class I am asking about)

    Please do let me know if there is any solution.

    Thanks in advance

    • http://[email protected] Ankur Bedi

      Yes , you simply use different loggers or dynamically change logger properties !!

  • Tien Thai

    My hibernate binding parameters does show in the log when run in Tomcat and WebSphere application 6.1 but does not show in WebSphere 7. By any chance, would you please give me any suggestion you might have. Your help is appreciated.

    Tien Thai

  • joe

    Hello,

    How do I get tomcat logs (access and error)(as well as for webapps) to go syslog?
    I have syslog configured to go to a syslog server. Ideally I want the logs I specified to to go that same syslog server. (Syslog server is ArcSight)

    Thanks
    Joe

  • http://www.mkyong.com veeraababu

    *** Please give a simple example to integrate Spring and Log4j ***

    • manu

      OK i will give you ..

  • http://www.test.com User1
     
    public static String replaceWord(String original, String find, String replacement) {
    	    int i = original.indexOf(find);
    	    if (i < 0) {
    	        return original;  // return original if 'find' is not in it.
    	    }
    	    String partBefore = original.substring(0, i);
    	    String partAfter  = original.substring(i + find.length());
     
    	    return partBefore + replacement + partAfter;
    	}
  • Pingback: Struts 2 + Log4j integration example()

  • Pingback: Struts + Log4j integration example | Struts()

  • http://anjanb.wordpress.com anjan bacchu

    hi there,

    thanks for the post.

    Google can be a good example for searching for a file type with an extension.

    You can type “log4j.properties filetype:properties” into the google search box(without the quotes) and you will get quite a few matches.

    example

    BR,
    ~A

    • mkyong

      Hi, nice info, thanks for the tips :)