<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>com.wafer.weixin.servlet.Log4JServlet</servlet-class>
<load-on-startup>1</load-on-startup>//加载该servlet,1代表优先级
</servlet>
<servlet-mapping>
<servlet-name>log4j</servlet-name>
<url-pattern>/log4j</url-pattern>
</servlet-mapping>
package com.wafer.weixin.servlet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;
import com.wafer.weixin.util.MessageUtil;
import com.wafer.weixin.util.SignUtil;
import com.wafer.weixin.util.WeixinUtil;
public class Log4JServlet extends HttpServlet{
public static org.apache.log4j.Logger log=org.apache.log4j.Logger.getLogger(Log4JServlet.class.getName());
public void init() throws ServletException{
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=df.format(d);
log.info("开始加载日志配置....");
String path = getServletContext().getRealPath("/");
String filename = path+"WEB-INF\\log4j.properties";
PropertyConfigurator.configure(filename);
log.info("日志配置加载完成....");
log.info("日志配置地址:"+filename);
String name="config\\system_config.propertys";
InputStream in;
try {
Properties p = new Properties();
in = new BufferedInputStream(new FileInputStream(path+name));
log.info("系统配置路径1:"+path+name);
p.load(in);
SignUtil.token=p.getProperty("token");
WeixinUtil.appId=p.getProperty("appId");
WeixinUtil.appSecret=p.getProperty("appSecret");
MessageUtil.cache=p.getProperty("cache");
MessageUtil.INTER_FACE_HOST=p.getProperty("INTER_FACE_HOST");
MessageUtil.WEIXIN_HOST=p.getProperty("WEIXIN_HOST");
MessageUtil.toUserName=p.getProperty("toUserName");
log.info(SignUtil.token);
log.info(WeixinUtil.appId);
log.info(WeixinUtil.appSecret);
log.info(MessageUtil.cache);
log.info(MessageUtil.INTER_FACE_HOST);
log.info(MessageUtil.WEIXIN_HOST);
log.info(MessageUtil.toUserName);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
token=wafersystems
appId=wx6696dd322fae2a1e
appSecret=685ab9909e609da53464bac801bb08e6
cache=D:\\tomcat\\apache-tomcat-7.0.53\\webapps\\Weixin
WEIXIN_HOST=http://tianjian115.xicp.net
INTER_FACE_HOST=http://smartoffice.wafersystems.com
toUserName=gh_31bc4c1cddb8
#cache=D:\\WSE\\Weixin\\WebContent
log4j.configuration=log4j.properties
log4j.rootCategory =debug,CONSOLE,FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4d [%t] %-5p %c %x - %m%n
log4j.appender.debug = org.apache.log4j.FileAppender
log4j.appender.debug.Threshold = ERROR
log4j.appender.debug.file = d:/debug.log
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%-4d [%t] %-5p %c %x - %m%n
log4j.logger.com.opensymphony=ERROR
log4j.logger.org.apache.struts2=ERROR
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=C\:/SmartOfficeLogs/WX.log
log4j.appender.FILE.Append=false
log4j.appender.FILE.Encoding=utf-8
log4j.appender.FILE.Threshold=info
log4j.appender.FILE.BufferedIO=false
#Buffer单位为字节,默认是8K 现在为 32k缓冲
log4j.appender.FILE.BufferSize=8192
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-4d [%t] %-5p %c %x - %m%n