美文网首页
logback配置

logback配置

作者: 清蒸三文鱼_ | 来源:发表于2024-06-05 17:41 被阅读0次

    logback.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true">
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>logs/app.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>logs/app.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="FILE"/>
            <appender-ref ref="STDOUT"/>
        </root>
    </configuration>
    

    pom.xml

    <dependencies>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.3.14</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.3.14</version>
            </dependency>
        </dependencies>
    

    新版本的logback, 如1.14.x和1.15.x对JDK的版本有要求

    测试

        public static void main(String[] args) {
            Logger logger = LoggerFactory.getLogger(Log.class);
            logger.info("Hello, INFO!");
            logger.error("Hello, ERROR!");
            logger.warn("Hello, WARN!");
            System.out.println("Hello, Console!");
        }
    

    从图中可知, out.print的信息并不会输出到FILE中, 需要额外来实现(不建议做更改, 代码上做好规范), 参考System.out.print的输出加到logback的日志文件中

    相关文章

      网友评论

          本文标题:logback配置

          本文链接:https://www.haomeiwen.com/subject/akguqjtx.html