帮助文档:
https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-feature-logging
log4j.properties转换logback.xml
https://logback.qos.ch/translator/ https://logback.qos.ch/manual/index.html
转换前代码:
### 设置###log4j.rootLogger = debug,stdout,D,E### 输出信息到控制抬 ###log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n### 输出DEBUG 级别以上的日志到=E://logs/error.log ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = E://logs/log.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### 输出ERROR 级别以上的日志到=E://logs/error.log ###log4j.appender.E = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.File =E://logs/error.log log4j.appender.E.Append = truelog4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayoutlog4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
1.添加pom.xml的依赖
4.0.0 top.ytheng springboot-demo 0.0.1 jar org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-devtools true true org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 runtime mysql mysql-connector-java runtime com.alibaba druid 1.1.6 myspringboot org.springframework.boot spring-boot-maven-plugin maven-compiler-plugin
2.添加lockback-spring.xml配置文件
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n app_log/log/log-error-%d{yyyy-MM-dd}.%i.log 30 1GB 2MB true ===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n utf-8 ERROR ACCEPT DENY
3.添加控制器
package top.ytheng.demo.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/v1/log")public class LogController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @GetMapping("/test") public Object test() { logger.debug("this is debug level"); logger.info("this is info level"); logger.warn("this is warn level"); logger.error("this is error level"); return "success"; }}
4.添加启动类
package top.ytheng.demo;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication //等于下面3个//@SpringBootConfiguration//@EnableAutoConfiguration//@ComponentScanpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
5.右键项目Rum As启动,访问地址
http://localhost:8080/api/v1/log/test
如图:
另附:
转载:
https://www.cnblogs.com/xm-02/p/9109563.html