0

I use typesafe config library to manage my Scala project's configuration. I use the following pattern to organize and load my configurations in a type-safe manner

object AppConfig {
  private val config = ConfigFactory.load()
  lazy val host: String = config.getString("host")
  object Splunk {
    private lazy val splunkConf = config.getConfig("splunk")
    lazy val index = splunkConf.getString("index")
    lazy val token = splunkConf.getString("token")
  }
}

I need to inject some of those configs in my logback.groovy. This works perfectly when accessing direct properties of AppConfig : example AppConfig.host(), but not for nested objects: like AppConfig.Splunk.token() logback.groovy

appender("splunk", HttpEventCollectorLogbackAppender) {
    url = "http://someSplunkUrl:8088/services/collector"
    host = AppConfig.host()
    token = AppConfig.Splunk.token() 
    index = AppConfig.Splunk.index()
    batch_size_count = 1
    layout(PatternLayout) {
        pattern = "%msg"
    }
}
groovy.lang.MissingPropertyException: No such property: Splunk for class: AppConfig
    at groovy.lang.MetaClassImpl.invokeStaticMissingProperty(MetaClassImpl.java:1028)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1932)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1908)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3886)
    at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:50)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:298)
    at Script1$_run_closure2.doCall(Script1.groovy:18)
    at Script1$_run_closure2.doCall(Script1.groovy)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2
  • Use typesafe config wrapper - github.com/pureconfig/pureconfig . Define config as case classes, load it and then access properties of these case classes. Commented Sep 14, 2019 at 9:26
  • Can you try AppConfig.Splunk$.MODULE$.token() Commented Sep 14, 2019 at 15:34

1 Answer 1

0

I ended up referencing the nested objected as below :

def splunkConf = Class.forName("com.util.AppConfig\$Splunk\$")
                      .getDeclaredConstructor().newInstance()

I could then access the fields successfully :

token = splunkConf."token"()
index = splunkConf."index"()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.