Test log4j the project Structure
log4j.properties the path is src/config/log4j
log4j.properties the contents of the file
The following definition logs output level is INFO, and 2 output destinations are configured, one is A3, one is Consolelog4j.rootlogger = info,a3,console// Log lowest output level log4j.appender.a3.threshold=infolog4j.appender.a3.encoding=utf-8//produces one file per day Dailyrollingfileappender Log4j.appender.A3 = Org.apache.log4j.dailyrollingfileappender//file property specifies the location and file name of the resulting log file, which is configured under Windows//c:/ LOGTEST/LOGTEST.LOG,//Company project under Linux configuration is/app/weblogic/applications/logs/sxvip_logslog4j.appender.a3.file=c:/ logtest/logtest.log//output immediately when there is a log, the default is truelog4j.appender.a3.immediateflush=truelog4j.appender.a3.datepattern= ' _ ' yyyy-mm-dd//Log Layout Method log4j.appender.a3.layout=org.apache.log4j.patternlayout// Format of log in log file Log4j.appender.a3.layout.conversionpattern=%-d{yyyy/mm/dd HH:mm:ss} OSS%-5p [%c]-%m%n// Here you use Org.apache.log4j.ConsoleAppender to specify that you want to output the log to the console log4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.threshold=info//output target is console log4j.appender.console.target= system.outlog4j.appender.console.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%-D{YYYY/MM/DD HH:mm: ss} OSS%-5p [%c]-%m%n
in the Web Application of WEB.XMl with Spring configuration log4j
Add Configuration <!--Configure the path to the log4j configuration file in Web. Xml or properties (this parameter must be fitted)--classpath parameter specifies the location of the Log4j.properties file so that the log4j configuration file does not have to be placed under SRC <context-param> <param-name> Log4jconfiglocation</param-name> <param-value>classpath:config/log4j/log4j.properties</ Param-value></context-param> uses the spring listener to read the log4j profile when the app starts <listener><listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class></listener>
in the Java used in log4j
Import Org.apache.commons.logging.log;import org.apache.commons.logging.LogFactory; public class Myserviceparamaction extends baseadmaction{private static final log log = Logfactory.getlog ( Myserviceparamaction.class);p ublic actionforward ngcallserviceinfo (actionmapping mapping, actionform form, HttpServletRequest request, HttpServletResponse response) { //Get login user information, no login prompt user needs to log back in log.info ("My service query starts ....................................."); UserBean user = (UserBean) request.getsession (). getattribute ("User_info"); Log.info ("Get login user from session " +user); if (user! = null) { log.info ("user.getmisisdn () " +user.getmsisdn ()) ...}
When the app starts, it generates a log file under the C drive based on the log4j configuration (log4j.appender.a3.file=c:/logtest/logtestxxx.log)
Log information is added to the project's resources when it is accessed
2013/12/05 11:46:33 OSS INFO [com.sinovatech.myservice.action.MyServiceParamAction]-My service query begins .....
2013/12/05 11:46:33 OSS INFO [com.sinovatech.myservice.action.MyServiceParamAction]-Get login user from session NULL
2013/12/05 11:46:33 OSS INFO [com.sinovatech.myservice.action.MyServiceParamAction]-VIP My service query is over ...
Note when the Java Class of Main method does not add log information to the log file when you test it in the
Using an instance of log4j in a Java project