Skip to content

Commit

Permalink
feat: add good canonized logging setup for log4j and layouts
Browse files Browse the repository at this point in the history
currently only bound in test

Signed-off-by: Kevin O'Donnell <kevin@blockchaintp.com>
  • Loading branch information
scealiontach committed Jul 19, 2021
1 parent b305b4f commit 723d257
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 10 deletions.
92 changes: 82 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,40 @@
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.0-alpha2</version>
</dependency>
<!-- Include the SLF4j bindings only in test and Main packages not libraries -->
<dependency>
<groupId>kr.pe.kwonnam.slf4j-lambda</groupId>
<artifactId>slf4j-lambda-core</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0-rc1</version>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0-rc1</version>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<version>2.14.1</version>
</dependency>

<!-- Resilience -->
<dependency>
Expand Down Expand Up @@ -222,6 +246,7 @@
<version>2.13.0-rc1</version>
</dependency>


<!-- Test deps -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -253,18 +278,44 @@
<dependencies>

<!-- Logging -->
<!-- SLF4J Bindings should only be included in "main" projects and test scope -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>
<dependency>
<groupId>kr.pe.kwonnam.slf4j-lambda</groupId>
<artifactId>slf4j-lambda-core</artifactId>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- Needed implicitly for log4j2 YAML configuration -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<scope>test</scope>
</dependency>


<!-- AWS -->
<dependency>
Expand Down Expand Up @@ -373,11 +424,28 @@
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
<version>1.7.0</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
Expand Down Expand Up @@ -570,6 +638,10 @@
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
59 changes: 59 additions & 0 deletions src/main/resources/log4j-json-layout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"mdc": {
"$resolver": "mdc"
},
"exception": {
"exception_class": {
"$resolver": "exception",
"field": "className"
},
"exception_message": {
"$resolver": "exception",
"field": "message"
},
"stacktrace": {
"$resolver": "exception",
"field": "stackTrace",
"stackTrace": {
"stringified": true
}
}
},
"line_number": {
"$resolver": "source",
"field": "lineNumber"
},
"class": {
"$resolver": "source",
"field": "className"
},
"@version": 1,
"source_host": "${hostName}",
"message": {
"$resolver": "message",
"stringified": true
},
"thread_name": {
"$resolver": "thread",
"field": "name"
},
"@timestamp": {
"$resolver": "timestamp"
},
"level": {
"$resolver": "level",
"field": "name"
},
"file": {
"$resolver": "source",
"field": "fileName"
},
"method": {
"$resolver": "source",
"field": "methodName"
},
"logger_name": {
"$resolver": "logger",
"field": "name"
}
}
52 changes: 52 additions & 0 deletions src/test/resources/log4j2-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Configuration:
# Initial log level after loading configuration
status: info
name: TEST
monitorInterval: 30
Properties:
Appenders:
File:
# logging to a file for testing where a complete human readable, and detail level log is required
- name: TEST_FILE
fileName: target/test.log
PatternLayout:
# level, date and time, thread, full class name and line number, message
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C{10}:%L] - %msg%n"
# logging to a file for testing where a complete and detailed but not easily readable, log is required
- name: TEST_JSON_FILE
fileName: target/test-json.log
JsonTemplateLayout:
eventTemplateUri: classpath:log4j-json-layout.json
Console:
# For console logging during normal runs where detailed info about the code
# is not desirable (no performance impact)
- name: CONSOLE
target: SYSTEM_OUT
PatternLayout:
# Level, thread, simple logger name, message
pattern: "[%-5level] [%t] [%c{1}] - %msg%n"
# For console logging while testing where timestamps are not needed
# but detail about the code are (performance impact)
- name: TEST_CONSOLE
target: SYSTEM_OUT
PatternLayout:
# level, class shortname, line number, message
pattern: "[%-5level] [%C{1}:%L] - %msg%n"
# For logging json to a console, such as in a container
- name: JSON_CONSOLE
target: SYSTEM_OUT
JsonTemplateLayout:
eventTemplateUri: classpath:log4j-json-layout.json
Loggers:
Root:
# Items not otherwise classified are recorded in test logs for forensics
level: info
AppenderRef:
- ref: TEST_FILE
- ref: TEST_JSON_FILE
Logger:
# Log what we are interested in at the root and the console
- name: com.blockchaintp
level: debug
AppenderRef:
- ref: TEST_CONSOLE

0 comments on commit 723d257

Please sign in to comment.