Spring Boot 如何在Sping Boot 中禁用logback Console

yrwegjxp  于 2023-11-17  发布在  Spring
关注(0)|答案(7)|浏览(160)

我正在使用Sping Boot 构建一个命令行应用程序。在这样的应用程序中,logback控制台日志记录是不合适的。我如何完全禁用控制台appender,但仍然让文件appender使用默认的Sping Boot 支持?

更新

我在这里创建了一个功能请求,以便更简单地支持此功能:
https://github.com/spring-projects/spring-boot/issues/1612

eqqqjvef

eqqqjvef1#

使用最新的1.3.1 Spring Boot 释放测试。
将logback.xml放在资源文件夹下
logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />

    <!-- Send debug messages to a file "application.log" -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>application.log</file>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>application.%i.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="FILE" />
    </root>
</configuration>

字符串
另外,如果您之前添加了排除项,请确保从“spring-boot-starter”和“spring-boot-starter-web”依赖项中删除spring-boot-starter-logging的排除项。
不需要下面的依赖项

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.2</version>
</dependency>


application.properties

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG

ffscu2ro

ffscu2ro2#

我试着从logback.xml中删除控制台配置.但是,它仍然在控制台中登录.所以我做的是,我只是删除了springboot在日志配置中添加的appender.因此,我们可以在控制台中停止springboot日志记录并单独记录日志文件。在应用程序的末尾添加以下行特定的appender。您的自定义appender不应与这些appender名称中的任何一个匹配。它工作了为我

// get instance of your log4j instance
Logger logger = LogManager.getRootLogger();
logger.removeAppender("CONSOLE"); // stops console logging
logger.removeAppender("LOGFILE"); // stops file logging

字符串

gpnt7bae

gpnt7bae3#

logback-spring.xml中包含file-appender.xml而不是console-appender,并具有以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

字符串
您还需要将logging.file添加到application.properties
这符合 Spring Boot 文档-http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html中提到的内容

wfauudbj

wfauudbj4#

将以下内容添加到您的application.properties

logging.level.root=OFF
spring.main.banner-mode=OFF

字符串

u1ehiz5o

u1ehiz5o5#

这里是logback-spring.xml,您可以在其中启用和禁用控制台日志,以及基于配置文件的文件日志。
我已经注解掉了prod配置文件和默认配置文件的控制台附加器(当没有配置文件活动时)。它将只打印文件中的日志。它将打印到本地,开发等配置文件的控制台和文件。

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

    <!-- include **/appLog*log* in global gitignore file
         to avoid below log files showing up in git -->
    <property name="log.path" value="log"/>
    <property name="log.filename.prefix" value="appLog"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <!-- logs folder created in user.dir folder,
         value is printed by calling System.getProperty("user.dir")
         if you want see this property for a running jvm process, then do the following
          - execute command "jps -lm" and identify the pid of the server process
          - execute command "jcmd <SERVER PROCESS PID> VM.system_properties" to see the properties
    -->

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} || [%thread] || %-5level || %logger{36} || %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/${log.filename.prefix}.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/archive/year_%d{yyyy, aux}/month_%d{MM, aux}/${log.filename.prefix}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <!-- each archived file, size max 10MB -->
            <maxFileSize>10MB</maxFileSize>
            <!-- total size of all archive files, if total size > 1GB, it will delete old archived file -->
            <totalSizeCap>1GB</totalSizeCap>
            <!-- 3653 days (10 years) to keep -->
            <maxHistory>3653</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} || [%thread] || %-5level || %logger{36} || %msg%n</pattern>
        </encoder>
    </appender>

    <springProperty scope="context" name="application_name" source="spring.application.name"/>

    <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>

    <!-- remove log related properties in application.yaml and application-test.yaml files
         of application for below log level setting to be honored -->
    <springProfile name="!(local | dev | test | test_common)">
        <root level="INFO">
<!--        <appender-ref ref="jsonConsoleAppender" />   -->
            <!-- <appender-ref ref="CONSOLE" /> -->
            <appender-ref ref="FILE-ROLLING" />
        </root>
    </springProfile>

    <!-- prod profile specific configuration -->
    <springProfile name="prod | production">
        <root level="WARN">
            <!-- appenders included in the first spring profile tag, if included here,
                 then it will lead to duplicate logs with json format and console format-->
            <!--
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE-ROLLING" />
            -->
        </root>
    </springProfile>

    <springProfile name="local | dev | test | test_common">
        <root level="DEBUG">
<!--    <root level="TRACE"> -->
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE-ROLLING" />
        </root>
    </springProfile>

</configuration>

字符串

6rqinv9w

6rqinv9w6#

使用Sping Boot 1.3.0,我创建了一个文件logback-spring.xml(在src/main/resources中,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <springProfile name="dev">
        <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
        <root level="INFO">
            <appender-ref ref="CONSOLE" />
        </root>
    </springProfile>

    <springProfile name="staging,prod">
        <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
        <root level="INFO">
            <appender-ref ref="FILE" />
        </root>
    </springProfile>
</configuration>

字符串
此外,我在application-staging.propertiesapplication-prod.properties中添加了logging.file属性,以指定文件名。
这将记录到dev的控制台和stagingprod配置文件的文件。

6uxekuva

6uxekuva7#

只需在src/main/resources中添加一个名为logback.xml的文件,内容如下(除了Sping Boot 的源代码中的控制台部分外,逐字复制):

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

    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>

</configuration>

字符串
注意

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>


为了支持从Sping Boot 的logging.filelogging.path设置日志文件,需要。
如果你想做的只是设置一些标准的日志文件,你可以在上面的属性中设置它的路径。

更新(02-04-2015)

在较新版本的Sping Boot 中,您可以轻松地包含Sping Boot 中的base.xml并创建以下logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />

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

更新(15-09-2017)

为了在Sping Boot 1.5.x和2.0.0.M4上运行此功能,我添加了一个名为logback-spring.xml的文件,并将其添加到resources目录中。

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

    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n"/>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>

</configuration>

相关问题