使用Logback设置property参数方式
作者:yh_1524 发布时间:2022-07-28 01:06:01
标签:Logback,property,参数
Logback设置property参数
更多参数设置查看官方文档
1.方式一:直接配置参数值
<configuration>
<property name="USER_HOME" value="/home/sebastien" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
2.方式二:通过file属性引入参数文件
<configuration>
<!-- 引入项目内的文件指定文件所在的包路径 -->
<property file="src/main/java/chapters/configuration/variables1.properties" />
<!-- 引入项目外的文件指定文件所在的绝对路径 -->
<property file="/home/logback/variables.properties" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
3.方式三:通过resource属性引入参数文件
<configuration>
<!-- 使用classpath的方式引入文件,只需写明文件名即可 -->
<property resource="resource1.properties" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
4.引入文件处理类PropertyAction
文件所在路径:ch.qos.logback.core.joran.action
在begin方法中读取引入的properies文件,如果引入的文件中的参数值需要进行额外的加工处理,可重写覆盖此类,在begin或loadAndSetProperties方法中添加相关逻辑
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.core.joran.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.xml.sax.Attributes;
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;
/**
* This class serves as a base for other actions, which similar to the ANT
* <property> task which add/set properties of a given object.
*
* This action sets new substitution properties in the logging context by name,
* value pair, or adds all the properties passed in "file" or "resource"
* attribute.
*
* @author Ceki Gülcü
*/
public class PropertyAction extends Action {
static final String RESOURCE_ATTRIBUTE = "resource";
static String INVALID_ATTRIBUTES = "In <property> element, either the \"file\" attribute alone, or "
+ "the \"resource\" element alone, or both the \"name\" and \"value\" attributes must be set.";
/**
* Set a new property for the execution context by name, value pair, or adds
* all the properties found in the given file.
*
*/
public void begin(InterpretationContext ec, String localName, Attributes attributes) {
if ("substitutionProperty".equals(localName)) {
addWarn("[substitutionProperty] element has been deprecated. Please use the [property] element instead.");
}
String name = attributes.getValue(NAME_ATTRIBUTE);
String value = attributes.getValue(VALUE_ATTRIBUTE);
String scopeStr = attributes.getValue(SCOPE_ATTRIBUTE);
Scope scope = ActionUtil.stringToScope(scopeStr);
if (checkFileAttributeSanity(attributes)) {
String file = attributes.getValue(FILE_ATTRIBUTE);
file = ec.subst(file);
try {
FileInputStream istream = new FileInputStream(file);
loadAndSetProperties(ec, istream, scope);
} catch (FileNotFoundException e) {
addError("Could not find properties file [" + file + "].");
} catch (IOException e1) {
addError("Could not read properties file [" + file + "].", e1);
}
} else if (checkResourceAttributeSanity(attributes)) {
String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
resource = ec.subst(resource);
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
if (resourceURL == null) {
addError("Could not find resource [" + resource + "].");
} else {
try {
InputStream istream = resourceURL.openStream();
loadAndSetProperties(ec, istream, scope);
} catch (IOException e) {
addError("Could not read resource file [" + resource + "].", e);
}
}
} else if (checkValueNameAttributesSanity(attributes)) {
value = RegularEscapeUtil.basicEscape(value);
// now remove both leading and trailing spaces
value = value.trim();
value = ec.subst(value);
ActionUtil.setProperty(ec, name, value, scope);
} else {
addError(INVALID_ATTRIBUTES);
}
}
void loadAndSetProperties(InterpretationContext ec, InputStream istream, Scope scope) throws IOException {
Properties props = new Properties();
props.load(istream);
istream.close();
ActionUtil.setProperties(ec, props, scope);
}
boolean checkFileAttributeSanity(Attributes attributes) {
String file = attributes.getValue(FILE_ATTRIBUTE);
String name = attributes.getValue(NAME_ATTRIBUTE);
String value = attributes.getValue(VALUE_ATTRIBUTE);
String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
return !(OptionHelper.isEmpty(file)) && (OptionHelper.isEmpty(name) && OptionHelper.isEmpty(value) && OptionHelper.isEmpty(resource));
}
boolean checkResourceAttributeSanity(Attributes attributes) {
String file = attributes.getValue(FILE_ATTRIBUTE);
String name = attributes.getValue(NAME_ATTRIBUTE);
String value = attributes.getValue(VALUE_ATTRIBUTE);
String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
return !(OptionHelper.isEmpty(resource)) && (OptionHelper.isEmpty(name) && OptionHelper.isEmpty(value) && OptionHelper.isEmpty(file));
}
boolean checkValueNameAttributesSanity(Attributes attributes) {
String file = attributes.getValue(FILE_ATTRIBUTE);
String name = attributes.getValue(NAME_ATTRIBUTE);
String value = attributes.getValue(VALUE_ATTRIBUTE);
String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
return (!(OptionHelper.isEmpty(name) || OptionHelper.isEmpty(value)) && (OptionHelper.isEmpty(file) && OptionHelper.isEmpty(resource)));
}
public void end(InterpretationContext ec, String name) {
}
public void finish(InterpretationContext ec) {
}
}
来源:https://blog.csdn.net/yh_1524/article/details/105499007


