From 4004d8a2cedf6276e6a893c621f6f9fcf48f1678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Kope=C4=87?= Date: Mon, 14 Sep 2020 13:02:26 +0200 Subject: [PATCH] [ETCM-107] Backport logging configuration --- build.sbt | 27 ++++++++------- src/main/resources/application.conf | 11 +++++++ src/main/resources/logback.xml | 29 ++++++++++++++-- .../LoadFromApplicationConfiguration.scala | 18 ++++++++++ .../scala/io/iohk/ethereum/utils/Logger.scala | 22 +++++++++++-- src/universal/conf/logback.xml | 33 ------------------- src/universal/conf/logging.conf | 12 +++++++ src/universal/conf/mantis.conf | 1 + 8 files changed, 103 insertions(+), 50 deletions(-) create mode 100644 src/main/scala/io/iohk/ethereum/utils/LoadFromApplicationConfiguration.scala delete mode 100644 src/universal/conf/logback.xml create mode 100644 src/universal/conf/logging.conf diff --git a/build.sbt b/build.sbt index 1000c0e961..9ae194cb95 100644 --- a/build.sbt +++ b/build.sbt @@ -31,24 +31,15 @@ val dep = { "ch.megard" %% "akka-http-cors" % "1.1.0", "org.json4s" %% "json4s-native" % "3.5.4", "de.heikoseeberger" %% "akka-http-json4s" % "1.34.0", - "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % "it,test", "io.suzaku" %% "boopickle" % "1.3.0", "org.ethereum" % "rocksdbjni" % rocksDb, - "org.scalatest" %% "scalatest" % "3.0.5" % "it,test", - "org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" % "test", - "org.scalacheck" %% "scalacheck" % "1.14.0" % "it,test", - "ch.qos.logback" % "logback-classic" % "1.2.3", - "org.jline" % "jline" % "3.1.2", - "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.0", "io.circe" %% "circe-core" % circeVersion, "io.circe" %% "circe-generic" % circeVersion, "io.circe" %% "circe-parser" % circeVersion, "io.circe" %% "circe-generic-extras" % circeVersion, - "com.miguno.akka" %% "akka-mock-scheduler" % "0.5.5" % "it,test", "commons-io" % "commons-io" % "2.6", "org.scala-sbt.ipcsocket" % "ipcsocket" % "1.0.0", "org.bouncycastle" % "bcprov-jdk15on" % "1.59", - "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0", "org.typelevel" %% "mouse" % "0.23", "org.typelevel" %% "cats-core" % "2.0.0", "org.typelevel" %% "cats-effect" % "2.0.0", @@ -61,12 +52,23 @@ val dep = { // mallet deps "org.jline" % "jline" % "3.1.2", "net.java.dev.jna" % "jna" % "4.5.1", - "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5", + "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.0", "com.github.scopt" %% "scopt" % "3.7.0", // Metrics (https://github.com/DataDog/java-dogstatsd-client) "com.datadoghq" % "java-dogstatsd-client" % "2.5", "org.xerial.snappy" % "snappy-java" % "1.1.7.2", - "org.web3j" % "core" % "3.4.0" % "test" + // Logging + "ch.qos.logback" % "logback-classic" % "1.2.3", + "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0", + "net.logstash.logback" % "logstash-logback-encoder" % "6.4", + "org.codehaus.janino" % "janino" % "3.0.6", + // Test utils + "org.web3j" % "core" % "3.4.0" % "test", + "org.scalatest" %% "scalatest" % "3.0.5" % "it,test", + "com.miguno.akka" %% "akka-mock-scheduler" % "0.5.5" % "it,test", + "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % "it,test", + "org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" % "test", + "org.scalacheck" %% "scalacheck" % "1.14.0" % "it,test" ) } @@ -146,6 +148,8 @@ mainClass in Compile := Some("io.iohk.ethereum.App") // Enables creating an executable with the configuration files, has to be run on the OS corresponding to the desired version jdkPackagerType := "image" +Universal / mappings += (resourceDirectory in Compile).value / "logback.xml" -> "conf/logback.xml" + val sep = java.io.File.separator jdkPackagerJVMArgs := Seq( "-Dconfig.file=." + sep + "conf" + sep + "mantis.conf", @@ -155,7 +159,6 @@ jdkPackagerJVMArgs := Seq( coverageExcludedPackages := "io\\.iohk\\.ethereum\\.extvm\\.msg.*" - addCommandAlias( "compile-all", """;compile diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 080837497c..2f0813c42a 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -535,6 +535,17 @@ mantis { log-errors = true } + logging { + # Flag used to switch logs to the JSON format + json-output = false + + # Logs directory + logs-dir = ${mantis.datadir}"/logs" + + # Logs filename + logs-file = "mantis" + } + async { dispatchers { block-forger { diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index e642e5ca4c..a6c582467f 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -3,17 +3,33 @@ + + + + + + ${stdoutEncoderPattern} + + + {"hostname":"${HOSTNAME}"} + + timestamp + [ignore] + + + + - ${user.home}/.mantis/logs/mantis.log + ${LOGSDIR}/${LOGSFILENAME}.log true - ${user.home}/.mantis/logs/mantis.%i.log.zip + ${LOGSDIR}/${LOGSFILENAME}.%i.log.zip 1 10 @@ -26,7 +42,14 @@ - + + + + + + + + diff --git a/src/main/scala/io/iohk/ethereum/utils/LoadFromApplicationConfiguration.scala b/src/main/scala/io/iohk/ethereum/utils/LoadFromApplicationConfiguration.scala new file mode 100644 index 0000000000..2b9e3d2269 --- /dev/null +++ b/src/main/scala/io/iohk/ethereum/utils/LoadFromApplicationConfiguration.scala @@ -0,0 +1,18 @@ +package io.iohk.ethereum.utils + +import ch.qos.logback.core.joran.action.Action +import ch.qos.logback.core.joran.spi.InterpretationContext +import com.typesafe.config.ConfigFactory +import org.xml.sax.Attributes + +/** + * Make properties defined in application.conf available to logback + */ +class LoadFromApplicationConfiguration extends Action { + + val config = ConfigFactory.load + override def begin(ic: InterpretationContext, body: String, attributes: Attributes): Unit = { + ic.addSubstitutionProperty(attributes.getValue("as"), config.getString(attributes.getValue("key"))) + } + override def end(ic: InterpretationContext, body: String): Unit = () +} diff --git a/src/main/scala/io/iohk/ethereum/utils/Logger.scala b/src/main/scala/io/iohk/ethereum/utils/Logger.scala index bdef44f15c..3c77c941b9 100644 --- a/src/main/scala/io/iohk/ethereum/utils/Logger.scala +++ b/src/main/scala/io/iohk/ethereum/utils/Logger.scala @@ -1,8 +1,26 @@ package io.iohk.ethereum.utils +import com.typesafe.scalalogging +import org.slf4j.{LoggerFactory, MDC} + trait Logger { - import com.typesafe.scalalogging.Logger + val log: scalalogging.Logger = com.typesafe.scalalogging.Logger(LoggerFactory.getLogger(getClass)) +} - val log = Logger(getClass) +trait LazyLogger { + lazy val log: scalalogging.Logger = com.typesafe.scalalogging.Logger(LoggerFactory.getLogger(getClass)) } +trait LoggingContext { + val asParameterMap: Map[String, String] +} + +object LoggingContext { + implicit class ContextLoggerOps[T <: scalalogging.Logger](log: T) { + def withContext(context: LoggingContext)(doLog: T => Unit): Unit = { + context.asParameterMap.foreach { case (key, value) => MDC.put(key, value) } + doLog(log) + MDC.clear() + } + } +} diff --git a/src/universal/conf/logback.xml b/src/universal/conf/logback.xml deleted file mode 100644 index 389c30ae7f..0000000000 --- a/src/universal/conf/logback.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - ${stdoutEncoderPattern} - - - - - ${user.home}/.mantis/logs/mantis.log - true - - ${user.home}/.mantis/logs/mantis.%i.log.zip - 1 - 10 - - - 10MB - - - ${fileEncoderPattern} - - - - - - - - - \ No newline at end of file diff --git a/src/universal/conf/logging.conf b/src/universal/conf/logging.conf new file mode 100644 index 0000000000..b63b434bfc --- /dev/null +++ b/src/universal/conf/logging.conf @@ -0,0 +1,12 @@ +mantis { + logging { + # Flag used to switch logs to the JSON format + # json-output = false + + # Logs directory + # logs-dir = ${mantis.datadir}"/logs" + + # Logs filename + # logs-file = "mantis" + } +} diff --git a/src/universal/conf/mantis.conf b/src/universal/conf/mantis.conf index fa229b59db..c1c51d04e1 100644 --- a/src/universal/conf/mantis.conf +++ b/src/universal/conf/mantis.conf @@ -15,6 +15,7 @@ include "sync.conf" include "misc.conf" include "consensus.conf" include "metrics.conf" +include "logging.conf" # Uncomment to run Mantis in test-mode (similar to --test flag in cpp-ethereum). # When set, test validators and consensus are used by this node.