Simplify loading of logback file (#10592)

* Simplify loading of logback file

doConfigure accepts a URL which slightly simplifies things.
Really the primary reason why I’m doing this is that it gets veracode
to shut up. I don’t fully understand what it’s worried about in the
first place but it looks like it gets angry about calling openStream
on the resource *shrug*

changelog_begin
changelog_end

* fix 2.12 build

changelog_begin
changelog_end
This commit is contained in:
Moritz Kiefer 2021-08-17 10:46:37 +02:00 committed by GitHub
parent 19bfdbee00
commit 01b8e3011f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 25 deletions

View File

@ -16,11 +16,6 @@ da_scala_library(
],
scalacopts = lf_scalacopts,
tags = ["maven_coordinates=com.daml:http-json-cli-opts:__VERSION__"],
versioned_scala_deps = {
"2.12": [
"@maven//:org_scala_lang_modules_scala_collection_compat",
],
},
visibility = ["//visibility:public"],
runtime_deps = [
"@maven//:org_codehaus_janino_janino",

View File

@ -12,29 +12,26 @@ object Logging {
import ch.qos.logback.core.joran.spi.JoranException
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.joran.JoranConfigurator
import java.io.File
import java.net.URL
import org.slf4j.LoggerFactory
import scala.util.Using
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()
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 $path: $je"
)
} finally stream.close()
def reloadConfig(path: URL): Unit =
try {
val context = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
val configurator = new JoranConfigurator()
configurator.setContext(context)
context.reset()
configurator.doConfigure(path)
} 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 $path: $je"
)
}
System.getProperty("logback.configurationFile") match {
case null => reloadConfig("logback.xml", clazz.getClassLoader.getResource(_).openStream())
case path => reloadConfig(path, new FileInputStream(_))
case null => reloadConfig(clazz.getClassLoader.getResource("logback.xml"))
case path => reloadConfig(new File(path).toURI().toURL())
}
}