• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Testing | Logging (slf4j, logback)
  1. The POM file (pom.xml)
  2. The logging configuration file (logback-test.xml)
  3. A very basic template for logging

  1. The POM file (pom.xml)
    <?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>mtitek.logging.logback.samples</groupId>
        <artifactId>mtitek-logging-logback-samples</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>2.0.6</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.4.5</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
  2. The logging configuration file (logback-test.xml)
    src/test/resources/logback-test.xml:
    <configuration>
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <target>System.out</target>
    
            <encoder>
                <pattern>%p [%d{ISO8601}] \(%F: %L\) %c - %m%n</pattern>
            </encoder>
        </appender>
    
        <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>./target/mtitek-logging-logback-samples.log</file>
    
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>./target/mtitek-logging-logback-samples.%d{yyyy-MM-dd-HH-mm-ss}.%i.log</fileNamePattern>
    
                <maxFileSize>20MB</maxFileSize>
                <maxHistory>10</maxHistory>
                <totalSizeCap>200MB</totalSizeCap>
            </rollingPolicy>
    
            <encoder>
                <pattern>%p [%d{ISO8601}] \(%F: %L\) %c - %m%n</pattern>
            </encoder>
        </appender>
    
        <logger name="mtitek.logging.logback.samples" level="DEBUG" />
    
        <root level="INFO">
            <appender-ref ref="console" />
            <appender-ref ref="file" />
        </root>
    </configuration>
  3. A very basic template for logging
    src/test/java/mtitek/logging/logback/samples/LogbackTest.java:
    package mtitek.logging.logback.samples;
    
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LogbackTest {
        private static final Logger LOG = LoggerFactory.getLogger(LogbackTest.class);
    
        @Test
        public void test() {
            LogbackTest.LOG.info("test - looging - logback");
            System.out.println("test - looging - logback");
        }
    }
© 2025  mtitek