如何从Typesafe config中配置系统属性或重新登录配置变量? [英] How can I configure system properties or logback configuration variables from typesafe config?
问题描述
我的logback.xml
配置文件中具有默认值的变量,并且我希望能够有选择地从我的typesafe config application.conf
文件中设置这些变量.
I have variables with defaults in my logback.xml
configuration file, and I would like to be able to optionally set these variables from my typesafe config application.conf
file.
我正在使用一个jar部署应用程序,并且打包在可部署jar中的application.conf
文件包含默认值.我在执行时通过-Dconfig.file=foo.conf
来提供服务器特定的配置文件的路径.
I am deploying the application using one-jar, and the application.conf
file packaged up in the deployable jar contains defaults. I pass -Dconfig.file=foo.conf
on execution to provide the path to a server-specific config file.
现在,我还可以传递-Dlog.level
和其他变量来覆盖我在logback.xml
中的默认值,而且我还必须在命令行中传递-Dfile.encoding=UTF-8
.我正在寻找一种能够在typesafe配置中而不是在命令行中指定这些属性的方法.感觉应该有一种方法可以做到,但是我找不到神奇的词.
Right now I can also pass -Dlog.level
and other variables to override my defaults in logback.xml
, and I also have to pass -Dfile.encoding=UTF-8
on the command line. I'm looking for a way to be able to specify these properties in the typesafe config instead of on the command line. It feels like there should be a way to do it, but I can't find the magic words.
logback.xml:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path:-logs/}/${log.file:-myLog.log}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 15 days' worth of history -->
<maxHistory>${log.history.days:-15}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="${log.level:-INFO}">
<appender-ref ref="FILE" />
</root>
</configuration>
application.conf(捆绑):
akka {
log-config-on-start = false
event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
}
特定于服务器的示例app.conf:
include "/application.conf"
akka.log-config-on-start = true
log.level = WARN // this is what I'd LIKE to be able to do
我当前如何运行该应用程序:
java -Dfile.encoding=UTF-8 -Dconfig.file=myApp.conf -Dlog.level=WARN -jar myApp_2.10-0.1-one-jar.jar
推荐答案
您可以使用
You can use the PropertyDefiner
interface that logback provides. Non trivial scaffolding but allows you to configure using XML instead of within your application. E.g.:
package com.myapp;
import ch.qos.logback.core.PropertyDefinerBase;
import com.typesafe.config.ConfigFactory;
public class TypesafeConfigPropertyDefiner extends PropertyDefinerBase {
private String propertyName;
@Override
public String getPropertyValue() {
return ConfigFactory.load().getString( propertyName );
}
public void setPropertyName( String propertyName ) {
this.propertyName = propertyName;
}
}
然后,在您的logback.xml文件中:
Then, in your logback.xml file:
<configuration>
<define name="loglevel" class="com.myapp.TypesafeConfigPropertyDefiner">
<propertyName>myapp.logging.loglevel</propertyName>
</define>
<root level="${loglevel}">
...
</root>
</configuration>
现在,上面的logback.xml文件将从类型安全配置文件(例如application.conf
)中读取myapp.logging.loglevel
.
Now, the above logback.xml file will read myapp.logging.loglevel
from your typesafe config file (e.g. application.conf
).
这篇关于如何从Typesafe config中配置系统属性或重新登录配置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!