操作实践: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属性值就是控制变更日志系统配置后是否将这些变化信息打印出来。
完。
操作实践:Java桌面程序实现日志级别热修改的更多相关文章
- 结合BeautyEye开源UI框架实现的较美观的Java桌面程序
BeautyJavaSwingRobot 结合BeautyEye开源UI框架实现的较美观的Java桌面程序,主要功能就是图灵机器人和一个2345网站万年历的抓取.... 挺简单而且实用的一个项目,实现 ...
- java桌面程序打包教程
首先打包成j可执行的jar文件. . 接下来找到自己生成jar文件的路径就可以看到jar文件了,我的是在桌面: 在桌面新建一个文件夹(名字随便取,一般去项目名字) 上面是我取的文件夹名字,然后把资料文 ...
- Java桌面程序打包成exe可执行文件
前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...
- Log4j配置记录(特定java包/类的日志级别控制)
最近使用log4j,关于日志级别的如何配置生效百思不得其解,花了些时间,误打误撞终于整了,记录一下,备忘. 注意: 1.图中的2(log4j.logger.com.taobao)限制级别最高,它直接指 ...
- Java 为程序创建日志系统
使用JAVA创建日志系统有两种方法 1.使用log4j操作日志文件 2.使用系统重定向输出日志信息 方法1:使用log4j操作日志文件(可使用jar或者xml) 步骤1:下载log4j.jar 下载地 ...
- 《Java程序代理器》- java桌面程序运行的前端启动框架
虽说让java直接在桌面运行,有很多方法,但最简单的还是有个exe双击执行 要java执行就得有虚拟机,但原本的虚拟机文件体积太大,不方便随同打包,精简的虚拟机功能又不全,指不定什么时候报错 所以正规 ...
- 公布Java桌面程序
我拿了一份桌面工具的开源码,修改动改,在elipse上执行.感觉良好.但到了公布应用程序,就傻眼了. 我竟然不知道咋公布! 呵呵,不愧是Java小白. 假设是微软阵营,直接就编译成exe了. 但jav ...
- 发布Java桌面程序
我拿了一份桌面工具的开源代码,修修改改,在elipse上运行,感觉良好,但到了发布应用程序,就傻眼了.我居然不知道咋发布! 呵呵,不愧是Java小白! 如果是微软阵营,直接就编译成exe了.但java ...
- 制作Java桌面程序的一键安装包
一.简介 这个打包程序主要包含了对Java程序的普通打包.对程序的管理员权限设置.因为自己打包的时候要求程序在32位操作系统和64位操作系统下都能使用,所以有些打包步骤和设置都不相同.打包过程中主要使 ...
随机推荐
- JAVA Random 详解
Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范 ...
- ArcGIS二次开发的几种方式
1.ArcEngine开发 二次开发的常用方式,开发提供接口齐全,功能强大,比较成熟.但是,开发的软件使用需要指定版本的运行环境才能运行. 2.Addin开发 二次开发与ArcMap嵌入,开发方便,可 ...
- B. Misha and Changing Handles
B. Misha and Changing Handles time limit per test 1 second memory limit per test 256 megabytes input ...
- Idea--使用Idea调试设置
参考 https://blog.csdn.net/yyjava/article/details/81453748 关闭一些Idea默认设置,否则懵逼到爆炸.. 1.关闭集合类视图 2.关闭watch视 ...
- PHP数据库连接
<?php //设置编码格式 header("Content-type:text/html;charset=utf-8"); /** * Created by PhpStor ...
- 048-PHP定义常量
<?php define('NUM',123); //定义常量NUM echo NUM; //输出NUM的值 define('STR','ABC',TRUE); //定义常量STR并设置大小写不 ...
- 147-PHP strip_tags函数,剥去字符串中的 HTML 标签(一)
<?php $html=<<<HTM <title>PHP输出HTML代码</title> <body> <a href=#>转 ...
- 066-PHP通过函数名调用函数
<?php function hello(){ //定义函数 echo '<br />Hello!<br />'; } function hellophp(){ //定义 ...
- 056-for循环中continue的使用
<?php ; $i <= ; $i++) { //for循环输出数值 == ) //判断变量是否为3的整数倍 { continue;//跳过本次循环剩余语句 } echo "$ ...
- APIO 2010 特别行动队 斜率优化DP
Description 你有一支由 n 名预备役士兵组成的部队,士兵从 1 到 n 编号,要将他们拆分 成若干特别行动队调入战场.出于默契的考虑,同一支特别行动队中队员的编号 应该连续,即为形如 (i ...