开发条件:log4j.properties 配置文件 +sqlserver数据库
所做功能:能够保存用户在登录状态下所做的操作
准备步骤:
1.log4j.properties配置文件中的信息:
log4j.rootLogger=debug,DB,console
#log4j.logger.business=INFO,db
#log4j.appender.db=com.neam.commons.MyJDBCAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%C:%L] - %m%n
#log4j.appender.db=com.neam.commons.MyJDBCAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%C:%L] - %m%n
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=userdb
log4j.appender.DB.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
log4j.appender.DB.user=数据库的用户名
log4j.appender.DB.password=数据库连接的密码
log4j.appender.DB.Threshold=ERROR
log4j.appender.DB.sql=insert into LOGS (name,password,DATED,LOGGER,lEVEL,MESSAGE) values ('%X{name}','%X{password}','%d{yyyy-MM-dd HH:mm:ss}','%c','%p','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
2.数据库表结构
![](https://i-blog.csdnimg.cn/blog_migrate/449cf14d85b8171848cb5406c33f45b8.png)
3. Controller中:
// 1.查询出Project表中的数据
@RequestMapping(value = "/showAll")
public String findAll(Model model,HttpSession session) {
List<Project> projects2 = projectService.findProject();
MDC.put("***", "****");
logger.info("查询所有");
logger.debug("查询所有");
logger.warn("查询全部");
logger.error("查询全部");
List<User> users=projectService.findUsers();
session.setAttribute("users", users);
session.setAttribute("projects", projects2);
return "bosIndex";
}
以上三步就可以直接保存操作日志,当然这个logger也可以放到其他接口中
如果要加上用户的操作日志:请加上第四步
4. ResFilter(MDc去保存用户名和密码)
public class ResFilter implements Filter{
private final static double DEFAULT_USERID= Math.random()*100000.0;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpSession session= req.getSession();
MDC.put("name","元气");
MDC.put("password","123456");
chain.doFilter(request, response);
}
public void init(FilterConfig Config) throws ServletException {
// this.filterConfig = Config;
// String ccc = Config.getServletContext().getInitParameter("cherset");
// this.targetEncoding = Config.getInitParameter("cherset");
}
5. web.xml中配置信息
<filter>
<filter-name>set log4j MDC for log</filter-name>
<filter-class>com.bos.util.ResFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>set log4j MDC for log</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>set log4j MDC for log</filter-name>
<filter-class>com.bos.util.ResFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>set log4j MDC for log</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
保存用户的操作日志大概就是这样,后面若有完善的地方会更新。。。