【Spring in action】SSM接入log4j日志
程序员文章站
2024-02-15 22:39:53
...
前言:
上篇文章 ssm框架已经搭建起来,并且可以通过Mybatis调用数据库,本文是基于上一文章的代码接入log4j日志(其实看不看都不重要,可以直接拿代码过来尝试接入)。
那么这篇文章,我开始接入log日志功能。后端如果没有日志,出错了找谁去呢?
一、导入jar包:
(jar包从哪来?Mybatis的jar包自带Log4j的各种jar包)
二、新建一个配置文件log4j.properties
配置代码如下:第三行起是控制台输出;第8行起是输出到本地绝对路径:
/Users/user/eclipse-workspace/logSpring/WebContent/WEB-INF/logs/error.log
log4j.rootLogger = debug , stdout , D
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 = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = /Users/user/eclipse-workspace/logSpring/WebContent/WEB-INF/logs/error.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR
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
三、在web.xml中配置Log4j
<!-- Log4j -->
<!-- Log4j配置文件 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
四、测试一下:
1、在conf文件夹下新建mybatis配置文件generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动-->
<!-- <classPathEntry location="mysql-connector-java-5.1.18-bin.jar"/> -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ebook" userId="root" password="haibo1118">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.model" targetProject="logSpring/src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.mapping" targetProject="logSpring/src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="logSpring/src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="biquge" domainObjectName="Book" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
2、右键配置文件,生成三个文件。分别为Book.java,BookMapper.xml,BookMapper.java。具体代码就不写了。
3、然后新建service包,写一个接口对外:
package com.service;
import com.model.Book;
public interface IBookService {
Book getBookById(String bookId);
}
4、写一个实现类:
package com.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.BookMapper;
import com.model.Book;
@Service("userService")
public class BookServiceImpl implements IBookService{
@Resource
private BookMapper bookDao;
@Override
public Book getBookById(String bookId) {
// TODO Auto-generated method stub
return this.bookDao.selectByPrimaryKey(bookId);
}
}
5、写一个Controller:
package com.controller;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.service.IBookService;
@Controller
public class EbookController {
@Resource
private IBookService bookService = null;
@RequestMapping(value="/b.do")
public void addrShow(HttpServletResponse response) throws IOException{
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(JSONObject.toJSONString(bookService.getBookById("59cc3a7f3ea44b15c1f92ac0")).toString());
}
}
测试一下。结果可以在本地绝对路径生成日志,控制台也能打印日志。推荐阅读
-
【Spring in action】SSM接入log4j日志
-
详解SSM框架下结合log4j、slf4j打印日志
-
ssm框架集成时,在spring配置文文件中集成mybatis时,在sqlSessionFactory中的属性configuration配置日志出错
-
Spring回顾之四 —— 使用Log4j,添加日志功能
-
Spring回顾之四 —— 使用Log4j,添加日志功能
-
在Spring Boot中如何使用log4j记录日志详解
-
Spring boot配置log4j输出日志
-
Spring Boot配置log4j记录日志
-
spring boot日志及Log4j日志配置
-
在Spring Boot中如何使用log4j记录日志详解