LOG4J Mid-order application
1. Asynchronously bulk writes of important business logs to the database
Example configuration file:
Log4j.logger.business=info,db
Log4j.appender.db=org.apache.log4j.jdbc.jdbcappender
log4j.appender.db.buffersize=10
Log4j.appender.db.url=jdbc:hsqldb:res:/hsqldb/bookstore
Log4j.appender.db.driver=org.hsqldb.jdbcdriver
Log4j.appender.db.user=sa
log4j.appender.db.password=
Log4j.appender.db.sql=insert into Ss_log4j_log (priority,logdate,class,method,msg) VALUES ('%p ', '%d{yyyy-mm-dd HH:mm : ss} ', '%c ', '%M ', '%M ')
Log4j.appender.db.layout=org.apache.log4j.patternlayout log4j provides a simple and flexible mechanism that does not affect performance, writing important business logs to the database for future queries:
1. Create an arbitrarily named Log record table, set the connection parameters in the Log4j.properties, and write the inserted statements according to the name of the table names you just built.
2. buffersize=10 such parameters allow the log to be batched asynchronously, without impacting system performance.
3. Business log It is best not to use the original package structure, but to coordinate a separate name in the project, such as businesses.
Log Buzzlog = Logfactory.getlog ("Business"); 4. The demo code of the above function is in bookstore example Ordermanager.java, test code example in bookstore Dblogtest.java.
2.Spring several enhancements to the log4j
Personal advice is not to be used.
1. Refresh the log4j properties file regularly without restarting the server and apply the new log4j settings.
Although this is a Java EE best practice, it is noted in spring Javadoc that it is not recommended for production environments because the watch thread does not shut down when the server restarts.
2. Place the Log4j.properties file in the web-inf/log4j.properties.
This enhancement does not see any benefit, and if you put Web-inf, the test will not be able to output log4j information, unless the Web-inf is also set as class path, such as Appfuse, but this is only applicable to the case of ant script.
3. Place the log file in the project's Web-inf directory by setting a system variable such as {web.root}
Log4j.appender.logfile.file=${bookstore.root}/web-inf/logs/ Bookstore.log The advantage of this is that the path is stable, but if it is a war-type deployment, each time the redeployment of the original log file will be deleted, the application server in the case of JBoss this kind of confusion war package will be more error, so put the log file web-inf/ Logs is not a good choice.
In fact, the rule of the logfile path is that, if it is not an absolute path, it is the root of the relative path as the directory where the app runs.
So I usually define this, usually pointing to the logs directory of Tomcat and many application servers:
Log4j.appender.logfile.file=. The disadvantage of/logs/bookstore.log is that if you add the Tomcat/bin directory to the value of the path variable and like to enter "Startup.bat" from the Run box in the Windows Start menu to start Tomcat, then log4j Log will be placed in the C:\Documents and Settings\logs directory.
3. Ignoring unnecessary information for some class library frameworks
Control the logging level of the third-party framework class library to avoid full screen is unimportant info, masking the really important information.
# Changing the log level to debug when debug
Log4j.logger.org.springframework=warn
# Changing the log level to DEBUG would display SQL Hibernate generated
Log4j.logger.org.hibernate=warn
Log4j.logger.org.hibernate.sql=error
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.