Integrate log4j with SSH

Source: Internet
Author: User

Struts, spring, and hibernate both support log4j as their log implementation system. Therefore, if log4j can be reasonably configured, a complete log system can be created for the SSH architecture.

Advantages of using log4j In the SSH framework:

1. dynamically change the record level and policy, that is, to modify log4j. properties, you do not need to restart the web application, which needs to be set in Web. xml.
2. Set the log file to/WEB-INF/logs/without an absolute path.
3. You can put log4j. properties and other properties together in/WEB-INF/, rather than class-path.

Steps:

1. Import the required jar package.

2. Create a "*. propertites" File

3. Add the configuration in Web. xml. The Code is as follows:

1 <context-param> 2 <param-Name> log4jconfiglocation </param-Name> 3 <param-value> WEB-INF/log4j. properties </param-value> 4 </context-param> 5 <! -- Set the container to scan the log4j configuration file every 60 seconds --> 6 <context-param> 7 <param-Name> log4jrefreshinterval </param-Name> 8 <param-value> 60000 </param-value> 9 </context-param> 10 <! -- Set the webapprootkey parameter to export data in log4j. the placeholder used in the properties configuration file for the Web application root directory --> 11 <context-param> 12 <param-Name> webapprootkey </param-Name> 13 <param-value> webapp. root </param-value> 14 </context-param> 15 <! -- You need to add a spring-web.jar package, or use the error message --> 16 <listener> 17 <listener-class> Org. springframework. web. util. log4jconfiglistener </listener-class> 18 </listener>
View code

Note:

1. If the log4j configuration file is placed in the src directory and named "log4j. propertites", it will be read at Tomcat startup. The reason is that the default log system of Tomcat is also log4j. at startup, log4j. propertites under SRC will be searched by default.

2. spring pushes the web directory path to webapp. root System variable (in the web. XML settings), $ {webapp. root} indicates the project directory. You can add it to the front of the input path. For example:Log4j. appender. appender_demo.file =$ {webapp. Root}/WEB-INF/log/ABC. Log

3. The spring listener is loaded at the front of the spring log listener, resulting in the use of logs during spring loading. However, the spring log listener has not been loaded yet. Therefore, the spring log listener should be placed before the spring listener.

Basic log4j Demo:

 1 # For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!   2 # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.   3 log4j.rootLogger=INFO, stdout, logfile   4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender   5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   6 log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n   7 log4j.appender.logfile=org.apache.log4j.RollingFileAppender   8 log4j.appender.logfile.File=${webApp.root}/WEB-INF/SSHIntegration.log   9 log4j.appender.logfile.MaxFileSize=512KB  10 # Keep three backup files.  11 log4j.appender.logfile.MaxBackupIndex=3  12 # Pattern to output: date priority [category] - message  13 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  14 log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n  15 ...  
View code

 

Integrate log4j with SSH

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.