Skip to content

Commit a51e27b

Browse files
authored
Merge pull request #667 from input-output-hk/etcm-107-backport-logging-configuration
[ETCM-107] Backport logging configuration
2 parents ed10b5d + 4004d8a commit a51e27b

File tree

8 files changed

+103
-50
lines changed

8 files changed

+103
-50
lines changed

build.sbt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,15 @@ val dep = {
3131
"ch.megard" %% "akka-http-cors" % "1.1.0",
3232
"org.json4s" %% "json4s-native" % "3.5.4",
3333
"de.heikoseeberger" %% "akka-http-json4s" % "1.34.0",
34-
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % "it,test",
3534
"io.suzaku" %% "boopickle" % "1.3.0",
3635
"org.ethereum" % "rocksdbjni" % rocksDb,
37-
"org.scalatest" %% "scalatest" % "3.0.5" % "it,test",
38-
"org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" % "test",
39-
"org.scalacheck" %% "scalacheck" % "1.14.0" % "it,test",
40-
"ch.qos.logback" % "logback-classic" % "1.2.3",
41-
"org.jline" % "jline" % "3.1.2",
42-
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.0",
4336
"io.circe" %% "circe-core" % circeVersion,
4437
"io.circe" %% "circe-generic" % circeVersion,
4538
"io.circe" %% "circe-parser" % circeVersion,
4639
"io.circe" %% "circe-generic-extras" % circeVersion,
47-
"com.miguno.akka" %% "akka-mock-scheduler" % "0.5.5" % "it,test",
4840
"commons-io" % "commons-io" % "2.6",
4941
"org.scala-sbt.ipcsocket" % "ipcsocket" % "1.0.0",
5042
"org.bouncycastle" % "bcprov-jdk15on" % "1.59",
51-
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.0",
5243
"org.typelevel" %% "mouse" % "0.23",
5344
"org.typelevel" %% "cats-core" % "2.0.0",
5445
"org.typelevel" %% "cats-effect" % "2.0.0",
@@ -61,12 +52,23 @@ val dep = {
6152
// mallet deps
6253
"org.jline" % "jline" % "3.1.2",
6354
"net.java.dev.jna" % "jna" % "4.5.1",
64-
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5",
55+
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.0",
6556
"com.github.scopt" %% "scopt" % "3.7.0",
6657
// Metrics (https://github.com/DataDog/java-dogstatsd-client)
6758
"com.datadoghq" % "java-dogstatsd-client" % "2.5",
6859
"org.xerial.snappy" % "snappy-java" % "1.1.7.2",
69-
"org.web3j" % "core" % "3.4.0" % "test"
60+
// Logging
61+
"ch.qos.logback" % "logback-classic" % "1.2.3",
62+
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.0",
63+
"net.logstash.logback" % "logstash-logback-encoder" % "6.4",
64+
"org.codehaus.janino" % "janino" % "3.0.6",
65+
// Test utils
66+
"org.web3j" % "core" % "3.4.0" % "test",
67+
"org.scalatest" %% "scalatest" % "3.0.5" % "it,test",
68+
"com.miguno.akka" %% "akka-mock-scheduler" % "0.5.5" % "it,test",
69+
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % "it,test",
70+
"org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" % "test",
71+
"org.scalacheck" %% "scalacheck" % "1.14.0" % "it,test"
7072
)
7173
}
7274

@@ -146,6 +148,8 @@ mainClass in Compile := Some("io.iohk.ethereum.App")
146148
// Enables creating an executable with the configuration files, has to be run on the OS corresponding to the desired version
147149
jdkPackagerType := "image"
148150

