折腾:
【未解决】VSCode中用Java的Spring Boot搭建智能电力系统后端框架
期间,参考之前的:
【已解决】java的log4j2中没有生成File类型的log文件
去加上日志,输出日志到文件
然后遇到:
【已解决】spring boot中已配置log4j2有console的log输出但是没有生成日志文件
但是同时能看到很多log,感觉好像不太对,哪些方面除了问题似的。
不过后来发现貌似是没问题的。
然后不过需要去优化配置:
【已解决】java中log4j2中配置日志的level不同:console是INFO而file是DEBUG
【总结】
最后,给spring boot的log4j2的配置如下:
src/server/xxx/xxx/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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <?xml version = "1.0" encoding = "UTF-8" ?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 < / modelVersion> <parent> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - starter - parent< / artifactId> <version> 2.2 . 2.RELEASE < / version> <relativePath / > <! - - lookup parent from repository - - > < / parent> <groupId>com.crifan< / groupId> <artifactId>xxx< / artifactId> <version> 0.0 . 1 - SNAPSHOT< / version> <name>xxx< / name> <description>Demo project for Spring Boot< / description> <properties> <java.version> 1.8 < / java.version> <log4j2.version> 2.13 . 0 < / log4j2.version> < / properties> <dependencies> <dependency> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - starter - web< / artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - starter - logging< / artifactId> < / exclusion> < / exclusions> < / dependency> <dependency> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - starter - log4j2< / artifactId> < / dependency> <dependency> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - devtools< / artifactId> <scope>runtime< / scope> <optional>true< / optional> < / dependency> <dependency> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - starter - test< / artifactId> <scope>test< / scope> <exclusions> <exclusion> <groupId>org.junit.vintage< / groupId> <artifactId>junit - vintage - engine< / artifactId> < / exclusion> < / exclusions> < / dependency> <dependency> <groupId>com.jayway.jsonpath< / groupId> <artifactId>json - path< / artifactId> <scope>test< / scope> < / dependency> <dependency> <groupId>org.apache.logging.log4j< / groupId> <artifactId>log4j - api< / artifactId> <version>${log4j2.version}< / version> < / dependency> <dependency> <groupId>org.apache.logging.log4j< / groupId> <artifactId>log4j - core< / artifactId> <version>${log4j2.version}< / version> < / dependency> <dependency> <groupId>org.apache.logging.log4j< / groupId> <artifactId>log4j - slf4j - impl< / artifactId> <version>${log4j2.version}< / version> < / dependency> <dependency> <groupId>org.slf4j< / groupId> <artifactId>slf4j - api< / artifactId> <version> 1.7 . 30 < / version> < / dependency> < / dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot< / groupId> <artifactId>spring - boot - maven - plugin< / artifactId> < / plugin> < / plugins> < / build> < / project> |
src/server/xxx/xxx/src/main/resources/log4j2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version= "1.0" encoding= "UTF-8" ?> <Configuration> <Appenders> <File name= "FileLog" fileName= "xxx.log" append= "false" > <PatternLayout> <Pattern>%d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F%L - %m%n</Pattern> </PatternLayout> </File> <Console name= "ConsoleLog" target= "SYSTEM_OUT" > <PatternLayout pattern= "%d %-5p %F:%L - %m%n" /> </Console> </Appenders> <Loggers> <Root level= "debug" > <AppenderRef ref= "FileLog" level= "debug" /> <AppenderRef ref= "ConsoleLog" level= "info" /> </Root> </Loggers> </Configuration> |
代码:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/GreetingController.java
1 2 3 4 5 6 7 8 9 10 | import org.slf4j.LoggerFactory; @RestController public class GreetingController { private static Logger logger = LoggerFactory.getLogger(GreetingController. class ); @PostMapping... public Greeting addMember(@RequestBody Map <String, Object > payload) { logger.info( "post greeting: payload={}" , payload); ... |
效果:
