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.
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.
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");
}
}
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
ReplyDeleteplease explain the Snmp version-3 getbulk operatin in java server and agent both with example.
ReplyDeleteI 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
ReplyDeleteThanks 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
ReplyDeleteI really like your writing style, great information, thankyou for posting. 토토사이트
ReplyDeleteThankyou for this wondrous post, I am glad I observed this website on yahoo. 토토사이트
ReplyDeleteYou 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. 토토사이트
ReplyDeleteThis is the type of information I’ve long been trying to find. Thank you for writing this information. 릴게임
ReplyDeleteThis is the type of information I’ve long been trying to find. Thank you for writing this information. 안전놀이터
ReplyDeleteMerely a smiling visitant here to share the love (:, btw outstanding style. best site to buy youtube views
ReplyDeleteNice 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
ReplyDeleteThis is the type of information I’ve long been trying to find. Thank you for writing this information. factcheck.org
ReplyDeleteImpressive 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/
ReplyDeleteYou 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
ReplyDeleteIt 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
ReplyDeleteThanks 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
ReplyDeleteWhat is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge! North American Bancard Agent Program
ReplyDeleteThank 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
ReplyDeleteI 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
ReplyDeleteThanks for the blog post buddy! Keep them coming... winnerslot
ReplyDeletethanks for the tips and information..i really appreciate it.. buy cialis 40mg in
ReplyDeleteI 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
ReplyDeleteThis is the type of information I’ve long been trying to find. Thank you for writing this information. 메이저놀이터
ReplyDeleteThis is the type of information I’ve long been trying to find. Thank you for writing this information. 먹튀검증사이트
ReplyDeleteThis 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