Created
July 10, 2023 17:10
-
-
Save Stwissel/06e5c11bf5829fcdbbbd7318d5da85b0 to your computer and use it in GitHub Desktop.
Getting mvn and log4j2 to work nicely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Console appender configuration | |
appender.console.type = Console | |
appender.console.name = consoleLogger | |
appender.console.layout.type = PatternLayout | |
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n | |
# Root logger level | |
rootLogger.level = trace | |
# Root logger referring to console appender | |
rootLogger.appenderRef.stdout.ref = consoleLogger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>starter</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> | |
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> | |
<junit-jupiter.version>5.9.1</junit-jupiter.version> | |
<log4j.version>2.20.0</log4j.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.junit.jupiter</groupId> | |
<artifactId>junit-jupiter-api</artifactId> | |
<version>${junit-jupiter.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.junit.jupiter</groupId> | |
<artifactId>junit-jupiter-engine</artifactId> | |
<version>${junit-jupiter.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<!-- Logging using Log4j2 --> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-api</artifactId> | |
<version>${log4j.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-core</artifactId> | |
<version>${log4j.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-jul</artifactId> | |
<version>${log4j.version}</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>${maven-compiler-plugin.version}</version> | |
<configuration> | |
<release>11</release> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-surefire-plugin</artifactId> | |
<version>${maven-surefire-plugin.version}</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Maven needs to be on th path | |
mvn -Dlog4j2.configurationFile=$HOME/Code/junit-maven/log4j2.properties clean test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.starter; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
public class TestMain { | |
Logger logger = LogManager.getLogger(TestMain.class); | |
@BeforeEach | |
void beforeEach() { | |
logger.info("Running in BeforeEach"); | |
} | |
@Test | |
void test1() { | |
String msg = "Hello World"; | |
logger.warn(msg); | |
assertEquals(11, msg.length(), "Is Hello World 11 chars?"); | |
logger.debug("Test1 completed"); | |
} | |
@Test | |
void test2() { | |
try { | |
throw new Exception("'FAIL'"); | |
} catch (Exception e) { | |
logger.error(e); | |
} | |
} | |
@Test | |
void test3() { | |
String msg = "Hello World"; | |
assertEquals(11, msg.length(), "Is Hello World 11 chars, again?"); | |
logger.trace("Test3 completed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment