从Logback和Spring Boot的应用程序属性文件中加载datePattern。

huangapple go评论89阅读模式
英文:

Loading a datePattern from application-properties file in Logback and Spring Boot

问题

Sure, here's the translated content:

在我的 logback-spring.xml 文件中,我已经定义了一个时间戳,如下所示:

<timestamp key="date" datePattern="yyyyMMdd"/>

我想知道是否可以从我的 application-properties 文件中加载这个 datePattern 的值。我已经定义了一个属性 logging.date.format=yyyyMMdd,我在代码的其他部分中正在使用它,如果我能在我的 logback 文件中也使用它,那将非常有帮助,这样我只需要在一个地方进行更改。

英文:

In my logback-spring.xml file, I have a defined a timestamp like so:

&lt;timestamp key=&quot;date&quot; datePattern=&quot;yyyyMMdd&quot;/&gt;

I wanted to know if it is possible to load this datePattern value from my application-properties file. I have defined a property logging.date.format=yyyyMMdd which I am using in other parts of the code and it would be really helpful if I could use this in my logback file as well so that I have to only make changes in a single place.

答案1

得分: 1

我在我的应用程序中将属性传递给logback。在我的webapp初始化程序中获取LoggerContext并输入这些属性。我重置上下文,因为我更改了一些其他设置,不知道是否需要。

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
context.reset();
context.putProperty("prop-name", "prop-value");
jc.doConfigure(config);

在logback文件中,您可以像其他属性一样使用这些属性:

${prop-name}
英文:

I pass properties to logback in my application. In my webapp initializer obtain the LoggerContext and input the properties. I reset the context because I change some other settings, don't know if it is required.

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
context.reset();
context.putProperty(&quot;prop-name&quot;, &quot;prop-value&quot;);
jc.doConfigure(config)

In logback file, you can then use the properties line any other

${prop-name}

huangapple
  • 本文由 发表于 2020年8月12日 19:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63375162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定