Spring Boot应用程序中的双日志文件 [英] Double log files in spring boot application

查看:53
本文介绍了Spring Boot应用程序中的双日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为service1的spring boot应用程序,它使用以下属性文件层次结构来配置日志:

I have a spring boot application, called service1, which uses the following hierarchy of property files to configure the logs:

bootstrap.yml具有:

bootstrap.yml has:

  • spring.application.name = service1

application.yml具有:

application.yml has:

  • logging.file:/app/logs/$ {spring.application.name} .log
  • logging.level.root:信息
  • logging.level.com.myproject.api:TRACE

使用的日志框架是Slf4j,通过Lombok的@Sfl4j注解注入到每个类中.spring boot 声称对此有默认支持,确实如此,但是当应用程序启动时,我在 /app/logs 中看到的是两个日志文件:

The log framework used is Slf4j, which is injected into every class by using Lombok's @Sfl4j annotation. spring boot claims to have default support for this, which it does, but what I see in /app/logs when the application starts are two logfiles:

  • /app/logs/myservice1.log :此文件具有正确的位置和名称,但日志级别不是trace(仅信息)
  • /app/logs/myservice1-service.log :此文件名称错误( -service 来自何处?),但日志级别正确.我注意到该文件的名称与该代码所在的Git中的目录名称匹配,并且它也是IntelliJ中此代码段的模块名称.我无法解释如何在春季之前拾取"此名称,并在运行编译后的代码时将其用作日志文件名?!
  • /app/logs/myservice1.log : this file has the correct location and name, but the loglevel is not trace (only info)
  • /app/logs/myservice1-service.log: this file has the wrong name (where does the -service come from?) but it has the correct loglevel. I noticed that the name of this file matches the name of the directory in Git where this code resides, and it is also the name of the module of this piece of code in IntelliJ. I cannot explain how this name can be "picked up" by spring and somehow be used for a log filename when running the compiled code?!

我在pom文件中使用了以下声明来进行春季启动,并且我怀疑也许除了我声明的Sfl4j之外,一些默认的logback东西还在创建日志文件.有什么办法可以让我只有一个日志文件,该文件具有我指定的名称和正确的日志级别?也许我需要排除依赖项?

I used the following declaration in my pom file to get spring boot, and I suspect perhaps some default logback stuff is also creating a logfile in addition to the Sfl4j I declared. Is there any way I can have only one logfile, with the name I specifiede and the correct loglevel? Perhaps I need to exclude a dependency?

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

推荐答案

将以下内容添加到属性文件中

add the folowing to properties file

logging.config=logback.xml

然后创建一个logback.xml文件.在这里您可以放置​​任何您喜欢的配置,例如:

Then create a logback.xml file. Here you can place any config you like, for example:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true">

<!-- Logging to system file -->
<appender name="system-file" class="ch.qos.logback.core.rolling.RollingFileAppender">

    <!-- output filename -->
    <file>logs/app.log</file>


    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>logs/app_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>500MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
    <pattern>%d{dd-MM-yyyy HH:mm:ss} - %c [%t] %-5p - %m%n</pattern>
    </encoder>
</appender>


<!-- overide levels for specific loggers-->
<logger name="com.myproject.api" level="TRACE" />


<root level="INFO">
    <appender-ref ref="system-file"/>
</root>
</configuration>

这篇关于Spring Boot应用程序中的双日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