Skip to main content

log4j - How to write log to multiple log files using log4j.properties

In Java applications some times you may need to write your log messages to specific log files with its own specific log properties. If you are using log4j internally then first step that you need to do is to have a proper log4j.properties file.

Below example shows 2 log4j appenders which write to 2 different log files, one is a debug log and another one is a reports log. Debug log file can have all log messages and reports log can have log messages specific to reporting on say splunk monitoring.

# Root logger option  
 log4j.rootLogger=ALL,STDOUT,debugLog  
 log4j.logger.reportsLogger=INFO,reportsLog  
 log4j.additivity.reportsLogger=false 
 
 log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender  
 log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout  
 log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n 
 
 # Direct log messages to a log file  
 log4j.appender.debugLog=org.apache.log4j.RollingFileAppender  
 log4j.appender.debugLog.File="/Users/test/debuglog.log"  
 log4j.appender.debugLog.MaxFileSize=10MB  
 log4j.appender.debugLog.MaxBackupIndex=10  
 log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout  
 log4j.appender.debugLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n 
 
# Direct log messages to a log file  
 log4j.appender.reportsLog=org.apache.log4j.RollingFileAppender  
 log4j.appender.reportsLog.File="/Users/test/reportslog.log"  
 log4j.appender.reportsLog.MaxFileSize=10MB  
 log4j.appender.reportsLog.MaxBackupIndex=10  
 log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout  
 log4j.appender.reportsLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n  

Here we defined a rootLogger which is the default logger where all messages will go to except those are designated for reportsLogger.

log4j.logger.reportsLogger=INFO,reportsLog -  indicates reportsLog appender will log INFO messages and up.

In Java Code you will initialize loggers as below. Here I am using Apache commons logging which is using log4j under the hood.

package com.developertrciks;  
   
 import org.apache.commons.logging.Log;  
 import org.apache.commons.logging.LogFactory;  
   
 public class MyTestClass {  
        
      private static Log log= LogFactory.getLog("debugLogger");  
      private static Log reportsLog= LogFactory.getLog("reportsLogger");  
        
      public void logTester() {  
           log.debug("debug log message");  
           reportsLog.debug("reports debug message");  
           reportsLog.info("reports info message");  
      }  
   
       public static void main(String[] args) {  
             
           MyTestClass test = new MyTestClass();  
           test.logTester();  
           System.out.println("completed successfully");  
             
      }  
 }  
   


Comments

  1. Awesome! Log4j is an open source , light weight framework that facilities a developer to log their message during the application development. These messages can be categorized and optionally displayed in development ,QA and production environment. thanks~ Charlotte W. from custom iot development

    ReplyDelete
  2. please explain the Snmp version-3 getbulk operatin in java server and agent both with example.

    ReplyDelete
  3. I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. quads for sale

    ReplyDelete
  4. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. taxiid

    ReplyDelete
  5. I really like your writing style, great information, thankyou for posting. 토토사이트

    ReplyDelete
  6. Thankyou for this wondrous post, I am glad I observed this website on yahoo. 토토사이트

    ReplyDelete
  7. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. 토토사이트

    ReplyDelete
  8. This is the type of information I’ve long been trying to find. Thank you for writing this information. 릴게임

    ReplyDelete
  9. This is the type of information I’ve long been trying to find. Thank you for writing this information. 안전놀이터

    ReplyDelete
  10. Merely a smiling visitant here to share the love (:, btw outstanding style. best site to buy youtube views

    ReplyDelete
  11. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. pii-email

    ReplyDelete
  12. This is the type of information I’ve long been trying to find. Thank you for writing this information. factcheck.org

    ReplyDelete
  13. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. https://www.abercrombieand-fitch.com.co/

    ReplyDelete
  14. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. www.digitalanalog.in

    ReplyDelete
  15. It is my first visit to your blog, and I am very impressed with the articles that you serve. Give adequate knowledge for me. Thank you for sharing useful material. I will be back for the more great post. start a credit card processing business

    ReplyDelete
  16. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. Blissy pillow case reviews

    ReplyDelete
  17. What is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge! North American Bancard Agent Program

    ReplyDelete
  18. Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! new internet device

    ReplyDelete
  19. I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Dentitox pro review

    ReplyDelete
  20. Thanks for the blog post buddy! Keep them coming... winnerslot

    ReplyDelete
  21. thanks for the tips and information..i really appreciate it.. buy cialis 40mg in

    ReplyDelete
  22. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. detectives infidelidades

    ReplyDelete
  23. This is the type of information I’ve long been trying to find. Thank you for writing this information. 메이저놀이터

    ReplyDelete
  24. This is the type of information I’ve long been trying to find. Thank you for writing this information. 먹튀검증사이트

    ReplyDelete
  25. This content is written very well. Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you. Golden revive plus

    ReplyDelete

Post a Comment

Popular posts from this blog

How to format and install macOS in your old Macbook/ iMac

 You can follow these steps to install a mac OS on an old Mac book following these steps. Here I assume that you have the actual bootable CD for the OS for installation. 1. Restart the laptop 2. Press Command + R key until it shows recovery mode 3. Open Disk Utilities 4. Select the hard drive and try to partition the drive. For example I have created a partition called Partition1 5. Insert bootable CD and restart the laptop. When option comes choose to boot from the CD. 6. Choose partition1 as the place to install the OS 7. Continue the installation process. 8. Once installation is completed then it might need to restart for further updates. 9. Most of the times a more recent compatible version of the OS might be available. In order to upgrade to the more latest compatible OS follow below steps. 11. Find the latest compatible version of OS. 12. Go to apple support sites and manually download the image and click to install. 13. Follow installation instructions and this would upgrade you

How to create a minikube single node cluster for learning Kubernetes

In this post I will explain how to setup a minikube single node kubernetes cluster using AWS EC2 instance which would help anyone who is trying to learn kubernetes and also help them to gain practical knowledge in kubernetes by running kubernetes commands, creating kubernetes objects etc. Minikube is a single node kubernetes cluster which means a kubernetes cluster with only one node that is a single VM. Minikube is only used for learning purposes and it is not an alternative for a real kubernetes cluster and should not be used for development and production usage. In this example I have launched an AWS EC2 instance with below configuration where I will install minikube and related tools. AWS EC2 Instance Configuration AMI: Ubuntu Free tier eligible 64 bit Instance type : t2-large ( For me t2-small or t2-micro is giving performance issues due to less memory) Once the EC2 instance is up and running, login to the instance using below command on terminal. If you are using wi