最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】给Spring Boot的java项目中加上输出日志到文件中

Java crifan 247浏览 0评论
折腾:
【未解决】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
<?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
<?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
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);
...
效果:

转载请注明:在路上 » 【已解决】给Spring Boot的java项目中加上输出日志到文件中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.115 seconds, using 20.72MB memory