151+
Universal / mappings += (resourceDirectory in Compile).value / "logback.xml" -> "conf/logback.xml"
152+
149153
val sep = java.io.File.separator
150154
jdkPackagerJVMArgs := Seq(
151155
"-Dconfig.file=." + sep + "conf" + sep + "mantis.conf",
@@ -155,7 +159,6 @@ jdkPackagerJVMArgs := Seq(
155159

156160
coverageExcludedPackages := "io\\.iohk\\.ethereum\\.extvm\\.msg.*"
157161

158-
159162
addCommandAlias(
160163
"compile-all",
161164
""";compile

src/main/resources/application.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,17 @@ mantis {
535535
log-errors = true
536536
}
537537

538+
logging {
539+
# Flag used to switch logs to the JSON format
540+
json-output = false
541+
542+
# Logs directory
543+
logs-dir = ${mantis.datadir}"/logs"
544+
545+
# Logs filename
546+
logs-file = "mantis"
547+
}
548+
538549
async {
539550
dispatchers {
540551
block-forger {

src/main/resources/logback.xml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@
33
<property name="stdoutEncoderPattern" value="%d{HH:mm:ss} [%logger{36}] - %msg%n" />
44
<property name="fileEncoderPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %X{akkaSource} - %msg%n" />
55

6+
<!--read properties from application.conf-->
7+
<newRule pattern="*/load" actionClass="io.iohk.ethereum.utils.LoadFromApplicationConfiguration"/>
8+
<load key="mantis.logging.json-output" as="ASJSON"/>
9+
<load key="mantis.logging.logs-dir" as="LOGSDIR"/>
10+
<load key="mantis.logging.logs-file" as="LOGSFILENAME"/>
11+
612
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
713
<encoder>
814
<pattern>${stdoutEncoderPattern}</pattern>
915
</encoder>
1016
</appender>
1117

18+
<appender name="STASH" class="ch.qos.logback.core.ConsoleAppender">
19+
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
20+
<customFields>{"hostname":"${HOSTNAME}"}</customFields>
21+
<fieldNames>
22+
<timestamp>timestamp</timestamp>
23+
<version>[ignore]</version>
24+
</fieldNames>
25+
</encoder>
26+
</appender>
27+
1228
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
13-
<file>${user.home}/.mantis/logs/mantis.log</file>
29+
<file>${LOGSDIR}/${LOGSFILENAME}.log</file>
1430
<append>true</append>
1531
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
16-
<fileNamePattern>${user.home}/.mantis/logs/mantis.%i.log.zip</fileNamePattern>
32+
<fileNamePattern>${LOGSDIR}/${LOGSFILENAME}.%i.log.zip</fileNamePattern>
1733
<minIndex>1</minIndex>
1834
<maxIndex>10</maxIndex>
1935
</rollingPolicy>
@@ -26,7 +42,14 @@
2642
</appender>
2743

2844
<root level="DEBUG">
29-
<appender-ref ref="STDOUT" />
45+
<if condition='p("ASJSON").contains("true")'>
46+
<then>
47+
<appender-ref ref="STASH" />
48+
</then>
49+
<else>
50+
<appender-ref ref="STDOUT" />
51+
</else>
52+
</if>
3053
<appender-ref ref="FILE" />
3154
</root>
3255

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.iohk.ethereum.utils
2+
3+
import ch.qos.logback.core.joran.action.Action
4+
import ch.qos.logback.core.joran.spi.InterpretationContext
5+
import com.typesafe.config.ConfigFactory
6+
import org.xml.sax.Attributes
7+
8+
/**
9+
* Make properties defined in application.conf available to logback
10+
*/
11+
class LoadFromApplicationConfiguration extends Action {
12+
13+
val config = ConfigFactory.load
14+
override def begin(ic: InterpretationContext, body: String, attributes: Attributes): Unit = {
15+
ic.addSubstitutionProperty(attributes.getValue("as"), config.getString(attributes.getValue("key")))
16+
}
17+
override def end(ic: InterpretationContext, body: String): Unit = ()
18+
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package io.iohk.ethereum.utils
22

3+
import com.typesafe.scalalogging
4+
import org.slf4j.{LoggerFactory, MDC}
5+
36
trait Logger {
4-
import com.typesafe.scalalogging.Logger
7+
val log: scalalogging.Logger = com.typesafe.scalalogging.Logger(LoggerFactory.getLogger(getClass))
8+
}
59

6-
val log = Logger(getClass)
10+
trait LazyLogger {
11+
lazy val log: scalalogging.Logger = com.typesafe.scalalogging.Logger(LoggerFactory.getLogger(getClass))
712
}
813

14+
trait LoggingContext {
15+
val asParameterMap: Map[String, String]
16+
}
17+
18+
object LoggingContext {
19+
implicit class ContextLoggerOps[T <: scalalogging.Logger](log: T) {
20+
def withContext(context: LoggingContext)(doLog: T => Unit): Unit = {
21+
context.asParameterMap.foreach { case (key, value) => MDC.put(key, value) }
22+
doLog(log)
23+
MDC.clear()
24+
}
25+
}
26+
}

src/universal/conf/logback.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/universal/conf/logging.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
mantis {
2+
logging {
3+
# Flag used to switch logs to the JSON format
4+
# json-output = false
5+
6+
# Logs directory
7+
# logs-dir = ${mantis.datadir}"/logs"
8+
9+
# Logs filename
10+
# logs-file = "mantis"
11+
}
12+
}

src/universal/conf/mantis.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ include "sync.conf"
1515
include "misc.conf"
1616
include "consensus.conf"
1717
include "metrics.conf"
18+
include "logging.conf"
1819

1920
# Uncomment to run Mantis in test-mode (similar to --test flag in cpp-ethereum).
2021
# When set, test validators and consensus are used by this node.

0 commit comments

Comments
 (0)