日志框架log4j的小Demo
目录结构
依赖
org.springframework.boot
spring-boot-starter-web
2.1.3.RELEASE
log4j
log4j
1.2.17
DemoController.java
package com.demo.web;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class DemoController {
private Logger logger = Logger.getLogger(DemoController.class);
@GetMapping("/getLog")
public String getLog(String info) {
logger.info(new Date() + "调用了方法");
return new Date() + ":" + info;
}
}
DemoApplication.java
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.properties
server.port=8080
log4j.properties
log4j.rootCategory=DEBUG,CONSOLE,LOGFILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=-%p-%d{yyyy/MM/dd HH:mm:ss,SSS}-%l-%L-%m%n
log4j.appender.LOGFILE=org.apach服务器托管网e.log4j.FileAppender
log4j.appender.logFile.Threshold=DEBUG
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.LOGFILE.Append=true
#文件路径
log4j.appender.LOGFILE.File=/Users/canrenehan/Desktop/mylog.log
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=-%p-%d{yyyy/MM/dd HH:mm:ss,SSS}-%l-%L-%m%n
项目启动时,会自动在/Users/canreneha服务器托管网n/Desktop路径下创建mylog.log文件,当接口被访问时,日志会被打印在控制台同时写入/Users/canrenehan/Desktop/mylog.log文件中
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
本文首发自公粽hao「林行学长」,欢迎来撩,免费领取20个求职工具资源包。 了解校招、分享校招知识的学长来了! 大家有没有这样的感受:面试的时候和面试官聊得火热朝天,气味相投。 面完觉得自己稳了,已经在畅想入职事宜了,结果一封感谢信让人瞬间清醒。 不少同学应该…