英文:
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:
<timestamp key="date" datePattern="yyyyMMdd"/>
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("prop-name", "prop-value");
jc.doConfigure(config)
In logback file, you can then use the properties line any other
${prop-name}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论