这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)

如何使用application.properties设置日志级别? 日志文件的位置,等等?


当前回答

logging:
  level:
    root: INFO
    com.mycompany.myapp: DEBUG

其他回答

如果你想设置更多的细节,请添加一个日志配置文件名“logback.xml”或“logback-spring.xml”。

在你的应用中。属性文件,输入如下:

logging.config: classpath:logback-spring.xml

在loback-spring.xml中,像这样输入:

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

        <appender name="ROOT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>

            <file>sys.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">


                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/system.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>


        <appender name="BUSINESS_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>TRACE</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>

            <file>business.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/business.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>


        <logger name="{project-package-name}" level="TRACE">
            <appender-ref ref="BUSINESS_APPENDER" />
        </logger>

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

    </configuration>

in spring boot project we can write logging.level.root=WARN but here problem is, we have to restart again even we added devtools dependency, in property file if we are modified any value will not autodetectable, for this limitation i came to know the solution i,e we can add actuator in pom.xml and pass the logger level as below shown in postman client in url bar http://localhost:8080/loggers/ROOT or http://localhost:8080/loggers/com.mycompany and in the body you can pass the json format like below

{
  "configuredLevel": "WARN"
}

在我当前的配置中,我在应用程序中定义了它。Yaml是这样的:

logging:
  level:
    ROOT: TRACE

我正在使用spring-boot:2.2.0.RELEASE。您可以像这样定义任何应该具有TRACE级别的包。

您可以尝试将日志级别设置为DEBUG,它将在启动应用程序时显示所有内容

logging.level.root=DEBUG

根据文档,您可以根据java包拥有不同的日志级别。

 logging.level.com.mypackage.myproject=WARN
 logging.level.org.springframework=DEBUG
 logging.level.root=INFO 

这就意味着

对于您的自定义包com.mypackage.myproject将应用WARN日志级别 对于spring框架包org。将应用springframework DEBUG日志级别 对于每个其他包,将应用INFO日志级别

您还可以将不同的java包分组在一起,并指示系统在一行中对该组的所有包使用相同的日志级别。

在前面的例子中你可以这样做

 logging.level.root=INFO 
 logging.level.org.springframework=DEBUG
 
 logging.group.myCustomGroup = com.mypackage.myproject, com.otherpackage.otherproject, com.newpackage.newproject
 logging.level.myCustomGroup=WARN

这就意味着包裹

com.mypackage.myproject com.otherpackage.otherproject com.newpackage.newproject

是否所有日志级别都应用了WARN