Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Testing
|
Logging (slf4j, log4j)
The POM file (pom.xml)
The logging configuration file (log4j.xml)
A very basic template for logging
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.log4j.samples</groupId> <artifactId>mtitek-logging-log4j-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>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>2.0.6</version> <scope>test</scope> </dependency> </dependencies> </project>
The logging configuration file (log4j.xml)
src/test/resources/log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p [%d{ISO8601}] \(%F: %L\) %c - %m%n" /> </layout> </appender> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="./target/mtitek-logging-log4j-samples.log" /> <param name="Append" value="false" /> <param name="MaxFileSize" value="20MB" /> <param name="MaxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p [%d{ISO8601}] \(%F: %L\) %c - %m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="ALL" /> </filter> </appender> <logger name="mtitek.logging.log4j.samples" additivity="false"> <level value="INFO" /> <appender-ref ref="file" /> <appender-ref ref="console" /> </logger> <root> <priority value="INFO" /> <appender-ref ref="file" /> <appender-ref ref="console" /> </root> </log4j:configuration>
A very basic template for logging
src/test/java/mtitek/logging/log4j/samples/Log4jTest.java:
package mtitek.logging.log4j.samples; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Log4jTest { private static final Logger LOG = LoggerFactory.getLogger(Log4jTest.class); @Test public void test() { Log4jTest.LOG.info("test - looging - log4j"); System.out.println("test - looging - log4j"); } }
© 2010-2022
mti
tek