使用Logback设置property参数方式

作者:yh_1524 时间:2022-07-28 01:06:01 

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 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

标签:Logback,property,参数
0
投稿

猜你喜欢

  • idea启动springmvc项目时报找不到类的解决方法

    2023-11-09 16:51:04
  • C#计算两个文件的相对目录算法的实例代码

    2022-08-27 10:27:44
  • Java设计模式编程之解释器模式的简单讲解

    2022-01-24 16:03:32
  • Java背包问题求解实例代码

    2023-10-05 06:20:33
  • Java数据结构之顺序表和链表精解

    2021-07-01 14:30:39
  • Java实现发送手机短信语音验证功能代码实例

    2023-04-04 19:03:31
  • Java线程的基本概念

    2022-05-20 16:30:22
  • Android打造属于自己的新闻平台(客户端+服务器)

    2022-03-31 13:45:00
  • JavaWeb使用Session和Cookie实现登录认证

    2023-12-11 19:13:29
  • Spring Junit单元测试加载配置文件失败问题

    2022-06-05 13:37:48
  • java实现动态 代理方法浅析

    2023-11-28 23:33:59
  • 两种JAVA实现短网址服务算法

    2023-05-08 12:17:30
  • java web学习_浅谈request对象中get和post的差异

    2022-10-28 00:25:20
  • Java Swing中JDialog实现用户登陆UI示例

    2021-10-12 13:58:00
  • 关于Maven的使用,这些你都真的了解么

    2022-01-02 14:19:08
  • Java并发编程之栅栏(CyclicBarrier)实例介绍

    2023-12-01 19:48:53
  • Java聊天室之实现客户端一对一聊天功能

    2022-12-28 09:23:30
  • SpringBoot 配置文件总结

    2021-09-06 13:12:57
  • C#逐行读取文件的方法

    2023-05-16 23:53:02
  • 一文搞懂并学会使用SpringBoot的Actuator运行状态监控组件的详细教程

    2022-01-31 10:28:23
  • asp之家 软件编程 m.aspxhome.com