logback.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="false" scanPeriod="60000" debug="false"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </layout> </appender> <appender name="eventFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--<File>${catalina.base}/logs/BigeyeCenter/BigeyeCenter.log</File>--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 --> <FileNamePattern>${catalina.home}/logback-logs/logback.%d{yyyy-MM-dd}.log</FileNamePattern> <!--<FileNamePattern>D:/AssessmentLogs/logback.%d{yyyy-MM-dd}.log</FileNamePattern>--> <!-- keep 30 days' worth of history --> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern> <charset>UTF-8</charset> <!-- 此处设置字符集 --> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT"/> <appender-ref ref="eventFile"/> </root> </configuration>
webTest
package com.kikyo.admin.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.Date; @Controller @RequestMapping("login") public class LoginController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("hello") public String hello() { logger.error("error " + new Date()); return "admin/login/hello"; } }
Test
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Test { static Logger logger = LoggerFactory.getLogger(Object.class); public static void main(String[] args) { logger.trace("=====trace====="); logger.debug("=====debug====="); logger.info("=====info====="); logger.warn("=====warn====="); logger.error("=====error====="); } }
Pom.xml
<!-- logback+slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.0</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>0.9.28</version> <type>jar</type> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.28</version> <type>jar</type> </dependency>