Spring 5 Framework Logging with slf4j/log4j2 - maven

I have a application that I've put together to become familiar with Spring Framework 5, using Maven and Java 8. Once executed, it extracts some information from a PostgreSQL database table and logs it to the console (via slf4j/log4j2).
The actual application logic is working fine - I'm seeing the info retrieved from the database on the console as expected. However, I'm not seeing any of the expected Spring/DBCP2 logging. After the application finishes executing, there are only 15 lines printed on the console - all lines that I specifically logged via logger.debug("...").
The dependencies section of my pom.xml looks like this:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
My log4j2.xml file looks like this...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configuration>
<Configuration status="INFO" strict="true">
<Properties>
<Property name="CONSOLE_PATTERN">%-5level %c{1} %M - %m%n</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${CONSOLE_PATTERN}" />
</Console>
</Appenders>
<Loggers>
<Logger name="local.spring5" level="DEBUG" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="org.apache.commons.dbcp2" level="DEBUG" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Root level="INFO">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
Note that if I change the org.springframework logging level to DEBUG or TRACE, I see spring logging appropriate for those levels. Setting the log level to INFO, WARN or ERROR results in no logging from spring at all. I'm sceptical, to say the least, that spring doesn't log anything at those levels.
Does anyone have any ideas on how to get the spring framework logging to appear?

Related

Log4j2 with SpringBoot (configuration)

I did read other questions, also I've read configuration and I came up with a solution but I don't know if it's okay.
Problem: I have SpringBoot application and I was trying to use log4j2 as logging framework but I couldn't configure it properly. I had correct xml configuration, configuration was on a classpath but I was able to log only these 4 log levels:
INFO, WARN, ERROR and FATAL
By log4j2 documentation, if configuration is not found, default configuration is used and that configuration display only ERROR and FATAL, but mine app was showing 4 log levels as I wrote earlier. I found that behavior bizarre so I kept reading articles and I came up with this solution.
I replaced these 3 dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
With these 2:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
And it works, but it feels wrong. By excluding spring-boot-starter-logging my log4j2.xml has power again.
This is my (log4j2) XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
<Property name="APP_LOG_ROOT">c:/temp</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="appLog"
fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.howtodoinjava.app" additivity="false">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
And java class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MyApp{
private static final Logger logger = LogManager.getLogger(MyApp.class);
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
logger.fatal("HoHoHo FATAL");
logger.debug("HoHoHo DEBUG");
logger.trace("HoHoHo TRACE");
logger.info("HoHoHo INFO");
logger.warn("HoHoHo WARN");
logger.error("HoHoHo ERROR");
}
}
So now if I set Root level="trace" in my log4j2.xml file, I will see all these HoHoHo logs in a console which is what I want. If I set Root level="error" I will see only error and fatal which is also okay. However, I noticed that by changing these levels logs, my application shows a bit different logs while starting so I'm not sure if that is okay or not.. I'm wondering if I configured everything the way it should be configured.
These 2 images are showing what is different when I change log levels between trace and error.
So in one example I have ~1150 lines and with other approach I have ~1200 and they're mostly identical besides this. And as you can see, when I use trace as logging level, my application doesn't start with Spring drawing on a first line in a console.
Sorry if post is too long or unclear, so I'll wrap my question up once again in one sentence. Did I configure my log4j2 correctly (check 2 dependencies I'm using instead of 3 that I removed). Thanks!
Everything is configured correctly.
Also check out https://www.baeldung.com/spring-boot-logging#log4j2-configuration-logging

Log4j2.xml with springboot is not generating log file

I am upgrading log4j1 to logj42 in one of the springBoot porject (maven, Java 1.8) with log4j-core,log4j-api as maven dependency.
Log file mentioned in log4j2.xml as Rolling file appender is not creating log file, I am able to see the logs in console but no log file. Please reply if anyone has faced same issue.
By default spring boot contains an integration with logback logging library.
So you'll have to exclude the "default" and add a special starter that will handle the integration:
In the pom.xml make sure you have the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Optional: If you want to use an asynchronous logging, add also the following dependency:
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version> <!-- check the actual version it might be different -->
</dependency>
After this step you can start configuring the log4j2. There are plenty of sample configurations and tutorials about this, examples: here and here.
I won't dive into possible configurations of log4j2, but from the spring boot's standpoint I'll mention that you should place the log4j2's configurations into src/main/resources/log4j2.xml
You can try with the below log4j2.xml. This is working for me.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Properties>
<Property name="basePath">/home/user/myapp/logs</Property>
</Properties>
<Appenders>
<!-- File Appender -->
<File name="FILE" fileName="${basePath}/my-app.log" append="true">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</File>
<!-- Console Appender -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.abc.xyz" level="debug" />
<Root level="info">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>
Change the log file name and path accordingly.
Also change the package for the logger. <Logger name="com.abc.xyz" level="debug" />

No appenders could be found for logger in local server but same application works fine in Production server- log4j

