2

I tried to get the log message from message.properties and did string interpolation with that log message.In this time,The log message is not interpolated with original message.

I am not able to get string interpolated result and am getting the output as log message what i have specified in properties file

Here I dont want to hard code any log message in scala file,Instead of this,I want to get all message from properties file and redirected into application log after interpolating string value.

import com.typesafe.config.ConfigFactory
import grizzled.slf4j.Logging
object Test extends Logging {
  def main(args: Array[String]){
    val subjectArea="Member"
    val  messageProp = ConfigFactory.load("message.properties")
    val log=messageProp.getString("log.subject.area")
    debug(s"$log")
  }
}

message.properties
log.subject.area=The Subject Area : $subjectArea

Console Output: The Subject Area : $subjectArea i want this output : The Subject Area : Member

Thanks in advance!!! Test.scala message.propeties

1
  • What is the question? Commented Nov 16, 2017 at 12:35

2 Answers 2

1

This is not a string interpolation problem. You want a lightweight templating engine (e.g. http://jtwig.org/documentation/quick-start/application or something else). I feel that most of them would be an overkill if your problem is as simple as in the snippet you've provided.

If you want to do something more or less complex, then sure, go with template engines.

Otherwise, I'd just go with string substitution.

Sign up to request clarification or add additional context in comments.

Comments

0

String interpolation only works with constants. To do what you want dynamically, you need to write some explicit processing yoruself (or use a template engine library). Something like this, perhaps?

 val substPattern = """\$\{(.+?)\}""".r
 import java.util.regex.Matcher.{ quoteReplacement => qq }
 def processSubstitutions(
   input: String, 
   vars: Map[String, String]
 ) = substPattern.replaceAllIn(
     input, { m => 
       val ref = m.group(1)
       qq(vars.getOrElse(ref, ref)
     }
 )

 val vars = Map("subjectArea" -> "Member")
 val  messageProp = ConfigFactory.load("message.properties")
 val log=processSubstitutions(
    messageProp.getString("log.subject.area"),
    vars
 )

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.