使用Logback设置property参数方式

 更新时间:2023年03月10日 09:31:19   作者:yh_1524  
这篇文章主要介绍了使用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
 * &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) {
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java使用FutureTask实现预加载的示例详解

    Java使用FutureTask实现预加载的示例详解

    基于FutureTask的特性,通常可以使用FutureTask做一些预加载工作,比如一些时间较长的计算等,本文就来和大家讲讲具体实现方法吧,感兴趣的可以了解一下
    2023-06-06
  • Spring Cloud Config Client超时及重试示例详解

    Spring Cloud Config Client超时及重试示例详解

    这篇文章主要给大家介绍了关于Spring Cloud Config Client超时及重试的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2018-05-05
  • Mybatis-Plus自动填充更新操作相关字段的实现

    Mybatis-Plus自动填充更新操作相关字段的实现

    数据库表中应该都要有create_time、update_time字段;那么在开发中,对于这些共有字段的处理应该要进行统一,这样就可以简化我们的开发过程。那么本文就对Mybatis-Plus中的字段自动填充进行记录
    2021-11-11
  • java 字符串反转的实例详解

    java 字符串反转的实例详解

    这篇文章主要介绍了java 字符串反转的实例详解的相关资料,这里提供实现代码帮助大家学习参考这部分内容,需要的朋友可以参考下
    2017-08-08
  • springmvc集成shiro登录失败处理操作

    springmvc集成shiro登录失败处理操作

    这篇文章主要介绍了springmvc集成shiro登录失败处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot 自定义注解之脱敏注解详解

    SpringBoot 自定义注解之脱敏注解详解

    这篇文章主要介绍了SpringBoot 自定义注解之脱敏注解详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 浅谈Spring boot cache使用和原理

    浅谈Spring boot cache使用和原理

    这篇文章主要介绍了浅谈Spring boot cache使用和原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 关于使用MyBatis简化JDBC开发和解决SQL语句警告的问题

    关于使用MyBatis简化JDBC开发和解决SQL语句警告的问题

    这篇文章主要介绍了关于使用MyBatis简化JDBC开发和解决SQL语句警告的问题,如果idea和数据库没有建立链接,idea不识别表的信息,就会出现SQL语句的警告,需要的朋友可以参考下
    2023-05-05
  • Java JConsole远程连接配置案例详解

    Java JConsole远程连接配置案例详解

    这篇文章主要介绍了Java JConsole远程连接配置案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java AQS 线程安全同步队列的实现

    Java AQS 线程安全同步队列的实现

    AQS 同步队列是很多的 Java 线程安全对象的实现,例如 ReentrantLock, Semaphore, CountDownLatch, ReentrantReadWriteLock 等等,本文就介绍了Java AQS 线程安全同步队列的实现,感兴趣的可以了解一下
    2023-08-08

最新评论