lib 加入 log4j.jar mail.jar activation.jar
web.xml
配置
<servlet>
<servlet-name>LogServlet</servlet-name>
<servlet-class>com.common.LogServlet</servlet-class>
<init-param>
<param-name>profile</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
package com.common;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class LogServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
String prefix = getServletContext().getRealPath("");//得到项目的绝对路径
String file = getInitParameter("profile");//得到log4j.properties文件的相对路径
String filePath = prefix + file;//log4j.properties文件的绝对路径
Properties props = new Properties();
FileInputStream istream = null;
try {
istream = new FileInputStream(filePath);
props.load(istream);//读取log4j.properties文件
//设置日志文件的位置
//通过得到 log4j.properties中File值,也就是文件的相对路径
//日志文件位置= 该项目路径+文件的相对路径
String logFile = prefix + props.getProperty("log4j.appender.file.File");
props.setProperty("log4j.appender.file.File", logFile);//设置日志文件的绝对路径
PropertyConfigurator.configure(props);
} catch (IOException e) {
System.out.println("Could not read configuration file [" + filePath + "].");
} finally {
try {
istream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void destroy() {
super.destroy();
}
}
log4j.properties文件配置
## LOGGERS ##
#configure root logger
log4j.rootLogger=INFO,console
#define a logger named myLogger
log4j.logger.myLogger=INFO,file,mail
### console ###
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### File###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### mail###
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.BufferSize=10
log4j.appender.mail.To=wengzil@163.com
log4j.appender.mail.From=logerror@sina.com
log4j.appender.mail.SMTPUsername=logerror
log4j.appender.mail.SMTPPassword=logerror
log4j.appender.mail.Subject=ErrorLog
log4j.appender.mail.SMTPHost=smtp.sina.com
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
日志的使用在java中写上
Logger myloger = Logger.getLogger("myLogger");
myloger.error(message);
web.xml
配置
<servlet>
<servlet-name>LogServlet</servlet-name>
<servlet-class>com.common.LogServlet</servlet-class>
<init-param>
<param-name>profile</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
package com.common;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class LogServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
String prefix = getServletContext().getRealPath("");//得到项目的绝对路径
String file = getInitParameter("profile");//得到log4j.properties文件的相对路径
String filePath = prefix + file;//log4j.properties文件的绝对路径
Properties props = new Properties();
FileInputStream istream = null;
try {
istream = new FileInputStream(filePath);
props.load(istream);//读取log4j.properties文件
//设置日志文件的位置
//通过得到 log4j.properties中File值,也就是文件的相对路径
//日志文件位置= 该项目路径+文件的相对路径
String logFile = prefix + props.getProperty("log4j.appender.file.File");
props.setProperty("log4j.appender.file.File", logFile);//设置日志文件的绝对路径
PropertyConfigurator.configure(props);
} catch (IOException e) {
System.out.println("Could not read configuration file [" + filePath + "].");
} finally {
try {
istream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void destroy() {
super.destroy();
}
}
log4j.properties文件配置
## LOGGERS ##
#configure root logger
log4j.rootLogger=INFO,console
#define a logger named myLogger
log4j.logger.myLogger=INFO,file,mail
### console ###
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### File###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### mail###
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.BufferSize=10
log4j.appender.mail.To=wengzil@163.com
log4j.appender.mail.From=logerror@sina.com
log4j.appender.mail.SMTPUsername=logerror
log4j.appender.mail.SMTPPassword=logerror
log4j.appender.mail.Subject=ErrorLog
log4j.appender.mail.SMTPHost=smtp.sina.com
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
日志的使用在java中写上
Logger myloger = Logger.getLogger("myLogger");
myloger.error(message);