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

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


当前回答

设置根日志级别的正确方法是使用logging.level.root属性。参见文档,自最初提出这个问题以来,文档已经更新。

例子:

logging.level.root=WARN

其他回答

如果您使用Spring Boot,那么您可以直接在应用程序中添加以下属性。属性文件设置日志级别, 自定义日志记录模式并将日志存储在外部文件中。

这些是不同的日志级别及其从最小值<<最大值的顺序。

关闭<<致命<<错误<<警告<< info << debug << trace << all

# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace

# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, its won't work.      
logging.file=D:/spring_app_log_file.log

# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

请通过这个链接来更生动地定制您的日志。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

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

如果你想设置更多的细节,请添加一个日志配置文件名“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>

如果使用eclipse IDE,并且您的项目是maven,请记住清理并构建项目以反映更改。

假设应用程序的包名为com.company.myproject。然后,您可以在应用程序中为项目中的类设置日志级别,如下所示。属性文件

loging.level.com.company.myproject = DEBUG

loging.level.org.springframework.web = DEBUG和loging.level.org.hibernate = DEBUG将仅为Spring框架web和Hibernate的类设置日志级别。

用于设置日志文件位置使用

伐木。文件= /home/ubuntu/myproject