溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Logback設(shè)置property參數(shù)

發(fā)布時間:2023-03-10 10:35:36 來源:億速云 閱讀:171 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“如何使用Logback設(shè)置property參數(shù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用Logback設(shè)置property參數(shù)”吧!

    Logback設(shè)置property參數(shù)

    1.方式一:直接配置參數(shù)值

    <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屬性引入?yún)?shù)文件

    <configuration>
    
      <!-- 引入項目內(nèi)的文件指定文件所在的包路徑 -->
      <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屬性引入?yún)?shù)文件

    <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文件,如果引入的文件中的參數(shù)值需要進(jìn)行額外的加工處理,可重寫覆蓋此類,在begin或loadAndSetProperties方法中添加相關(guān)邏輯

    /**
     * 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) {
        }
    }

    感謝各位的閱讀,以上就是“如何使用Logback設(shè)置property參數(shù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何使用Logback設(shè)置property參數(shù)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI