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

70 comments on “log4j.properties example

  1. Another Sample Log4j.Properties File :

    // Here we have defined root logger
    log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

    // Here we define the appender
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
    log4j.appender.HTML=org.apache.log4j.FileAppender

    // Here we define log file location
    log4j.appender.R.File=./log/testlog.log
    log4j.appender.TTCC.File=./log/testlog1.log
    log4j.appender.HTML.File=./log/application.html

    // Here we define the layout and pattern
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%d – %c -%p – %m%n
    log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
    log4j.appender.TTCC.layout.DateFormat=ISO8601
    log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
    log4j.appender.HTML.layout.Title=Application log
    log4j.appender.HTML.layout.LocationInfo=true

    Reply
  2. Hi, yong
    I need to set the logger level ( rootLoggerObj.setLevel(Level.DEBUG);) in properties file
    can you please give some idea

    Reply
  3. Hi How would you set the encoding for log file in the properties file.

    Reply
  4. How to write the log4j. prop file for using log4j-2* version.

    Reply
  5. Hi, please provide us with an example for netbeans + jax-rs + jersey (+ jackson) + log4j2.

    Reply
    1. Hi all where can we find this properties file or where to append this file in a java project? Kindly help

      Reply
      1. @sreenivas : you can
        – put it into “src/main/resources” (result in WEB-INF/classes for a WAR – WebApp archive),
        – or in your classpath,
        – or set it ; example: “-Dlog4j.configuration=file:..myDirectorylog4j.properties”

        Reply
  6. Hi, @mkyong. I am using Ubuntu and in the file path “log4j.appender.file.File=C:\logging.log” I am passing “log4j.appender.file.File=var/log/name/appManager/logging.log”. But no logging file was generated. Am I doing something wrong?

    Reply
  7. Hi All, i have a requirement where the maxFileSize of the log should be 10 MB size, and maxBackupIndex should be taken dynamically. The logs should be rolled back daily when the system date changes and new logs should be created for the next day. Is it possible to use both the techniques in a single configuration file and implement the scenario. Can any one help me on this.

    Reply
  8. Hi Mkyong,

    Is there a way to configure log4j properties file as to append ‘Locale’ alongside date ?

    Thanks in Advance

    Reply
  9. Hi, I have a challenge with the log4j. I need to filter some specific words before be written to the log. I have written a function in java that replace that words with asterisks but i don´t know how to instruct the logger that calls my routine before write to the log. My function accept 2 parameters as input, the message and the word to be replaced and returns the message filtered.

    Any ideas to accomplish this

    Thanks in advance.

    Reply
  10. Hi Mkyong,
    i have a requirement to make the log to be rolling over after reaching a maxfilesize. the log4j.properties was set to be rollover after 100MB. The log manage to roll but the archieve for the log was not to be seen in the folder. The log keep been written to the same file even after rooling over. i am using the log4j 1.2.8. is there any missing parameter in below code.

    ### LOG4J TRANSACTION
    log4j.logger.log4j_FE_BR=INFO, log4j_FE_BR
    log4j.additivity.log4j_FE_BR=false
    log4j.appender.log4j_FE_BR=org.apache.log4j.RollingFileAppender
    log4j.appender.log4j_FE_BR.File=C:/eai_logs/LOG4J/log4j_FE-BR.log
    log4j.appender.log4j_FE_BR.MaxFileSize=100MB
    log4j.appender.log4j_FE_BR.MaxBackupIndex=5
    log4j.appender.log4j_FE_BR.DatePattern=’.’yyyy-MM-dd
    log4j.appender.log4j_FE_BR.layout=org.apache.log4j.PatternLayout
    log4j.appender.log4j_FE_BR.layout.ConversionPattern=%d{ISO8601} %m%n

    Thanks & Regards,
    FaizJohan

    Reply
  11. From application, i print using sysout. those do not show in file. i am able to see on console only.
    Can you please let me how can i print those statements in a FIle.

    Reply
  12. Hi Mykong.
    Thanks for the useful information, i use your website for my projects.
    I need that my log file will be replaced by a new one if its size is more than 1 MB or
    the file creation date is more than 5 minutes.
    for the size issue i’m using the “MazFileSize” property .
    do you know what should i use for my second condition ?

    Thanks
    Aviram

    Reply
    1. can u share the code for checking the file size which you are using so that I can have a look as I am trying to achiever the same but t’s not happening.

      TIA
      Deepesh

      Reply
  13. Thank you so much. It saved me a lot of trouble. After a lot of struggle, got it correct from your website.

    Reply
  14. Mykong: I just wanted to say a thank you generally for your fantastic examples and explanations. You have recently become my ‘Go To Guy’ whenever I don’t understand something Java related, as well as many other topics.

    Reply
  15. 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?

    Reply
  16. 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

    Reply
  17. 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.

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

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

    Reply
  20. This is the best site to get the instant help on Java

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

    Reply
  22. 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 ?

    Reply
    1. 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);

      Reply
    1. 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!

      Reply
  23. 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.

    Reply
    1. put logging line in your JDBC java code or use aspect j to inject log lines

      Reply
  24. Hi,

    can anyone explain the meaning of below statement?

    log4j.appender.file.MaxBackupIndex=20

    Thanks,

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

    Mark

    Reply
  26. 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

    Reply
  27. 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

    Reply
  28. 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

    Reply
  29.  
    
    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;
    	}
    
    
    
     
    Reply
  30. 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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *