Skip to content

Commit

Permalink
Add simple log4j and logback example
Browse files Browse the repository at this point in the history
  • Loading branch information
carlphilipp committed Apr 25, 2020
1 parent d16e476 commit 3e6f8d9
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 45 deletions.
21 changes: 17 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormat
import io.freefair.gradle.plugins.lombok.tasks.GenerateLombokConfig
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED
import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
Expand All @@ -13,6 +15,7 @@ plugins {
`maven-publish`
checkstyle
id("com.github.sherter.google-java-format") version Version.google_format_gradle_plugin
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

subprojects {
Expand All @@ -23,6 +26,7 @@ subprojects {
apply(plugin = "maven-publish")
apply(plugin = "com.github.sherter.google-java-format")
apply(plugin = "checkstyle")
apply(plugin = "io.freefair.lombok")

repositories {
mavenCentral()
Expand All @@ -34,12 +38,16 @@ subprojects {
}

checkstyle {
version = 8.31
version = Version.checkstyle_gradle_plugin.toDouble()
isIgnoreFailures = false
maxErrors = 0
maxWarnings = 0
}

lombok {
setVersion(Version.lombok)
}

publishing {
publications {
create<MavenPublication>("maven") {
Expand All @@ -63,7 +71,7 @@ subprojects {
id.set("carl")
name.set("Carl-Philipp Harmant")
email.set("[email protected]")
organization.set("Slalom Build")
organization.set("Slalom LLC")
organizationUrl.set("https://www.slalombuild.com")
}
}
Expand Down Expand Up @@ -94,6 +102,11 @@ subprojects {
}

tasks.withType<Checkstyle> {
dependsOn("googleJavaFormat")
val googleJavaFormatTask = tasks.withType<GoogleJavaFormat>()
dependsOn(googleJavaFormatTask)
}
}

tasks.withType<GenerateLombokConfig> {
enabled = false
}
}
4 changes: 3 additions & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ object Version {
const val junit = "5.6.2"
const val log4j2 = "2.13.1"
const val logback = "1.2.3"
const val lombok = "1.18.12"
const val spring = "2.2.6.RELEASE"
const val slf4j = "1.7.30"

// Gradle plugins
const val lombok_gradle_plugin = "5.0.0"
const val checkstyle_gradle_plugin = "8.31"
const val google_format_gradle_plugin = "0.8"
const val lombok_gradle_plugin = "5.0.0"
}
4 changes: 0 additions & 4 deletions compliance-logging-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

description = "Common compliance library containing the masking logic"

dependencies {
Expand Down
2 changes: 0 additions & 2 deletions compliance-logging-common/lombok.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

public class JsonMaskMaskService extends AbstractMaskService {

private static final String JSON_REPLACEMENT_REGEX = "\"$1\":$2\"" + DEFAULT_MASK + "\"";
private static final String JSON_PATTERN = "\"(%s)\":([ ]*)\"([^\"]+)\"";
private static final String JSON_REPLACEMENT_REGEX = "\"$1\"$2:$3\"" + DEFAULT_MASK + "\"";
private static final String JSON_PATTERN = "\"(%s)\"([ ]*):([ ]*)\"([^\"]+)\"";

private final Pattern pattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ private static Stream<Arguments> source() {
ImmutableList.of("password"),
"{\"password\": \"1234\"}",
"{\"password\": \"***********\"}"),
arguments(
ImmutableList.of("password"),
"{\"password\" : \"1234\"}",
"{\"password\" : \"***********\"}"),
arguments(
ImmutableList.of("password"),
"{\"password\":\"1234\"} log after",
Expand Down
11 changes: 3 additions & 8 deletions compliance-logging-log4j2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
plugins {
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

description = "Compliance Log4j2 library"

dependencies {
implementation(platform("org.apache.logging.log4j:log4j-bom:${Version.log4j2}"))
api(platform("org.apache.logging.log4j:log4j-bom:${Version.log4j2}"))
api(project(":compliance-logging-common"))

implementation(group = "org.apache.logging.log4j", name = "log4j-api")
implementation(group = "org.apache.logging.log4j", name = "log4j-core")
api(group = "org.apache.logging.log4j", name = "log4j-api")
api(group = "org.apache.logging.log4j", name = "log4j-core")

testImplementation(group = "org.junit.jupiter", name = "junit-jupiter")
}
2 changes: 0 additions & 2 deletions compliance-logging-log4j2/lombok.config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.slalom.logging.compliance.log4j2.mask;

import static com.slalom.logging.compliance.common.MaskType.JSON_MARKER_NAME;
import static com.slalom.logging.compliance.common.MaskType.LOMBOK_MARKER_NAME;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MaskType {
public static Marker JSON = MarkerManager.getMarker(JSON_MARKER_NAME);
public static Marker LOMBOK = MarkerManager.getMarker(LOMBOK_MARKER_NAME);
}
8 changes: 2 additions & 6 deletions compliance-logging-logback/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
plugins {
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

description = "Compliance Logback library"

dependencies {
api(project(":compliance-logging-common"))
implementation(group = "ch.qos.logback", name = "logback-core", version = Version.logback)
implementation(group = "ch.qos.logback", name = "logback-classic", version = Version.logback)
api(group = "ch.qos.logback", name = "logback-core", version = Version.logback)
api(group = "ch.qos.logback", name = "logback-classic", version = Version.logback)

testImplementation(group = "org.junit.jupiter", name = "junit-jupiter")
}
2 changes: 0 additions & 2 deletions compliance-logging-logback/lombok.config

This file was deleted.

7 changes: 7 additions & 0 deletions examples/simple-log4j2-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
implementation(project(":compliance-logging-log4j2"))
implementation(group = "com.fasterxml.jackson.core", name = "jackson-databind", version = Version.jackson)
}

// Do not publish artifact
project.tasks.publishMavenPublicationToMavenLocal.get().enabled = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.slalom.logging.compliance.example;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.slalom.logging.compliance.log4j2.mask.MaskType;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SimpleLog4j2Example {

private static final Logger LOGGER = LogManager.getLogger(SimpleLog4j2Example.class);

public static void main(String[] args) throws JsonProcessingException {
final User user =
User.builder().login("mylogin").password("mypassword").ssn("123-123-1234").build();
LOGGER.info(
"=================================================================================");
LOGGER.info("No Marker: {}", user);
LOGGER.info(MaskType.LOMBOK, "Marker Lombok: {}", user);
LOGGER.info(MaskType.JSON, "Marker Json: {}", new ObjectMapper().writeValueAsString(user));
LOGGER.info(
"=================================================================================");
}

@Data
@ToString
@Builder
public static class User {
private final String login;
private final String password;
private final String ssn;
}
}
18 changes: 18 additions & 0 deletions examples/simple-log4j2-example/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration tatus="WARN" packages="com.slalom.logging">
<Properties>
<Property name="COMPLIANCE_FIELDS">password,ssn</Property>
<Property name="CONSOLE_LOG_PATTERN">%d{HH:mm:ss} [%t] %-5level %logger{36} - %mm{${COMPLIANCE_FIELDS}}%n
</Property>
</Properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}"/>
</Console>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</Configuration>
7 changes: 7 additions & 0 deletions examples/simple-logback-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
implementation(project(":compliance-logging-logback"))
implementation(group = "com.fasterxml.jackson.core", name = "jackson-databind", version = Version.jackson)
}

// Do not publish artifact
project.tasks.publishMavenPublicationToMavenLocal.get().enabled = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.slalom.logging.compliance.example;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.slalom.logging.compliance.common.MaskType;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleLogbackExample {

private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLogbackExample.class);

public static void main(String[] args) throws JsonProcessingException {
final User user =
User.builder().login("mylogin").password("mypassword").ssn("123-123-1234").build();
LOGGER.info(
"=================================================================================");
LOGGER.info("No Marker: {}", user);
LOGGER.info(MaskType.LOMBOK, "Marker Lombok: {}", user);
LOGGER.info(MaskType.JSON, "Marker Json: {}", new ObjectMapper().writeValueAsString(user));
LOGGER.info(
"=================================================================================");
}

@Data
@ToString
@Builder
public static class User {
private final String login;
private final String password;
private final String ssn;
}
}
15 changes: 15 additions & 0 deletions examples/simple-logback-example/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.slalom.logging.compliance.logback.PatternMaskingLayout">
<fields>password,ssn</fields>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</layout>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
4 changes: 0 additions & 4 deletions examples/spring-boot-log4j2-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:${Version.spring}"))
implementation(platform("org.apache.logging.log4j:log4j-bom:${Version.log4j2}"))
Expand Down
2 changes: 0 additions & 2 deletions examples/spring-boot-log4j2-example/lombok.config

This file was deleted.

4 changes: 0 additions & 4 deletions examples/spring-boot-logback-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
id("io.freefair.lombok") version Version.lombok_gradle_plugin
}

dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:${Version.spring}"))
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
Expand Down
2 changes: 0 additions & 2 deletions examples/spring-boot-logback-example/lombok.config

This file was deleted.

8 changes: 6 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ include(
":compliance-logging-log4j2",
":compliance-logging-logback",
":spring-boot-log4j2-example",
":spring-boot-logback-example"
":spring-boot-logback-example",
":simple-log4j2-example",
":simple-logback-example"
)

project(":spring-boot-log4j2-example").projectDir = file("examples/spring-boot-log4j2-example")
project(":spring-boot-logback-example").projectDir = file("examples/spring-boot-logback-example")
project(":spring-boot-logback-example").projectDir = file("examples/spring-boot-logback-example")
project(":simple-log4j2-example").projectDir = file("examples/simple-log4j2-example")
project(":simple-logback-example").projectDir = file("examples/simple-logback-example")

0 comments on commit 3e6f8d9

Please sign in to comment.