猜你喜欢
- 1、何为依赖冲突Maven是个很好用的依赖管理工具,但是再好的东西也不是完美的。Maven的依赖机制会导致Jar包的冲突。举个例子,现在你的
- 本人亲测,在使用IDEA使用Maven模板创建项目或者在当前项目中New Project,Maven的以下三个配置参数会重置使用C:\Use
- 一. 关键字Java中的关键字是由特定的单词组成,单词全为小写字母,每个都有特殊的含义,其实Java关键字也就那几十个,这个不需要背,以后都
- 创建自定义启动器0、项目总览1、创建项目,引入依赖创建项目 spring-boot-jdbc-starter,引入依赖,pom文件如下:&l
- 众所周知springboot项目,使用springboot插件打包的话,会打包成一个包含依赖的可执行jar,非常方便。只要有java运行环境
- package com.cjonline.foundation.evisa;import java.io.BufferedReader;im
- 声明式事务回顾事务事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!事务管理是企业级应用程序开发中必备技术,用来确保数据的完整
- 整型变量基本语法格式int变量名= 初始值;代码示例int a = 10;int表示变量的类型是一个整型。在 Java 中, 一个int变量
- java有两种类型的classload,一种是user-defined的,一种是jvm内置的bootstrap class loader,所
- 什么是MybatisMyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software fou
- 前言此节假日为严格按照国家要求的双休和法定节假日并且包含节假日的补班信息,大家可根据自己的需求自定义处理哦。以下为Maven配置,是程序用到
- 看完我上一篇文章,「你都理解创建线程池的参数吗?」之后,当遇到这种问题,你觉得你完全能够唬住面试官了,50k轻松到手。殊不知,要是面试官此刻
- 微信公众平台(map.weixin.qq.com)/开放平台(open.weixin.qq.com)/商户平台(pay.weixin.qq.
- Java8新特性系列我们已经介绍了Stream、Lambda表达式、DateTime日期时间处理,最后以“NullPointerExcept
- 我们都知道且经常用到 unsigned 关键字,但有没有想过,与此对应的 signed 关键字有啥用?int i = 0;signed in
- 网上的解决方法:这个是从网上看来的file-->setting-->plugins,搜索tomcat然后install之后会提示
- 前言在H5火热的时代,许多框架都出了底部弹窗的控件,在H5被称为弹出菜单ActionSheet,今天我们也来模仿一个ios的底部弹窗,取材于
- 背景介绍1,最近有一个大数据量插入的操作入库的业务场景,需要先做一些其他修改操作,然后在执行插入操作,由于插入数据可能会很多,用到多线程去拆
- 《Spring Boot Actuator详解与深入应用》预计包括三篇,第一篇重点讲Spring Boot Actuator 1.x的应用与
- 题目给定count=0;让5个线程并发累加到1000;思路创建一个类MyRunnable,实现Runnable(继承Thread类也可)定义