Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 949b7f7

Browse files
authored
Dw 3544 initial logging library (#1)
* DW-3544 - Initial copy & changes from data importer * DW-3544 - further changes & test fixes * Adding Docs * DW-3544 - Resolve PR comments * DW-3544 - Resolving more PR comments
1 parent cd31b4b commit 949b7f7

18 files changed

Lines changed: 1052 additions & 0 deletions

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.gradle
2+
build/
3+
out/
4+
5+
#Logs
6+
logs/
7+
*.log
8+
*.jks
9+
*.pem
10+
*.crt
11+
*.p12
12+
13+
# IntelliJ
14+
.idea/
15+
*.iml
16+
17+
# Testing
18+
.kotlintest

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,121 @@
11
# dataworks-common-logging
22
Kotlin utility library to be used in Dataworks applications to ensure common logging format.
3+
4+
## Log formatting
5+
Dataworks common logging provides opinionated ways to write messages into log files, using [sl4j](http://www.slf4j.org/).
6+
7+
Out of the box, it provides functionality to convert log messages to JSON and appends a number of common fields. These can be found in the `LogField` enum class. Any `LogField` which is not found at runtime gets set to a default value, also defined in that enum. Some example variable values are as follows:
8+
9+
| Variable | Example value |
10+
|----------------|----------------|
11+
| environment | development |
12+
| application | my-special-api |
13+
| app_version | v1 |
14+
| component | read-from-x |
15+
| correlation_id | 1 |
16+
| hostname | 127.0.0.1 |
17+
18+
## Using in projects
19+
To include this library in your project you will need to include the compiled this projects`.jar` file. You are also required to add a logback XML file in the resources for your project, and add the following code as an `appender`. This will inform the logging framework to use `LoggerLayoutAppender` to parse messages into our format.
20+
```xml
21+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
22+
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
23+
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
24+
<layout class="app.utils.logging.LoggerLayoutAppender"/>
25+
</encoder>
26+
</appender>
27+
```
28+
29+
## Custom fields in log messages
30+
For the case where you would like to add custom values to the logging lines which are output, `LogFields` provides functionality to do this.
31+
```kotlin
32+
LogFields.put("custom_env_var", "default value")
33+
val customLogField = LogFields.get("custom_env_var")
34+
```
35+
This functionality is ideal for use cases where an application would like to present information which is specific to its running.
36+
37+
Values associated to the custom variables are resolved in the same manner as common fields, i.e:
38+
1. Environment variable with the specified name
39+
2. Java System property with the specified name
40+
3. Default value provided to `LogFields.put()`
41+
42+
Log lines output with custom fields look like the following (without pretty printing):
43+
```json
44+
{
45+
"timestamp": "1970-04-25T07:29:03.210",
46+
"log_level": "WARN",
47+
"message": "some message about stuff",
48+
"customKey1": "Custom value 1",
49+
"customKey2": "Custom value 2",
50+
"thread": "my.thread.is.betty",
51+
"logger": "logger.name.is.mavis",
52+
"duration_in_milliseconds": "-1573885286308",
53+
"environment": "test",
54+
"application": "tests",
55+
"app_version": "v1",
56+
"component": "tests",
57+
"correlation_id": "1",
58+
"hostname": "127.0.0.1"
59+
}
60+
```
61+
62+
## Example log calls
63+
Below are some examples of how you can use the library to output logs.
64+
65+
##### Including a DataworksLogger in a class
66+
```kotlin
67+
companion object {
68+
val logger = DataworksLogger.getLogger("thisClass")
69+
}
70+
```
71+
72+
##### Writing a log at DEBUG for data output program
73+
```kotlin
74+
val logger = DataworksLogger.getLogger("thisClass")
75+
76+
// Using Kotlin-like syntax (preferred):
77+
logger.debug("Written manifest", "attempt_number" to "${attempts + 1}", "manifest_size" to "$manifestSize", "s3_location" to "s3://$manifestBucket/$manifestPrefix/$manifestFileName")
78+
79+
// Using Java-like syntax
80+
logger.debug("Written manifest", Pair("attempt_number", "${attempts + 1}"), Pair("manifest_size", "$manifestSize"), Pair("s3_location", "s3://$manifestBucket/$manifestPrefix/$manifestFileName"))
81+
```
82+
83+
## Outputs
84+
Some example outputs are displayed below. Note that in actuality these will be on a single line, the formatting here is shown for ease of reading.
85+
##### Fully populated
86+
```json
87+
{
88+
"timestamp": "1970-04-25T07:29:03.210",
89+
"log_level": "WARN",
90+
"message": "some message about stuff",
91+
"thread": "my.thread.is.betty",
92+
"logger": "logger.name.is.mavis",
93+
"duration_in_milliseconds": "-1573885286308",
94+
"environment": "test",
95+
"application": "tests",
96+
"app_version": "v1",
97+
"component": "tests",
98+
"correlation_id": "1",
99+
"hostname": "127.0.0.1"
100+
}
101+
```
102+
103+
##### With thrown exception
104+
```json
105+
{
106+
"timestamp": "1970-04-25T07:29:03.210",
107+
"log_level": "WARN",
108+
"message": "some message about stuff",
109+
"exception": "java.lang.RuntimeException: boom1 - \/:'!@\u00A3$%^&*() | at <omitted> | at <omitted> | ... 89 common frames omitted",
110+
"thread": "my.thread.is.betty",
111+
"logger": "logger.name.is.mavis",
112+
"duration_in_milliseconds": "-1573885286308",
113+
"environment": "test",
114+
"application": "tests",
115+
"app_version": "v1",
116+
"component": "tests",
117+
"correlation_id": "1",
118+
"hostname": "127.0.0.1"
119+
}
120+
```
121+

build.gradle.kts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
plugins {
4+
kotlin("jvm") version "1.3.21"
5+
}
6+
7+
group = "uk.gov.dwp.dataworks"
8+
9+
repositories {
10+
mavenCentral()
11+
jcenter()
12+
}
13+
14+
configurations.all {
15+
exclude(group="org.slf4j", module="slf4j-log4j12")
16+
}
17+
18+
dependencies {
19+
// Kotlin things
20+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
21+
// Logging things
22+
implementation("ch.qos.logback:logback-core:1.2.3")
23+
implementation("ch.qos.logback:logback-classic:1.2.3")
24+
// Utility things
25+
implementation("org.apache.commons:commons-text:1.8")
26+
27+
// JUnit things
28+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
29+
testImplementation("org.junit.jupiter:junit-jupiter-params:5.6.0")
30+
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.3")
31+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
32+
33+
// Testing helper things
34+
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
35+
testImplementation("org.assertj:assertj-core:3.15.0")
36+
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.10.3")
37+
}
38+
39+
tasks.withType<KotlinCompile> {
40+
kotlinOptions {
41+
freeCompilerArgs = listOf("-Xjsr305=strict")
42+
jvmTarget = "1.8"
43+
}
44+
}
45+
46+
tasks.withType<Test> {
47+
useJUnitPlatform()
48+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=0.0.1

gradle/wrapper/gradle-wrapper.jar

53.9 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

gradlew

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)