springmvc整合slf4jlog4j记录文本日志

Posted gossip

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc整合slf4jlog4j记录文本日志相关的知识,希望对你有一定的参考价值。

一、配置pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<properties>
        <slf4j.version>1.7.12</slf4j.version>
    </properties>
 
<!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
 
    <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>


二、resource文件夹添加log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Output pattern : date [thread] priority category - message
log4j.rootLogger=INFO, Console,RollingFile
 
#Console(write to console)
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c] - %m%n
  
 
#RollingFile(write to file /logs/hr.log)
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=C:/logs/hr.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d %-5p [%c] - %m%n
 
log4j.logger.com.ibatis=DEBUG 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO 
log4j.logger.java.sql.Connection=INFO 
log4j.logger.java.sql.Statement=INFO 
log4j.logger.java.sql.PreparedStatement=INFO 
log4j.logger.java.sql.ResultSet=INFO


三、写日志

1
2
3
protected static Logger logger = LoggerFactory.getLogger(LoginSevice.class);
 
logger.info("helloMvc-Service");


以上是关于springmvc整合slf4jlog4j记录文本日志的主要内容,如果未能解决你的问题,请参考以下文章

Spring+SpringMVC+MyBatis+Maven框架整合

UEditor之——与SpringMVC整合

springmvc学习笔记-springmvc整合mybatis之service

(Spring+SpringMVC+MyBatis)SSM三大框架整合

SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)

Log4J 系列:log4j 整合SpringMVC+MyBatis 实现日志记录(01)