国外建设网站情况,桂林北站怎么去阳朔,东莞网站建设品牌公司,医药cms是什么意思原文地址#xff1a;http://blog.jboost.cn/trick-logback-prop.html
当使用logback来记录Web应用的日志时#xff0c;我们通过在logback.xml中配置appender来指定日志输出格式及输出文件路径#xff0c;这在一台主机或一个文件系统上部署单个实例没有问题#xff0c;但是…原文地址http://blog.jboost.cn/trick-logback-prop.html
当使用logback来记录Web应用的日志时我们通过在logback.xml中配置appender来指定日志输出格式及输出文件路径这在一台主机或一个文件系统上部署单个实例没有问题但是如果部署多个实例比如通过容器的方式多个实例同时往同一文件写日志可能就会引起问题。这时可以将每个实例的日志文件加以区分如IP或UUID或两者结合的形式。这其实就涉及如何在logback.xml中自定义动态属性的问题。
可以有4种方式来实现logback.xml中获取自定义变量值
通过设置环境变量或传递系统属性比如在程序启动时通过-D传递的方式两者是可以直接在logback.xml中通过 ${变量名} 获取的。自定义logback.xml的加载时机在其加载前将需要设置的属性注入到logback的context中这种方式相对复杂本文不讨论。通过实现PropertyDefiner接口来提供属性值设置通过实现LoggerContextListener接口来设置属性值
第一种方式简单但不能通过程序生成属性值
需要在spring-boot启动类中加入设置环境变量的代码如下 修改logback.xml中的代码如下 第二种方式稍显复杂本文主要介绍后两种方式
PropertyDefiner方式
首先定义一个类实现PropertyDefiner接口可以通过继承PropertyDefinerBase会更方便
import ch.qos.logback.core.PropertyDefinerBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; /\*\*\*\* 将本地IP拼接到日志文件名中以区分不同实例避免存储到同一位置时的覆盖冲突问题\* Author ronwxy\* Date 2019/8/20 16:17 \*/
public class IPLogDefiner extends PropertyDefinerBase { private static final Logger LOG LoggerFactory.getLogger(IPLogDefiner.class); private String getUniqName() {String localIp \ null; try {localIp \ InetAddress.getLocalHost().getHostAddress();} catch (UnknownHostException e) {LOG.error(fail to get ip..., e);}String uniqName \ UUID.randomUUID().toString().replace(-, ); if (localIp ! null) {uniqName \ localIp - uniqName;} return uniqName;}Override public String getPropertyValue() { return getUniqName();}
}然后在logback.xml中添加 define 配置指定属性名本例中为localIP及获取属性值的实现类这样就可以在配置中通过 ${localIP}来引用该属性值了。在实现方法 getPropertyValue 中返回你需要生成的值本例中是返回 本地IP-UUID 的形式。
configurationdefine namelocalIP classcn.jboost.common.IPLogDefiner/appender nameinterfaceLogFileclass\ch.qos.logback.core.rolling.RollingFileAppenderencodingUTF-8/encodingFileD:\\\\logs\\\\elk\\\\interface\-${localIP}.log/Filefilter class\ch.qos.logback.classic.filter.ThresholdFilterlevelINFO/level/filter # 省略了其它配置LoggerContextListener方式
定义一个实现LoggerContextListener接口的类在start方法中将需要设置的属性设置到logback的Context中
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.LoggerContextListener; import ch.qos.logback.core.Context; import ch.qos.logback.core.spi.ContextAwareBase; import ch.qos.logback.core.spi.LifeCycle; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; /\*\*\*\* 第二种实现方式\* Author ronwxy\* Date 2019/8/20 18:45 \*/
public class LoggerStartupListener extends ContextAwareBase implements LoggerContextListener, LifeCycle { private boolean started false;Override public void start() { if (started) { return;}Context context \ getContext();context.putProperty(localIP, getUniqName());started \ true;} private String getUniqName() {String localIp \ null; try {localIp \ InetAddress.getLocalHost().getHostAddress();} catch (UnknownHostException e) { //LOG.error(fail to get ip..., e);}String uniqName \ UUID.randomUUID().toString().replace(-, ); if (localIp ! null) {uniqName \ localIp - uniqName;} return uniqName;} //省略了其它函数然后在logback.xml中配置如上监听器类这样就可以通过 ${localIP} 获取到上面 context.putProperty(localIP, getUniqName()); 设置的值了。
configuration!--define namelocalIP class\com.cnbot.common.IPLogDefiner/--contextListener classcn.jboost.common.LoggerStartupListener/define namelocalIP class\com.cnbot.common.IPLogDefiner/appender nameinterfaceLogFileclass\ch.qos.logback.core.rolling.RollingFileAppenderencodingUTF-8/encodingFileD:\\\\logs\\\\elk\\\\interface\-${localIP}.log/Filefilter class\ch.qos.logback.classic.filter.ThresholdFilterlevelINFO/level/filter # 省略了其它配置这种方式能设置任意个数的属性值比前一种方式灵活。
总结
在logback.xml中获取自定义属性值主要是需要在加载前将对应的属性值进行设置这样加载时才能有效获取。本文虽是自定义日志文件名称但不局限于此所有需要动态获取的变量都可以按这种方式实现。