如何从Typesafe config中配置系统属性或重新登录配置变量?

编程入门 行业动态 更新时间:2024-10-15 16:20:36
本文介绍了如何从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

推荐答案

您可以使用PropertyDefiner接口="noreferrer">日志提供.非琐碎的脚手架,但允许您使用XML进行配置,而不是在应用程序中进行配置.例如:

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中配置系统属性或重新登录配置变量?

本文发布于:2023-11-25 16:58:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   属性   系统   Typesafe   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!