Skip to content

Commit

Permalink
Load the correct logback file for the http json service respecting th…
Browse files Browse the repository at this point in the history
…e deployment situation (#9938)

* Load the correct logback file for the http json service respecting the deployment situation

changelog_begin

[HTTP-JSON]
- fixed that json log output could not be enabled via cli options except via usage of env vars

changelog_end

* Move import statement, remove some braces and reformat

* Move system prop dependend logic to cliopts Logging.scala

* Remove PathKind type in cliopts Logging.scala as it's not necessary anymore
  • Loading branch information
realvictorprm committed Nov 16, 2021
1 parent 9cc3512 commit e2fb70a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions ledger-service/cli-opts/src/main/scala/cliopts/Logging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@ import ch.qos.logback.classic.{Level => LogLevel}

object Logging {

def reconfigure(clazz: Class[_], pathToLogbackFileInJarFile: String): Unit = {
def reconfigure(clazz: Class[_]): Unit = {
// Try reconfiguring the library
import ch.qos.logback.core.joran.spi.JoranException
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.joran.JoranConfigurator
import org.slf4j.LoggerFactory
import scala.util.Using
Using.resource(clazz.getClassLoader.getResource(pathToLogbackFileInJarFile).openStream()) {
stream =>
import java.io.InputStream
import java.io.FileInputStream
def reloadConfig(path: String, openStream: String => InputStream): Unit =
Using.resource(openStream(path)) { stream =>
try {
val context = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
val configurator = new JoranConfigurator
val configurator = new JoranConfigurator()
configurator.setContext(context)
context.reset()
configurator.doConfigure(stream)
} catch {
case je: JoranException =>
// Fallback to System.err.println because the logger won't work in any way anymore.
System.err.println(s"reconfigured failed using url $pathToLogbackFileInJarFile: $je")
System.err.println(
s"reconfigured failed using url $path: $je"
)
je.printStackTrace(System.err)
} finally {
stream.close()
}
} finally stream.close()
}
System.getProperty("logback.configurationFile") match {
case null => reloadConfig("logback.xml", clazz.getClassLoader.getResource(_).openStream())
case path => reloadConfig(path, new FileInputStream(_))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Main {
case LogEncoder.Plain => () // This is the default
case LogEncoder.Json =>
Logging.setUseJsonLogEncoderSystemProp()
Logging.reconfigure(getClass, "logback.xml")
Logging.reconfigure(getClass)
}
// Here we set all things which are related to logging but not to
// any env vars in the logback.xml file.
Expand Down

0 comments on commit e2fb70a

Please sign in to comment.