We have two servers. One for QA and another is Production server (hosted on AWS). I have used "apache-tomcat-8.5.16" server in both environment. We have developed Spring boot app and Log4j configuration is same for applications deployed in both servers.
logging.level.net.companyname= DEBUG
logging.level.com.google.api.ads.dfp.lib.client.DfpServiceClient.soapXmlLogger=WARN
logging.level.com.google.api.ads.dfp.lib.client.DfpServiceClient.requestInfoLogger=INFO
Yet, in local server (used for QA), we are getting the error "log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment" .
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
But in Production server we are getting logs without any problem.
If it does matter, below is the pom.xml configuration related to log4j.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
Even configurations inside "apache-tomcat-8.5.16/conf/logging.properties" file are also same in both servers.
How to get logs as we are getting in Production environment?
What else would make difference when both tomcat server versions are same and same war is deployed on both of them with same configuration settings?
Thank you.
Update:
I downloaded the war file to local from server for testing and even there, log4j is working fine. What else do I need to check?
Thank you.
This is an alternative and recommended solution.
After waiting for a week, I thought, why should I worry on framework that has reached it's end of life. I upgraded to log4j2 and provided log4j2.xml file in the classpath (in my case, /src/main/resources, working in STS-Spring Tool Suite, Spring application.).
However the actual reason for this weird behavior is still unknown.
Below is the pom.xml file tailored only log4j2 dependency for verbosity.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - ***CustomLog*** %msg ***/CustomLog***%n" />
</Console>
<File name="traceLoggerFile" fileName="logs/trace.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="debugLoggerFile" fileName="logs/debug.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="infoLoggerFile" fileName="logs/info.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="errorLoggerFile" fileName="logs/errors.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.packageName" level="TRACE">
<AppenderRef ref="traceLoggerFile" level="TRACE" />
<AppenderRef ref="debugLoggerFile" level="DEBUG" />
<AppenderRef ref="infoLoggerFile" level="INFO" />
<AppenderRef ref="errorLoggerFile" level="ERROR" />
</Logger>
<Root level="DEBUG">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
You need to give proper package name or class name in this line
<Logger name="com.packageName" level="TRACE">
Will be happy if anyone can find the actual reason.
Thank you.

Direct Spring DEBUG logging to Console

I have a Maven project and I want to direct DEBUG Spring logging to the console.
My log interface is slf4j and I'm binding to Log4j2.
In my pom.xml I have excluded commons-logging in favour or Slf4j:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
I believe slf4j is wired up correctly because I have this in my log4j2.xml:
<Logger name="com.myApp" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
This in my test class constructor:
logger.info("constructor");
logger.debug("constructor");
and if i flip the log level in log4j2.xml from info/debug I get one or two log lines in the console.
I also have this in my log4j2.xml:
<Logger name="org.springframework" level="all" additivity="false">
<AppenderRef ref="Console" />
</Logger>
But it has zero effect. What am I missing? Why doesn't Spring log detailed debug lines to the console?
Found it if it helps anyone else. This dependency was missing:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.1</version>
</dependency>
So logging looks like its working, but Spring logging is not.

Can't get a RollingFile working with log4j2

I'm trying to configure log4j2 to write a rolling log file to disk, but I can't get it to work. No log file appears at the given path and the Glassfish server.log doesn't show any Spring logging at all. I've read a lot of similar questions on SO, but none of the proposed solutions have worked in this case. Can anyone help me? I'm using Spring 3.0 on a Glassfish 3.1 application server.
From my pom.xml:
<properties>
<junit.version>4.11</junit.version>
<tiles.version>3.0.3</tiles.version>
<slf4j.version>1.7.5</slf4j.version>
<log4j.version>2.0-beta9</log4j.version>
</properties>
<dependencies>
<dependency>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
<version>${slf4j.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-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
And here's my log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="fileName" value="C:/temp/rolling-file.log"/>
<Property name="fileNamePattern" value="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log"/>
<Property name="logPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<RollingFile name="RollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.root.package" level="info" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Besides the excellent point made by M. Deinum, it turns out my log4j2.xml needed to be changed to the following:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="fileName">C:/temp/rolling-file.log</Property>
<Property name="fileNamePattern">C:/temp/rolling-file-%d{dd-MM-yyyy}-%i.log</Property>
<Property name="logPattern">%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<RollingFile name="MyRollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.root.package" level="info" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyRollingFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Performed changes:
The value attributes of the Property elements were not parsed correctly. Specifying them as body of the Property element seems to solve this.
There was a mistake in the value of the fileNamePattern property. I used $d to denote the date, but it should be %d.
Apparently log4j2 doesn't like a RollingFile element with the name "RollingFile". After I changed it, logging started working.
You might need to include log4j-api to the pom dependencies (http://logging.apache.org/log4j/2.x/faq.html#which_jars ).
About the properties: if the problem goes away by putting the values in the config directly:
<RollingFile name="RollingFile" fileName="C:/temp/rolling-file.log"
filePattern="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log">
<PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>
then you may have found a bug. In that case, could your raise this issue in the log4j2 Jira issue tracker?

Resources