简体   繁体   English

用于记录的Spring Boot应用程序属性

[英]spring boot application properties for logging

I've seen many examples of configuring logging in Spring Boot, but what I look for is the simplest way to get the job done modifying this pattern in my application.properties: 我已经看到了许多在Spring Boot中配置日志记录的示例,但是我所寻找的是完成修改我的application.properties中的模式的最简单方法:

logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
logging.file=/Users/alessandroargentieri/Desktop/try/application.log

This works pretty fine but what I would like to get is set a MB limit for the single file and set that every day I want to have a different log file (with the date in the filename ). 这工作得很好,但是我想为单个文件设置MB限制 ,并设置每天我想要一个不同的日志文件( 日期在filename中 )。

Is possible to get this adding some lines in the application.properties without using XML or JSON files? 是否可以在不使用XML或JSON文件的情况下在application.properties中添加一些行呢?

I think you need to override the default logging config with your own custom configuration. 我认为您需要使用自己的自定义配置覆盖默认的日志记录配置。 Spring boot provide logging support with minimum configuration and it's limited. Spring Boot以最少的配置提供了日志支持,并且它是有限的。 It uses internally base.xml file which includes the below file-appender.xml: 它在内部使用base.xml文件,该文件包括以下file-appender.xml:

<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>

As you can see only few properties can be injected. 如您所见,只能注入很少的属性。

You can go through the below link to setup your custom configuration: 您可以通过以下链接来设置自定义配置:

https://dzone.com/articles/configuring-logback-with-spring-boot https://springframework.guru/using-logback-spring-boot/ https://dzone.com/articles/configuring-logback-with-spring-boot https://springframework.guru/using-logback-spring-boot/

No. The easiest way to do that is with an extra xml file. 否。最简单的方法是使用额外的xml文件。

I'm using logback.xml 我正在使用logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<property name="DEV_HOME" value="c:/logs" />

<appender name="FILE-AUDIT"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${DEV_HOME}/debug.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>
            ${DEV_HOME}/debug.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<logger name="ar.com" level="debug" additivity="false">
    <appender-ref ref="FILE-AUDIT" />
</logger>

<root level="error">
    <appender-ref ref="FILE-AUDIT" />
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM