操作实践: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位操作系统下都能使用,所以有些打包步骤和设置都不相同.打包过程中主要使 ...
随机推荐
- 014、Java中byte自动转型的操作
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- JS: 图片轮播模板——左右移动,点击编码移动,自动轮播
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- leetcode1162 As Far from Land as Possible
""" Given an N x N grid containing only values and , represents water and represents ...
- P 1038 统计同成绩学生
转跳点:
- JQuery 动画实现
$(this.div_wrong).show().css({width:"0px", height:"0px"}) .animate({width:&qu ...
- 将数据写入已有的excel文件
/** * 将反馈结果写入excel中 * */ public static void writeExcelResult(String url,List<Integer> result) ...
- Python基础笔记:使用dict和set
dict 就和 C语言中的 map 的作用一样.查找非常快,以空间换时间! dict的使用: >>> d={'Mike':66,'Bob':77,'John':88} #定义一个di ...
- Oracle Exadata 学习笔记之核心特性Part1
近年来,国内众多厂商都有一体机的产品,不过更多都是围绕硬件本身的堆砌和优化,那么这些产品和Oracle一体机最大的区别在哪里呢?最近读了李亚的<Oracle Exadata技术详解>,系统 ...
- HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律
一开始看这个题目以为是个裸的矩阵快速幂的题目, 后来发现会超时,超就超在 M = C^(N*N). 这个操作,而C本身是个N*N的矩阵,N最大为1000. 但是这里有个巧妙的地方就是 C的来源其实 ...
- 设置此div的子元素居中显示
下面样式设置到父div上: .modal { display: flex; align-items: center; /*竖直居中 垂直居中*/ justify-content: center; /* ...