Java Logging APIs Tutorial

This tutorial shows logging using the Java built-in logging APIs in the java.util.logging package. Table of contents 1. Java Logging APIs Hello World 2. Logging Levels 3. logging.properties 4. java.util.logging.config.file 5. Why choose java.util.logging 6. Download Source Code 7. References P.S The java.util.logging is bundled with the Java since JDK 1.4. 1. Java Logging APIs …

Read more

logging.properties example

The Java logging APIs (java.util.logging) default loads logging.properties in the $JAVA_HOME/jre/lib/ (Java 8 and before); for Java 9 and above, the logging.properties file moved to $JAVA_HOME/conf. Note Java Logging APIs Tutorial logging.properties Here is the logging.properties file from Java 11. C:\opt\jdk-11.0.1\conf ############################################################ # Default Logging Configuration File # # You can use a different file …

Read more

How to load logging.properties for java.util.logging

In Java Logging APIs or java.util.logging, we use system property java.util.logging.config.file to define the location of the logging.properties file. Table of contents 1. Loads logging.properties at runtime 2. Loads logging.properties from the classpath 2.1 LogManager 2.2 System.setProperty("java.util.logging.config.file") 3. Download Source Code 4. References Note Java Logging APIs Tutorial 1. Loads logging.properties at runtime In the …

Read more

log4j2.yml example

A simple log4j2.yml example, just for self-reference P.S Tested with Log4j 2.11.2 1. Jackson for YML Log4j 2 need the following libraries to parse yml file pom.xml <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> 2. log4j2.yml src/resources/log4j2.yml Configuration: status: warn appenders: Console: …

Read more

log4j2.properties example

A simple log4j2.properties example, just for self-reference P.S Tested with Log4j 2.11.2 src/resources/log4j2.properties status = warn appender.console.type = Console appender.console.name = LogToConsole appender.console.layout.type = PatternLayout appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n #appender.file.type = File #appender.file.name = LogToFile #appender.file.fileName=logs/app.log #appender.file.layout.type=PatternLayout #appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n # Rotate log file appender.rolling.type = …

Read more

log4j2.xml example

Some log4j2.xml examples, just for self-reference P.S Tested with Log4j 2.11.2 1. ConsoleAppender Logs to console. log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration status="DEBUG"> <Appenders> <Console name="LogToConsole" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} – %msg%n"/> </Console> </Appenders> <Loggers> <!– avoid duplicated logs with additivity=false –> <Logger name="com.mkyong" level="debug" additivity="false"> <AppenderRef ref="LogToConsole"/> </Logger> <Root level="error"> <AppenderRef ref="LogToConsole"/> </Root> …

Read more

Log4j 2 – java.lang.NoClassDefFoundError: com/lmax/disruptor/EventTranslatorVararg

Enable the log4j 2 loggers to asynchronous, but hits the following order : $ mvn -Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -jar app.jar java.lang.NoClassDefFoundError: com/lmax/disruptor/EventTranslatorVararg at java.lang.ClassLoader.defineClass1 (Native Method) at java.lang.ClassLoader.defineClass (ClassLoader.java:1009) at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:174) at java.net.URLClassLoader.defineClass (URLClassLoader.java:545) at java.net.URLClassLoader.access$100 (URLClassLoader.java:83) at java.net.URLClassLoader$1.run (URLClassLoader.java:453) Solution To fix it, add disruptor pom.xml <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency> Note Read this Making …

Read more

Apache Log4j 2 Tutorials

A simple log4j 2 hello world example. Tested with Log4j 2.11.2 Maven 3 Java 8 Note Apache Log4j 2, the fastest Java logging framework, provides significant improvements over its predecessor, Log4j 1.x. 1. Project Directory 2. Maven <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.2</version> </dependency> 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 …

Read more

How to stop logback status INFO at the start of every log?

The logbook will output its own status (INFO or WARN) at the start of the program, it’s really annoying! 15:34:46,181 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback.groovy] 15:34:46,181 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback-test.xml] 15:34:46,181 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Found resource [logback.xml] at [file:/E:/spring-boot-simple/target/classes/logback.xml] 15:34:46,247 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction – debug …

Read more

Logback – Set log file name programmatically

In Logback, it is easy to set a log file name programmatically : In logback.xml, declares a variable like ${log.name} In Java, set the variable via System.setProperty(“log.name”, “abc”) 1. Full example 1.1 A logback file, we will set the ${log.name} variable later. src/main/resources/logback.xml <?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="PRO_HOME" value="/home/mkyong/ant/logs" /> <property name="USER_HOME" value="${PRO_HOME}" /> …

Read more

Logback – Disable logging in Unit Test

While the unit test is running in the IDE, the Logback is showing a lot of configuration or status like this : 21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback.groovy] 21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback-test.xml] 21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Found resource [logback.xml] at … //… omitted for …

Read more

Logback – different log file for each thread

In this tutorial, we will show you how to use Logback Mapped Diagnostic Context (MDC) and SiftingAppender to create a separate log file for each thread. P.S Tested with Logback 1.1.2, should work in earlier version. Note More info, refer to this Logback MDC documentation 1. logback.xml example A logback.xml file to show you how …

Read more

logback.xml Example

Here are a few logback.xml examples that are used in my projects, just for sharing. P.S Tested with Logback 1.2.3 1. Send logs to Console All logging will be redirected to console. logback.xml <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} – %msg%n </Pattern> </layout> </appender> <logger name="com.mkyong" level="debug" additivity="false"> <appender-ref ref="CONSOLE"/> …

Read more

Logback – Duplicate log messages

Review a simple Java application and log a message via Logback. App.java package com.mkyong.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { private static final Logger log = LoggerFactory.getLogger(App.class); public static void main(String[] args) { log.debug("Testing"); } } P.S Tested with Logback 1.1.2 1. Problem A simple logback.xml to log a message to console. logback.xml …

Read more

Log4j hello world example

In this tutorial, we will show you how to use the classic log4j 1.2.x to log a debug or error message in a Java application. 1. Project Directory Review the final project structure, a standard Maven style Java project. 2. Get Log4j Declares the following dependencies : pom.xml <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> For non-Maven …

Read more

log4j.xml Example

Here’s an XML version of log4j properties file, just for sharing. 1. Output to Console Redirect the logging to console. log4j.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j=’http://jakarta.apache.org/log4j/’> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L – %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> …

Read more

log4j.properties example

I can’t find many log4j.properties examples, here are a few log4j.properties examples that are used in my project, just for sharing. 1. Output to Console All logging will be redirected to your console. log4j.properties # Root logger option log4j.rootLogger=INFO, stdout # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L – %m%n …

Read more