I am trying to run Spring Boot application (.jar) in Unix environment. It get shut down in some time. The application has REST webservice and scheduler component. The same jar runs fine in IDE & Windows CMD.
2021-01-13 16:46:16.591 INFO 6346 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-01-13 16:46:16.592 INFO 6346 --- [extShutdownHook] o.s.i.channel.PublishSubscribeChannel : Channel 'SFTP service.errorChannel' has 0 subscriber(s).
2021-01-13 16:46:16.592 INFO 6346 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer : stopped bean '_org.springframework.integration.errorLogger'
2021-01-13 16:46:16.598 INFO 6346 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler'
2021-01-13 16:46:16.601 INFO 6346 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Scheduler code snippet. I am facing reported issue without scheduler code as well.
#Component
Public class Task{
#Autowired
Private Environment env;
#Autowired
Private ConnectionFactory conn;
#Scheduled(fixedRate=1500)
public void scheduledRun(){
//monitors map
}}
Adding Logger Configuration
<configuration scan="true" scanPeriod="30 seconds">
<jmxConfigurator/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<appender name="SERVICE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/service-debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/service-debug-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy HH:mm:ss.SSS z} [%thread] %X{instructionsId}-%X{fileName} %X{step} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="SERVICE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/service-err.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/service-err-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy HH:mm:ss.SSS z} [%thread] %X{instructionsId}-%X{fileName} %X{step} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="trace" level="TRACE"/>
<springProfile name="prod, uat1">
<logger name="com.service" level="INFO"/>
</springProfile>
<springProfile name="!prod, !uat1">
<logger name="com.service" level="DEBUG"/>
<logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
<appender-ref ref="SERVICE_DEBUG" />
</logger>
</springProfile>
<logger name="org.springframework" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>
<root level="INFO">
<appender-ref ref="SERVICE_DEBUG" />
<appender-ref ref="SERVICE_ERROR" />
<appender-ref ref="FILE"/>
</root>
POM file
<modelVersion>4.0.0</modelVersion>
<groupId>com.servicep</groupId>
<artifactId>sftpservice</artifactId>
<version>1.0.2-SNAPSHOT</version>
<name>sftp-service</name>
<description>SFTP service</description>
<!-- Properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
<java.version>1.8</java.version>
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
</properties>
<!-- Spring Boot Parent to control the version of child dependency-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.0.9.RELEASE</version>-->
<version>2.2.0.RELEASE</version>
</parent>
<!-- Dependencies to build this service - will be imported in project -->
<dependencies>
<!-- Spring web brings all required dependencies to build web application. i.e. REST Service -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Dependency for sftp connection-->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--java library to support the SSH protocols on both the client and server side-->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<!--Unit test dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<!--<version>2.9.8</version>-->
<version>2.10.0</version>
</dependency>
<!--logging-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.9</version>
</dependency>
</dependencies>
<build>
<defaultGoal>spring-boot:run</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
Related
My application is throwing ConcurrentModificationException when it is trying to exit the program.It is using a standard Logback configuration. This works fine when running as a standalone java application but throws the exception when trying to run inside a docker container. The stack trace is as below:
Exception in thread "Logback shutdown hook [default]" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
at ch.qos.logback.classic.LoggerContext.fireOnReset(LoggerContext.java:323)
at ch.qos.logback.classic.LoggerContext.reset(LoggerContext.java:226)
at ch.qos.logback.classic.LoggerContext.stop(LoggerContext.java:348)
at ch.qos.logback.core.hook.ShutdownHookBase.stop(ShutdownHookBase.java:39)
at ch.qos.logback.core.hook.DelayingShutdownHook.run(DelayingShutdownHook.java:57)
at java.base/java.lang.Thread.run(Thread.java:834)
I am using a standard Logback configuration file through logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--See https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html -->
<shutdownHook
class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<!-- <prettyPrint>true</prettyPrint> -->
</jsonFormatter>
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSXXX</timestampFormat>
<includeContextName>false</includeContextName>
<appendLineSeparator>true</appendLineSeparator>
</layout>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>5000</queueSize> <!-- Default is 256 -->
<discardingThreshold>0</discardingThreshold> <!-- Instruction to drop events
once the buffer reaches the max capacity. Default value is 20%.
20 means drop lower event when has 20% capacity remaining -->
<neverBlock>true</neverBlock><!-- default false. Setting it to true to
cause the Appender not block the application and just drop the messages -->
<maxFlushTime>5000</maxFlushTime> <!-- The default maximum queue flush time allowed during appender stop. If
the worker takes longer than this time it will exit, discarding any remaining
cartItems in the queue -->
<appender-ref ref="CONSOLE_JSON"/>
</appender>
<logger name="org.springframework.web" level="INFO"/>
<root level="INFO">
<appender-ref ref="ASYNCSTDOUT"/>
</root>
</configuration>
The artifacts I am using in my pom are as below:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>0.1.5</version>
</dependency>
</dependencies>
I am using log4j2 to print log to a file. It work when I use it on my own computer, but when I deploying war on tomcat, It didn't work. And I have wasted a whole day on this.My tomcat version:
Server version: Apache Tomcat/9.0.62
Server built: Mar 31 2022 14:34:15 UTC
Server number: 9.0.62.0
OS Name: Linux
OS Version: 4.15.0-161-generic
Architecture: amd64
JVM Version: 17.0.2+8-Ubuntu-118.04
JVM Vendor: Private Build
My log4j2.xml:
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" strict="true" status="ERROR" name="ConfigTest">
<Properties>
<Property name="FILE_NAME">./src/logs/myLog.log</Property>
</Properties>
<Appenders>
<!-- log to file-->
<Appender type="File" name="File" fileName="${FILE_NAME}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
<!-- log to console-->
<Appender type="Console" name="STDOUT">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
<Filters>
<Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
<Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
</Appender>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
And I use log4j2 like this:
#SpringBootApplication
public class DemoApplication extends ServletInitializer { //
private static final Logger shortMessageLogger = LogManager.getLogger("shortMessageLogger");
public static void main(String[] args) {
shortMessageLogger.error("start running...");
SpringApplication.run(DemoApplication.class, args);
}
}
And my application.properies:
logging.config=classpath:log4j2.xml
And my maven pom.xml like this:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- exclusion embedded loggin -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-appserver</artifactId>
<version>2.17.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>Demo</finalName>
</build>
</project>
I found maybe it's because the tomcat9 has its own logger configuration and covered my config. I also referred to the Apache log4j2 documentation, but failed because it was not specific enough. So where I could get the more detailed process?
Tomcat by default uses java.util.logging as its logging backend, so it does not interfere with your application logging.
The most probable reason for your lack of logs is a permission problem, when creating the file ./src/logs/myLog.log. The error should be logged by Log4j's status logger (on stdout, which usually ends up in catalina.out).
When you run a Tomcat server, you can never assume anything about the working directory of the server, so you should not use relative paths. A more safe location for your log file is ${sys:catalina.base}/logs/myLog.log, since Tomcat can write to that directory.
I am a beginner and have a problem with setting log4j2 in my Spring Boot application. I've tried to do it myself for about a week now, read many hints, posts and tutorials and still didn't find solution. Could anyone pls help me with that? I would like to create different files for different log levels like error.log, warn.log and so on.
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.assessment.ww</groupId>
<artifactId>Assessment_Exercise</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Assessment_Exercise</name>
<description>Spring Boot Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" target="logs/app.log">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<!-- Log everything -->
<Logger name="com.assessment.ww.Assessment_Exercise" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>
<!-- Log everything in Spring Boot -->
<Logger name="org.springframework.boot" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>
<!-- Log everything in Spring Core -->
<Logger name="org.springframework.core" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>
<Root level="all">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
starting class
package com.assessment.ww.Assessment_Exercise;
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 AssessmentExerciseApplication {
private static final Logger logger = LogManager.getLogger(AssessmentExerciseApplication.class);
public static void main(String[] args) {
SpringApplication.run(AssessmentExerciseApplication.class, args);
logger.info("Starting SB app");
}
}
I also have such a warning - maybe it will help:
enter image description here
It prints my logger.info message (but why without formatting?) but do not create any files.
enter image description here
Thank You in advance for any advices.
Finally, I've found a solution. The problem was Logback - even that I disabled it in spring-boot-starter-web dependency in pom.xml, it was still active in spring-boot-starter-data-jpa and spring-boot-starter-actuator.
So now my pom.xml looks like this:
...
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
...
I have a spring boot application with trying to use slf4j by following tutorial from https://www.baeldung.com/spring-boot-logging however logs are getting printed only on console side. There is no log file created in given directory /var/logs.
log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<RollingFile name="RollingFile"
fileName="/var/logs/spring-boot-logger-log4j2.log"
filePattern="/var/logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches
10 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<Logger name="com.buraktas">
<AppenderRef ref="LogFile"/>
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
maven dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
<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>
You can define log path in the application.properties file, check the link below for example:
logging.level.com.example.springscheduler = debug
logging.level.org.springframework.web = WARN
logging.file.name =log/loggingdemocontroller.log
https://springbootmvc.blogspot.com/2020/06/spring-boot-logging-using-orgslf4jlogger.html
I found the problem where I had spring-boot-starter and spring-boot-starter-web dependencies. I excluded spring-boot-starter-logging dependency from only one of them which forced spring to use logback logger. And since logback logger doesn't pick up log4j2.xml for its configuration no files we getting created. So the pom.xml file was something like this;
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
<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>
I needed to exclude spring-boot-starter-logging from spring-boot-starter dependency as well. Well, after all since I have spring-boot-starter-web I just deleted spring-boot-starter package which was needless.
I am trying to do a simple test of Logback Kafka Appender. Following is what I have in my logback.xml which I have placed in src/main/resources:
<?xml version="1.0" encoding="UTF-8"?>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- This is the kafkaAppender -->
<appender name="kafkaAppender"
class="com.github.danielwegener.logback.kafka.KafkaAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
<topic>test</topic>
<keyingStrategy
class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy" />
<deliveryStrategy
class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
<!-- Optional parameter to use a fixed partition -->
<!-- <partition>0</partition> -->
<!-- Optional parameter to include log timestamps into the kafka message -->
<!-- <appendTimestamp>true</appendTimestamp> -->
<!-- each <producerConfig> translates to regular kafka-client config (format:
key=value) -->
<!-- producer configs are documented here: https://kafka.apache.org/documentation.html#newproducerconfigs -->
<!-- bootstrap.servers is the only mandatory producerConfig -->
<producerConfig>bootstrap.servers=localhost:9092</producerConfig>
<!-- this is the fallback appender if kafka is not available. -->
<!-- <appender-ref ref="STDOUT" /> -->
</appender>
<logger name="com.my.package" level="info" additivity="false">
<appender-ref ref="kafkaAppender" />
</logger>
<root level="info">
<appender-ref ref="kafkaAppender" />
</root>
I have started a Kafka server on Windows with a separate Zookeeper. I have created a test topic as well.
Started Zookeeper on Windows with : zkServer.cmd
Started Kafka Server on Windows with : kafka-server-start.bat config\server.properties
Created a topic : kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Started a consumer on Windows with : kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
In my Java class all I have is a logger inside a method.
private static final Logger logger =
LoggerFactory.getLogger(FormController.class);
logger.debug("DEBUG :: Inside do post method");
logger.info("INFO :: Inside do post method");
My imports are:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
My pom.xml is as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.package</groupId>
<artifactId>SampleIGApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleIGApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.danielwegener/logback-kafka-appender -->
<dependency>
<groupId>com.github.danielwegener</groupId>
<artifactId>logback-kafka-appender</artifactId>
<version>0.2.0-RC1</version>
</dependency>
</dependencies>
<build>
<finalName>MyApp</finalName>
</build>
Can someone please tell me as to what am I doing wrong? Even if I comment out STDOUT appender-name, then also logs keep getting written to console, which in my case is Tomcat's catalina.out. I would appreciate any help. Thank you.
The documentation says you need to add logback-classic as dependency:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
logback-kafka-appender