iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何使用Logback设置property参数
  • 445
分享到

如何使用Logback设置property参数

2023-07-05 10:07:07 445人浏览 八月长安
摘要

这篇文章主要讲解了“如何使用Logback设置property参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Logback设置property参数”吧!Logback设置prop

这篇文章主要讲解了“如何使用Logback设置property参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用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方法中添加相关逻辑

    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;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.";        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) {    }}

    感谢各位的阅读,以上就是“如何使用Logback设置property参数”的内容了,经过本文的学习后,相信大家对如何使用Logback设置property参数这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

    --结束END--

    本文标题: 如何使用Logback设置property参数

    本文链接: https://www.lsjlt.com/news/351279.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • 如何使用Logback设置property参数
      这篇文章主要讲解了“如何使用Logback设置property参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Logback设置property参数”吧!Logback设置prop...
      99+
      2023-07-05
    • 使用Logback设置property参数方式
      目录Logback设置property参数1.方式一:直接配置参数值2.方式二:通过file属性引入参数文件3.方式三:通过resource属性引入参数文件4.引入文件处理类Prop...
      99+
      2023-03-10
      Logback设置property参数 Logback property参数 Logback参数property
    • Logback的使用及如何配置
      目录1 为什么选用Logback?2 Logback的配置3 Logback的输出格式4 Appender5 Logback使用过程中遇到的问题日志在项目开发过程的作用不言而喻,项目...
      99+
      2022-11-12
    • 如何设置Repeat Interval参数
      如何设置Repeat Interval参数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。 设置Repeat ...
      99+
      2022-10-19
    • uniapp如何设置页面参数
      随着移动应用开发的快速发展,现在越来越多的开发者正在使用uniapp来快速构建跨平台的应用程序。然而,当我们创建一个页面时,有时候需要给页面传递参数,这些参数可能是从其他页面传递过来的。那么,uniapp如何设置页面参数呢?本文将详细介绍u...
      99+
      2023-05-14
    • access如何设置参数查询
      在access中,可以使用参数查询来设置参数并查询数据。以下是设置参数查询的步骤:1. 打开Access数据库并选择要查询的数据表。...
      99+
      2023-09-09
      access
    • 映射ADO.NET如何设置参数
      这篇文章主要为大家展示了“映射ADO.NET如何设置参数”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“映射ADO.NET如何设置参数”这篇文章吧。进行ADO.NET设置要注意许多问题,首先碰到的...
      99+
      2023-06-17
    • Springboot如何使用Logback实现日志配置
      这篇文章主要讲解了“Springboot如何使用Logback实现日志配置”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Springboot如何使用Logback实现日志配置”吧!概述默认情...
      99+
      2023-07-04
    • Python函数默认参数如何设置
      这篇文章主要介绍“Python函数默认参数如何设置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python函数默认参数如何设置”文章能帮助大家解决问题。我们知道,在调用函数时如果不指定某个参数,P...
      99+
      2023-07-05
    • linux如何设置jvm内存参数
      要设置JVM的内存参数,可以通过修改Java程序的启动脚本来实现。以下是在Linux上设置JVM内存参数的步骤:1. 打开Java程...
      99+
      2023-09-09
      linux jvm
    • python 如何设置柱状图参数
      version:python 3.6 环境:anaconda/JupyterLab 0.27.0 操作系统:Windows 10 import pandas as pd import matplotlib.pyp...
      99+
      2022-06-02
      python 柱状图参数 设置参数
    • 如何设置mysql的主要参数
      本文主要给大家介绍如何设置mysql的主要参数,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下如何设置mysql的主要参数吧。mysql的主要参数设置...
      99+
      2022-10-18
    • property()函数如何在Python中使用
      这篇文章将为大家详细讲解有关 property()函数如何在Python中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。python是什么意思Python是一种跨平台的、具有解释性、编译...
      99+
      2023-06-14
    • Springboot如何使用logback实现多环境配置?
      前言 Logback是由log4j创始人设计的又一个开源日记组件,Logback 当前分成三个模块:logback-core,logback- classic和logback-ac...
      99+
      2022-11-12
    • windows中chemdraw如何设置页面参数
      这篇文章主要介绍了windows中chemdraw如何设置页面参数的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇windows中chemdraw如何设置页面参数文章都会有所收获...
      99+
      2022-12-06
      windows chemdraw
    • win10玩csgo如何优化参数设置
      本篇内容主要讲解“win10玩csgo如何优化参数设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“win10玩csgo如何优化参数设置”吧!一、N卡设置方法 在电脑桌面空白位置,点击鼠标右键,...
      99+
      2023-07-01
    • Shell脚本位置参数如何使用
      这篇文章主要介绍“Shell脚本位置参数如何使用”,在日常操作中,相信很多人在Shell脚本位置参数如何使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Shell脚本位置参数如何使用”的疑惑有所帮助!接下来...
      99+
      2023-07-05
    • 如何进行JVM参数设置及分析
      本篇文章给大家分享的是有关如何进行JVM参数设置及分析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选...
      99+
      2023-06-17
    • Eclipse IDE中如何设置JVM启动参数
      目录如何设置JVM启动参数下面是一些设置的步骤在Eclipse上手动设置jvm参数典型设置如何设置JVM启动参数 关于《深入理解Java虚拟机》里面测试OutOfMemoryErro...
      99+
      2022-11-13
    • java重载方法的参数如何设置
      这篇文章主要介绍“java重载方法的参数如何设置”,在日常操作中,相信很多人在java重载方法的参数如何设置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java重载方法的参数如何设置”的疑惑有所帮助!接下来...
      99+
      2023-06-30
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作