这篇文章主要讲解了“如何使用Logback设置property参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Logback设置property参数”吧!Logback设置prop
这篇文章主要讲解了“如何使用Logback设置property参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Logback设置property参数”吧!
<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>
<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>
<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>
文件所在路径: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://lsjlt.com/news/351279.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0