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

Spring Boot Log4j 2 example

In this tutorial, we will show you how to use Apache Log4j 2 in Spring Boot framework. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Log4j 2.11.1 Maven 3 Java 8 1. Project Directory 2. Maven The key is exclude the default logback, and include log4j with spring-boot-starter-log4j2 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" …

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

log4j2 – Failed to load class “org.slf4j.impl.StaticLoggerBinder”

The Java project is using log4j2, but look like some components are used SLF4J logging and caused the following error message: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Solution To fix it, we need a SLF4J Bridge. Puts log4j-slf4j-impl in the classpath. pom.xml …

Read more

Spring Boot – Show Hibernate SQL query

Add the following lines in application.properties to log the Hibernate SQL query. application.properties #show sql statement logging.level.org.hibernate.SQL=debug #show sql values logging.level.org.hibernate.type.descriptor.sql=trace 1. org.hibernate.SQL=debug application.properties logging.level.org.hibernate.SQL=debug 1.1 Select query. Console 2017-02-23 21:36:42 DEBUG org.hibernate.SQL – select customer0_.id as id1_0_, customer0_.created_date as created_date2_0_, customer0_.email as email3_0_, customer0_.name as name4_0_ from customer customer0_ 2. org.hibernate.type.descriptor.sql=trace application.properties logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql=trace …

Read more

Oracle PL/SQL – After DELETE Trigger example

This article shows you how to use AFTER DELETE TRIGGER, it will fire after the delete operation is executed. In real life scenarios, it is mostly used for purposes like: Auditing or logging 1. After DELETE Trigger In this example, if the user deleted a row of medical_bills, the deleted row will be inserted into …

Read more

Spring Boot Logging Example

This article will demonstrate Spring Boot Logging using the Logback SLF4J implementation. Technologies used: Spring Boot 3.2.2 Java 17 Maven 3 Table of contents: 1. Project Directory 2. Spring Boot Logging Dependencies 3. Spring Boot Logging using Logback 3.1. Log Example 3.2 Log variables 4. Log to File 4.1 File Rotation 5. Log Levels 6. …

Read more

Gradle – Exclude commons-logging from Spring

Gradle example to exclude commons-logging from Spring frameworks. build.gradle compile(‘org.springframework:spring-webmvc:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } If you have multiple Spring dependencies, you have to exclude for each build.gradle compile(‘org.springframework:spring-webmvc:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } compile(‘org.springframework:spring-test:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } A better solution is excluding commons-logging at the project level. build.gradle configurations.all …

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

How to configure logging in Hibernate – Logback

In this tutorial, we show how to integrate Logback logging framework with Hibernate. Tools and Technologies used in this tutorial : Hibernate 3.6.3.Final slf4j-api-1.6.1 logback-core-0.9.28 logback-classic-0.9.28 Eclipse 3.6 Maven 3.0.3 1. Get SLF4j + Logback To use logback in Hibernate web application, you need 3 libraries : slf4j-api.jar logback-core logback-classic File : pom.xml <project …> …

Read more

Spring MVC + Log4j example

In this tutorial, we will show you how to use the log4j framework to do the logging in a Spring MVC web application. Technologies and tools used : Log4j 1.2.17 Spring 4.1.6.RELEASE Maven 3 Tomcat 6 Eclipse Kepler 4.3 Note By default, Spring (spring-core) is using the JCL (commons-logging) for logging, and the JCL has …

Read more

How to display hibernate sql parameter values – Log4j

Problem Hibernate has basic logging feature to display the SQL generated statement with show_sql configuration property. Hibernate: INSERT INTO mkyong.stock_transaction (CHANGE, CLOSE, DATE, OPEN, STOCK_ID, VOLUME) VALUES (?, ?, ?, ?, ?, ?) However , it just isn’t enough for debugging, the Hibernate SQL parameter values are missing. Solution – Log4j Log4J is required to …

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

How to configure logging in Hibernate – SLF4j + Log4j

Try logback Try logback logging framework, read this article for the “reasons to prefer logback over log4j. To integrate logback with Hibernate, refer this – How to configure logging in Hibernate – Logback Hibernate uses Simple Logging Facade for Java (SLF4J) to redirect the logging output to your perfer logging frameworkis (log4j, JCL, JDK logging, …

Read more

ClassNotFoundException: org.apache.commons.logging.LogFactory

Starting a web application, but hits the following error messages : … Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory … 1. Normal Case 1.1 Obviously, the Apache Commons logging is missing – commons-logging-xxx.jar. To fix it, get it from Maven central repository. pom.xml <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> 2. Spring Case 2.1 For Spring application, developers always excluded …

Read more