zoukankan      html  css  js  c++  java
  • 操作实践:Java桌面程序实现日志级别热修改

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635

    定位问题的时候往往需要动态修改日志级别并且不能影响业务的正常运行,也就是不能重启应用,此时就要使用到动态日志配置文件加载技术。
    如果使用的是logback,总的方法其实很简单,直接在logback.xml配置文件中的开头设置

    <configuration scan="true" scanPeriod="10 seconds">
    

    即可实现热加载。对于非java web程序,作者发现网上大部分例子很少给出一个真实可用的例子,这里经过自己实践加上阅读源码解决过程中遇到的问题,直接把可实现这种场景的例子展现出来。之所以作者不喜欢用java后端例子的原因就是后端程序经常扯进来太多不相关的东西,比如Spring、Tomcat,不够桌面应用程序纯粹,最简单的桌面程序往往就会告诉我们,只需要这些组件就足够了。
    直接上源码,这里并不探讨中间遇到的问题。
    Java源码

    package com.deskapp.testlogbackdynamicload;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class TestLogbackDynamicLoad {
        static {
            //指定配置文件加载路径
            System.setProperty("logback.configurationFile", "file:./src/main/java/com/deskapp/testlogbackdynamicload/logbackdynamicload.xml");
            LOGGER = LoggerFactory.getLogger(TestLogbackDynamicLoad.class);
        }
    
        public static Logger LOGGER;
    
        public static void main(String[] args)
                throws InterruptedException {
            do {
                LOGGER.trace("this is trace logback log");
                LOGGER.debug("this is debug logback log");
                LOGGER.info("this is info logback log");
                LOGGER.warn("this is warn logback log");
                LOGGER.error("this is error logback log");
                Thread.sleep(5000);
            } while (true);
        }
    }
    

    日志系统配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="5 seconds" debug="false">
        <property name="log.path" value="D:\dynamic_log_level.log"/>
        <appender name="logfileRun" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${log.path}</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${log.base.run}_%d{yyyyMMdd}-%i.log.zip
                </FileNamePattern>
                <MaxHistory>15</MaxHistory>
                <TimeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <MaxFileSize>50MB</MaxFileSize>
                </TimeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder>
                <pattern>[deskapp]|%-20(%date|[%thread])|%-1level| %msg [%file:%line]%n
                </pattern>
            </encoder>
        </appender>
        <root level="ERROR">
            <appender-ref ref="logfileRun"/>
        </root>
    </configuration>
    

    maven工程依赖如下

     <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.api.version}</version>
        </dependency>
    </dependencies>
    

    程序运行过程中动态修改leve级别,日志文件中的打印如下。

    [deskapp]|2018-10-14 02:22:52,305|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:22:57,305|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:23:02,305|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:23:07,320|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:23:12,336|[main]|TRACE| this is trace logback log [TestLogbackDynamicLoad.java:18]
    [deskapp]|2018-10-14 02:23:12,337|[main]|DEBUG| this is debug logback log [TestLogbackDynamicLoad.java:19]
    [deskapp]|2018-10-14 02:23:12,337|[main]|INFO| this is info logback log [TestLogbackDynamicLoad.java:20]
    [deskapp]|2018-10-14 02:23:12,337|[main]|WARN| this is warn logback log [TestLogbackDynamicLoad.java:21]
    [deskapp]|2018-10-14 02:23:12,337|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:23:17,338|[main]|TRACE| this is trace logback log [TestLogbackDynamicLoad.java:18]
    [deskapp]|2018-10-14 02:23:17,338|[main]|DEBUG| this is debug logback log [TestLogbackDynamicLoad.java:19]
    [deskapp]|2018-10-14 02:23:17,338|[main]|INFO| this is info logback log [TestLogbackDynamicLoad.java:20]
    [deskapp]|2018-10-14 02:23:17,338|[main]|WARN| this is warn logback log [TestLogbackDynamicLoad.java:21]
    [deskapp]|2018-10-14 02:23:17,338|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    [deskapp]|2018-10-14 02:23:22,352|[main]|TRACE| this is trace logback log [TestLogbackDynamicLoad.java:18]
    [deskapp]|2018-10-14 02:23:22,352|[main]|DEBUG| this is debug logback log [TestLogbackDynamicLoad.java:19]
    [deskapp]|2018-10-14 02:23:22,352|[main]|INFO| this is info logback log [TestLogbackDynamicLoad.java:20]
    [deskapp]|2018-10-14 02:23:22,352|[main]|WARN| this is warn logback log [TestLogbackDynamicLoad.java:21]
    [deskapp]|2018-10-14 02:23:22,352|[main]|ERROR| this is error logback log [TestLogbackDynamicLoad.java:22]
    

    这种修改配置文件不重启进程实现修改日志级别的方法在后台调试程序时是非常有用的。
    另外,说说配置文件中第2行中debug="false"改为debug="true"的控制台打印。如下。

    02:22:52,033 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [file:./src/main/java/com/deskapp/testlogbackdynamicload/logbackdynamicload.xml] at [file:./src/main/java/com/deskapp/testlogbackdynamicload/logbackdynamicload.xml]
    02:22:52,163 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
    02:22:52,169 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:./src/main/java/com/deskapp/testlogbackdynamicload/logbackdynamicload.xml] 
    02:22:52,170 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 5 seconds
    02:22:52,174 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
    02:22:52,190 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [logfileRun]
    02:22:52,261 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@872627152 - Will use zip compression
    02:22:52,261 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@872627152 - Will use the pattern log.base.run_IS_UNDEFINED_%d{yyyyMMdd}-%i.log for the active file
    02:22:52,273 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@47fd17e3 - The date pattern is 'yyyyMMdd' from file name pattern 'log.base.run_IS_UNDEFINED_%d{yyyyMMdd}-%i.log.zip'.
    02:22:52,273 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@47fd17e3 - Roll-over at midnight.
    02:22:52,273 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@47fd17e3 - Setting initial period to Sun Oct 14 02:19:36 CST 2018
    02:22:52,273 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@47fd17e3 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
    02:22:52,273 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@47fd17e3 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
    02:22:52,273 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
    02:22:52,289 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[logfileRun] - Active log file name: D:dynamic_log_level.log
    02:22:52,289 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[logfileRun] - File property is set to [D:dynamic_log_level.log]
    02:22:52,289 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
    02:22:52,289 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [logfileRun] to Logger[ROOT]
    02:22:52,289 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
    02:22:52,289 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@7cdbc5d3 - Registering current configuration as safe fallback point
    
    02:23:12,172 |-INFO in ReconfigureOnChangeTask(born:1539454972167) - Detected change in configuration files.
    02:23:12,172 |-INFO in ReconfigureOnChangeTask(born:1539454972167) - Will reset and reconfigure context named [default]
    02:23:12,179 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:./src/main/java/com/deskapp/testlogbackdynamicload/logbackdynamicload.xml] 
    02:23:12,180 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 5 seconds
    02:23:12,180 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
    02:23:12,180 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [logfileRun]
    02:23:12,187 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@545251795 - Will use zip compression
    02:23:12,188 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@545251795 - Will use the pattern log.base.run_IS_UNDEFINED_%d{yyyyMMdd}-%i.log for the active file
    02:23:12,188 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@690de2a5 - The date pattern is 'yyyyMMdd' from file name pattern 'log.base.run_IS_UNDEFINED_%d{yyyyMMdd}-%i.log.zip'.
    02:23:12,188 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@690de2a5 - Roll-over at midnight.
    02:23:12,189 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@690de2a5 - Setting initial period to Sun Oct 14 02:23:12 CST 2018
    02:23:12,189 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@690de2a5 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
    02:23:12,189 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@690de2a5 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
    02:23:12,189 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
    02:23:12,192 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[logfileRun] - Active log file name: D:dynamic_log_level.log
    02:23:12,192 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[logfileRun] - File property is set to [D:dynamic_log_level.log]
    02:23:12,192 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to TRACE
    02:23:12,192 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [logfileRun] to Logger[ROOT]
    02:23:12,193 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
    02:23:12,193 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3f46c96a - Registering current configuration as safe fallback point
    

    其实debug属性值就是控制变更日志系统配置后是否将这些变化信息打印出来。
    完。

  • 相关阅读:
    2019/5/15 写题总结
    CodeForces 804C Ice cream coloring
    CodeForces 367 C Sereja and the Arrangement of Numbers 欧拉回路
    CodeForces 464 B Restore Cube
    CodeForces 402 E Strictly Positive Matrix
    CodeForces 628 D Magic Numbers 数位DP
    CodeForces 340E Iahub and Permutations 错排dp
    CodeForces 780 E Underground Lab
    BZOJ 1010 [HNOI2008]玩具装箱toy 斜率优化dp
    CodeForces 715B Complete The Graph 特殊的dijkstra
  • 原文地址:https://www.cnblogs.com/xsl-thumb-rfcs/p/9941594.html
Copyright © 2011-2022 走看看