Deploying spring-boot application to WildFly 8 causes container logs to be redirected - spring-boot

I'm trying to deploy a spring-boot application using logback to an existing WildFly 8 container, but as soon as the application has deployed all the container logs stop being written to server.log and end up being captured by the application logs.
The symptoms are similar to those described in WildFly not logging after deploying app with Logback. If I deploy an application which manually contains logback/slf4j dependencies, the application behaves as I'd expect (application logs go to application logfile, container logs go to server.log), so I'm assuming it's something to do with spring-boot? Can I configure this such that the container logging continues to be handled by Wildfly?
pom.xml
<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>com.test.logging</groupId>
<artifactId>logger-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="path_base" value="d:/test-logger/" />
<property name="pattern"
value="%date{ISO8601} %level %class{30} %thread %msg %mdc %ex{full} %n" />
<!-- Simple File Appender -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${path_base}test-logger.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${path_base}/archive/%d{yyyy-MM,aux}/test-logger.log.%d.%i.gz
</fileNamePattern>
<maxHistory>90</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%date{ISO8601} %level %class{30} %thread %msg %mdc %ex{full} %n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="file" />
</root>
</configuration>
jboss-deployment.xml
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
Spring boot initializer
package com.test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
#SpringBootApplication
public class BootApp extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(BootApp.class);
}
}

I had similar problem when I deployed spring-boot application to websphere. After application prints banner all the log messages from websphere are logged into application log.
The problem occurred because spring-boot adds as one of its dependencies jul-to-slf4j.jar . This jar implements classes java.util.logging.* and websphere is also using java.util.logging hence by excluding jul-to-slf4j.jar from spring boot I was able to direct all websphere messages to container log and the application logs were written to application log.

I had a similar problem:
Running Spring-boot 1.5.8 on Wildfly 9.0.2Final
I had to exclude the following before the logging was handled by the container:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
spring-boot-starter-data-jpa has spring-boot-starter-logging which had the dependencies i excluded.

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

ERROR in ch.qos.logback.core.joran.spi.Interpreter- no applicable action for [maxHistory]

When i am using spring boot in my application i am getting below error while starting the spring boot application main method. Please note that application is running in kotlin and we are triggering spring boot start up from a seperate class. when i run the application from workspace it run successfully and start up but launching the application from a packaged jar throws this error.
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter#20:21 - no applicable action for [maxHistory], current ElementPath is [[configuration][appender][maxHistory]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter#37:21 - no applicable action for [maxHistory], current ElementPath is [[configuration][appender][maxHistory]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter#54:21 - no applicable action for [maxHistory], current ElementPath is [[configuration][appender][maxHistory]]
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:222)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:73)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:306)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:80)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
without using spring boot it is working absolutely fine. I saw this post
Logback file error: no applicable action for [configuration], current ElementPath is [[configuration][configuration]] but it didn't help.
my logback.xml:
The tag in the logback is creating the issue how to resolve it ?
<appender name="HealthRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${app.logpath:-log}/${log.base.name}-health.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${app.logpath:-log}/${log.base.name}-health-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<!-- Number of days of log files to keep -->
<maxHistory>14</maxHistory>
</appender>
My pom has multipl dependencies but below are some springboot dependencies which this code uses:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
``` <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>```
<version>5.3.8</version>
</dependency>
I checked multiple posts on this forum but there doesn't seems to be a solution fitting in this case. without springboot the application running fine.
Edit your logback file as follows:
<appender name="HealthRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${app.logpath:-log}/${log.base.name}-health.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${app.logpath:-log}/${log.base.name}-health-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<!-- Number of days of log files to keep -->
<maxHistory>14</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

Spring Boot SLF4j Logback

I have a small SpringBoot MvC app. with those dependencies:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
I put a logback-test.xml file in the /src/test/resources/ and when I run the test it works fine
<configuration debug="true" scan="true" scanPeriod="150 seconds">
<property name="LOG_DIR" value="logs-test" />
<appender name="FILE_INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/app_info.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
%msg%n
</Pattern>
....
</configuration>
but when I add to the file /src/test/resources/application.properties:
# logging level
logging.level.root=error
logging.level.org.springframework=ERROR
logging.level.com.plats.bruts=ERROR
but it seems not to work, because I see the DEBUG level on the console when running the test:
10:16:08.039 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class eu.europa.ec.oib.kw.frontoffice.repository.autorisation.AutorisationPersonneRepositoryTest]
10:16:08.047 [main] DEBUG org.springframework.test.context.BootstrapUtils
Make sure to use #SpringBootTest annotation when testing spring boot apps for spring boot to load default logback logging.
For the most part you can configure without needing custom logback xml file and can be managed using application.properties.
For non spring boot test cases you've to provide your own logback xml file.
For custom configuration you should build your file based on base.xml default provided in the spring boot library ( has both file and console appender defined) so you can import the defaults and add specific configuration based on your need.
This way you still manage the default configuration attributes from application properties for spring boot tests. Also rename the file to end with spring extension so spring boot can take control of logging initialization.
Something like
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
!-- custom configuration goes here --!
</configuration>

Spring output console dissapeared after removing logback dependency

I am trying to use log4j2 as logger, for that i have included
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
and used
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
in every dependency that used logback due to conflict, however after doing this, the spring upon running prints only its Logo and nothing.
DO i need to configure somehow spring for log4j2 or how to solve this?
Yes offcourse. You need to create log4j2.xml in src/main/resources path.
In that Xml file you need to configure your application logging level in ConsoleAppender.
Please add this xml configuration in log4j2.xml.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender"></AppenderRef>
</Root>
</Loggers>
</Configuration>

Tomcat logs are being written to syslog instead to the application log in Spring Boot and Log4j

It's a Spring Boot application with Log4j for logging.
All logs from org.apache.catalina.* are being sent to syslog, and it includes also uncaught exceptions.
Our app uses Log4j (v1) since it uses legacy code dependencies that use Log4j.
Spring Boot version is 1.5.2.RELEASE.
I believe that with correct set-up, also tomcat logs will be written to the same file that is configured in the log4j.xml, but i couldn't find any answer to that problem.
pom.xml (relevant parts):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
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="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%t] %5p %c{1} - %m%n" />
</layout>
</appender>
<!-- File appender -->
<appender name="logFileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="/tmp/myappname.log"/>
<param name="Append" value="true" />
<param name="BufferedIO" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%t] %5p %c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender" />
<appender-ref ref="logFileAppender" />
</root>
</log4j:configuration>
Could you try excluding it in the actuator artifact?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Based on your comment:
If I understood correctly, the application is logging fine, it's the servlet container the logging to syslog when exceptions bubble up, isn't?
Could you also try adding:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
</dependency>
and / or passing where log4j config file is located:
-Dlog4j.configuration=<path to file>
If these don't work, the only thing I could think of is that you are using Spring Boot 1.5.x doesn't get along with Log4j and likely requires to uplift it to Log4j2

Resources