log4j java web_log4j简单使用java web项目--后台打印以及输出到文件

1,导入log4j-1.2.15.jar

2,在src下面新建log4j.properties

其内容为

### 设置###

log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=E://logger_data/error.log ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = E://logger_data/log.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = DEBUG

log4j.appender.D.layout = org.apache.log4j.PatternLayout

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### 输出ERROR 级别以上的日志到=E://logger_data/error.log ###

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender

log4j.appender.E.File =E://logger_data/error.log

log4j.appender.E.Append = true

log4j.appender.E.Threshold = ERROR

log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

3,添加servlet 容器加载时初始化

packagecom.mucfc;importjava.io.File;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.log4j.BasicConfigurator;importorg.apache.log4j.PropertyConfigurator;

@WebServlet("/Log4JInitServlet")public class Log4JInitServlet extendsHttpServlet {private static final long serialVersionUID = 1L;publicLog4JInitServlet() {super();

}public void init(ServletConfig config) throwsServletException {

System.out.println("Log4JInitServlet 正在初始化 log4j日志设置信息");

String log4jLocation= config.getInitParameter("log4j-properties-location");

ServletContext sc=config.getServletContext();if (log4jLocation == null) {

System.err.println("*** 没有 log4j-properties-location 初始化的文件, 所以使用 BasicConfigurator初始化");

BasicConfigurator.configure();

}else{

String webAppPath= sc.getRealPath("/");

String log4jProp= webAppPath +log4jLocation;

File yoMamaYesThisSaysYoMama= newFile(log4jProp);if(yoMamaYesThisSaysYoMama.exists()) {

System.out.println("使用: " + log4jProp+"初始化日志设置信息");

PropertyConfigurator.configure(log4jProp);

}else{

System.err.println("*** " + log4jProp + " 文件没有找到, 所以使用 BasicConfigurator初始化");

BasicConfigurator.configure();

}

}super.init(config);

}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

}

}

4,添加servlet 测试类

packagecom.mucfc;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.log4j.Logger;

@WebServlet("/Log4JTestServlet")public class Log4JTestServlet extendsHttpServlet {private static final long serialVersionUID = 1L;private static Logger logger = Logger.getLogger(Log4JTestServlet.class);publicLog4JTestServlet() {super();

}public void init(ServletConfig config) throwsServletException {

}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//记录debug级别的信息

logger.debug("This is debug message.");//记录info级别的信息

logger.info("This is info message.");//记录error级别的信息

logger.error("This is error message.");

}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

doGet(request,response);

}

}

5,修改web.xml

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">

log4j

Log4JInitServlet

com.mucfc.Log4JInitServlet

log4j-properties-location

/WEB-INF/classes/log4j.properties

1

Log4JTestServlet

/test

Log4JTestServlet

com.mucfc.Log4JTestServlet

6,tomcat跑起来后访问

http://localhost:8089/test

后台输出

c3a7df15a22f9984f48361432eed0332.png

磁盘上面生成的日志文件

297f12b18652a06ef4be4d914496a8f7.png

原文:https://www.cnblogs.com/rdchen/p/13573416.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值