文章目录

  • Logback设置property参数
    • 1. 方式一:直接配置参数值
    • 2. 方式二:通过file属性引入参数文件
    • 3. 方式三:通过resource属性引入参数文件
    • 4. 引入文件处理类*PropertyAction*

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* &lt;property&gt; 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&uuml;lc&uuml;*/
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 spacesvalue = 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) {}
}

Logback设置property参数相关推荐

  1. LOGback设置SQL参数打印

    一.hibernate中设置SQL参数打印: (主要是第一句) <logger name="org.hibernate.type.descriptor.sql.BasicBinder& ...

  2. 【FFmpeg】设置H264参数

    0.fffmpeg源码编译时,何时需要连接libx264库? ffmpeg其自带H.264解码功能,但是要实现H.264编码时就需要链接编码库libx264 ubuntu16.04安装libx264的 ...

  3. R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行SpatialSign变换(将数据投影到单位圆之内)、设置method参数为spatialSign

    R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行SpatialSign变换(将数据投影到单位圆之内).设置method参数为spatialSign 目录

  4. R语言使用table1包绘制(生成)三线表、使用单变量分列构建三线表、设置transpose参数转置三线表、变量作为列,子组(strata)作为行

    R语言使用table1包绘制(生成)三线表.使用单变量分列构建三线表.设置transpose参数转置三线表.变量作为列,子组(strata)作为行 目录

  5. python使用matplotlib可视化3D柱状图(3D histogram、三维柱状图、包含三个坐标轴x、y、z)、设置zdir参数为z、改变3d图观察的角度

    python使用matplotlib可视化3D柱状图(3D histogram.三维柱状图.包含三个坐标轴x.y.z).设置zdir参数为z.改变3d图观察的角度 目录

  6. pandas使用fillna函数并设置bfill参数使用列中的后序值填充缺失值

    pandas使用fillna函数并设置bfill参数使用列中的后序值填充缺失值(replace missing values with following values in column in da ...

  7. pandas使用read_csv读取文件数据、设置converters参数将百分比字符串转换为数字

    pandas使用read_csv读取文件数据.设置converters参数将百分比字符串转换为数字 目录 pandas使用read_csv读取文件数据.设置converters参数将百分比字符串转换为 ...

  8. python使用matplotlib可视化3D柱状图(3D bar plot、三维柱状图、包含三个坐标轴x、y、z)、设置zdir参数为y、改变3d图观察的角度

    python使用matplotlib可视化3D柱状图(3D bar plot.三维柱状图.包含三个坐标轴x.y.z).设置zdir参数为y.改变3d图观察的角度 目录

  9. R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行独立成分分析ICA(Independent components analysis)、设置method参数为ica

    R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行独立成分分析ICA(Independent components analysis).设置method参数为ica 目 ...

  10. pandas使用fillna函数并设置fffill参数使用列中的前序值填充缺失值(replace missing values with preceding values in column in d

    pandas使用fillna函数并设置fffill参数使用列中的前序值填充缺失值(replace missing values with preceding values in column in d ...

最新文章

  1. python3在线-荐python3在线编程输入输出总结
  2. 把 分数化为循环小数 和 把循环小数化为分数 的方法
  3. Tomcat可运行源码资源分享
  4. 判断字典中指定key是否存在
  5. Markovs Chains采样
  6. PHP的钩子实现解析
  7. windows Secure CRT使用SSH访问Linux服务器被拒绝,winscp访问Linux服务器被拒绝
  8. asm 查看 数据文件 修改 时间_Oracle的ASM介绍及管理
  9. 概述嵌入式设备驱动,教你怎么“玩”转嵌入式开发
  10. linux新建用户不显示,linux系统无法添加用户帐号的原因分析
  11. 重磅!百度飞桨开源语音基础模型库|中英文语音识别、语音翻译、语音合成、声音分类通通一行代码轻松搞定...
  12. php设计模式 — 单例模式(singleton)
  13. php随机函数给字加颜色,四种php随机字生成符串的方法
  14. 事件mousseenter和mouseover的区别
  15. 计算机组成原理第五版(白中英)第三章多层次存储器 习题
  16. 固态硬盘SSD之Flash闪存的基本概念
  17. mysql x86 x64_X86和X86_64和X64有什么区别?
  18. Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛
  19. WPF GridSplitter中需要设置HorizontalAlignment和VerticalAlignment
  20. 兼莱宝分享:表情包项目的玩法思路,轻松在家制作表情包赚钱

热门文章

  1. LFS 11.1 arm64 meson编译失败,libffi路径错误
  2. NAND Flash一般地址线和数据线共用,对读写速度有一定影响;而NOR Flash闪存数据线和地址线分开,所以相对而言读写速度快一些。
  3. 知到计算机绘图网课答案,计算机绘图知到网课答案
  4. 线性代数学习笔记(一)——二阶和三阶行列式
  5. 分布式事务 - 两阶段提交与三阶段提交
  6. 将计算机放置桌面右上角,如何给电脑桌面上添加我的电脑快捷方式
  7. 如何讲好一个故事(6个要素)
  8. 工作流引擎的流程业务表设计
  9. vue 前端显示图片加token_Vue 页面权限控制和登陆验证
  10. html简繁体转换,在线繁体字转换工具