From 01b8e3011f9b4a35a70077984e0608ef0870e734 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Tue, 17 Aug 2021 10:46:37 +0200 Subject: [PATCH] Simplify loading of logback file (#10592) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- ledger-service/cli-opts/BUILD.bazel | 5 --- .../src/main/scala/cliopts/Logging.scala | 37 +++++++++---------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/ledger-service/cli-opts/BUILD.bazel b/ledger-service/cli-opts/BUILD.bazel index 7ab981bc6dd..26ff511b57a 100644 --- a/ledger-service/cli-opts/BUILD.bazel +++ b/ledger-service/cli-opts/BUILD.bazel @@ -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", diff --git a/ledger-service/cli-opts/src/main/scala/cliopts/Logging.scala b/ledger-service/cli-opts/src/main/scala/cliopts/Logging.scala index dcc10e12dae..16aabb82732 100644 --- a/ledger-service/cli-opts/src/main/scala/cliopts/Logging.scala +++ b/ledger-service/cli-opts/src/main/scala/cliopts/Logging.scala @@ -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()) } }