一 slf4j+log4j
1.添加依赖
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency>
2.添加 log4j.properties
# This is the configuring for logging displayed in the Application Server log4j.rootCategory=DEBUG, stdout, rollFile #u63A7u5236u53F0u8F93u51FA log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %t [%5p] %l - <%m> %n #u6587u4EF6u8F93u51FA log4j.appender.rollFile=org.apache.log4j.RollingFileAppender log4j.appender.rollFile.layout=org.apache.log4j.PatternLayout log4j.appender.rollFile.layout.ConversionPattern=%d %t [%5p] %l - <%m> %n log4j.appender.rollFile.File=C:/Users/Administrator/Desktop/war.log log4j.appender.rollFile.MaxFileSize=50MB log4j.appender.rollFile.MaxBackupIndex=20 #u65E5u5FD7 log4j.logger.org.springframework.web=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
3.测试
package com.qi.crm.util.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogTest { private static final Logger LOG = LoggerFactory.getLogger(LogTest.class); public static void main(String[] args) { LOG.info("Hello {}","SLF4J"); } }
二 slf4j+logback
1.添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yun</groupId> <artifactId>kong</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>kong Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.5.RELEASE</spring.version> <druid.version>1.0.5</druid.version> <mysql.version>5.1.40</mysql.version> <slf4j.version>1.7.7</slf4j.version> <logback.version>1.1.2</logback.version> <servlet.version>2.5</servlet.version> <fastjson.version>1.2.8</fastjson.version> <freemarker.version>2.3.23</freemarker.version> <mybatis.version>3.3.1</mybatis.version> <mybatis-spring.version>1.2.5</mybatis-spring.version> <mysql.connector.version>5.1.30</mysql.connector.version> </properties> <dependencies> <!-- springmvc start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- springmvc end --> <!-- logback --> <dependency> <!--主要介绍的是这个jar包,这个包是负责logback随着项目启动的jar包--> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.4</version> </dependency> <!-- slf4j start --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- slf4j end --> </dependencies> <build> <finalName>kong</finalName> </build> </project>
2.配置 logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="20 seconds"> <property resource="log.properties"></property> <property scope="system" name="SYSTEM_NAME" value="${log.system.name}" /> <property scope="system" name="LOG_DIR" value="${log.dir}" /> <property scope="system" name="ROOT_LEVEL" value="${log.root.level}" /> <!-- 系统日志 --> <appender name="ROOT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_DIR}/${SYSTEM_NAME}/system.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/system.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>5000MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 控制台日志 --> <appender name="ROOT_STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="${ROOT_LEVEL}"> <appender-ref ref="ROOT_APPENDER" /> <!-- 系统日志 --> <appender-ref ref="ROOT_STDOUT" /> <!-- 控制台输出 --> <appender-ref ref="LOGSTASH" /> <!-- 日志集中化 --> </root> </configuration>
3.web.xml 中配置监听器 ,也可以不用配置,默认的
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>kong</display-name> <!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>kong</param-value> </context-param> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param> <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:springMvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.添加 log.properties 当然你也可以不写,直接将值写在logback.xml中
log.system.name=bmp log.dir=C:\Users\Administrator\Desktop\logs log.root.level=DEBUG