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.
Related
I had an application working with log4j1. The configuration I have in the log4j.properties is working fine and it creates the file for log.
I migrate to log4j2 and now the file is not created, even though I follow the documentation. I try all the different solutions here but I can't make it work.
This is my log4j2.xml inside resources in my application
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="tmp/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
This is the dependency in my pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
I tired changing the path of my file with the full path, changing the folder in case it was permissions problem.
I don't have any error, but when the application runs I don't see any file.
I even print 2 versions of log4j; log4j and log4j2. Log4j creates the file no problem, log4j2 don't
this is my file for log4j
log4j.properties
log4j.rootLogger=INFO, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.RollingFileAppender
log4j.appender.Appender1.File=/tmp/info.log
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} - logger1 - %-5p %c %m%n
log4j.appender.Appender1.Threshold=INFO
log4j.appender.Appender1.maxFileSize=10000KB
log4j.appender.Appender1.MaxBackupIndex=10
log4j.appender.Appender2=org.apache.log4j.RollingFileAppender
log4j.appender.Appender2.File=/tmp/errors.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%d{dd-mm-YYYY HH:mm:ss.SSS} %-5p %c %m%n
log4j.appender.Appender2.Threshold=ERROR
log4j.appender.Appender2.maxFileSize=10000KB
log4j.appender.Appender2.MaxBackupIndex=10
I'm new to this, can anybody help me with this?
what am I'm missing?
let me know if you need more information.
edit: adding the whole pom file in case something is wrong with it:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.6
com.software
seguros
0.0.1-SNAPSHOT
seguros
Administracion de seguros
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-data-jpa
<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>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>8.0.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/eu.hansolo.enzo/Enzo -->
<dependency>
<groupId>eu.hansolo.enzo</groupId>
<artifactId>Enzo</artifactId>
<version>0.3.6</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>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
You need to remove the 1.2.17 jar and add the log4j-api and log4j-core jars. It will solve the problem.
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>
I'm working on a complex application that uses an Ignite version of log4j2. It works perfectly fine, but when I try to add a Pulsar appender it throws an error:
log4j:WARN No appenders could be found for logger (org.apache.pulsar.shade.io.netty.util.internal.logging.InternalLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I can't really figure out why it's failing to find the log4j2.xml file because without the Pulsar appender it works fine, so it must be on the classpath. I'm guessing it must be a conflict with the ignite-log4j2 dependency and pulsar-log4j2-appender dependency.
This is what my pom.xml looks like:
<dependencies>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-zookeeper</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-urideploy</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j2</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>${pulsar.version}</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- Pulsar logging -->
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-log4j2-appender</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
And my log4j2.xml:
<Configuration monitorInterval="60">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
</Console>
<Console name="CONSOLE_ERR" target="SYSTEM_ERR">
<PatternLayout pattern="[%d{ISO8601}][%-5p][%t][%c{1}]%notEmpty{[%markerSimpleName]} %m%n"/>
</Console>
<Routing name="FILE">
<Routes pattern="$${sys:nodeId}">
<Route>
<RollingFile name="Rolling-${sys:nodeId}" fileName="ignite/work/log/ignite-${sys:nodeId}.log"
filePattern="ignite/work/log/ignite-${sys:nodeId}-%i-%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="[%d{ISO8601}][%-5p][%t][%c{1}]%notEmpty{[%markerSimpleName]} %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
<Pulsar name="PULSAR" serviceUrl="pulsar://172.21.11.82:6650" topic="json_persistor" avoidRecursive="false">
<PatternLayout pattern="%msg%n"/>
</Pulsar>
</Appenders>
<Loggers>
<Logger name="org.apache.pulsar" level="INFO"/> <!-- to avoid recursive logging -->
<Logger name="com.coinflex.common.persistor.JsonObjectPersistor" level="INFO" additivity="false">
<AppenderRef ref="PULSAR"/>
</Logger>
<Logger name="org.apache.ignite" level="ERROR"/>
<Logger name="org.springframework" level="WARN"/>
<Logger name="org.eclipse.jetty" level="WARN"/>
<Logger name="org.eclipse.jetty.util.log" level="ERROR"/>
<Logger name="org.eclipse.jetty.util.component" level="ERROR"/>
<Logger name="com.amazonaws" level="WARN"/>
<Root level="INFO">
<AppenderRef ref="CONSOLE" level="INFO"/>
<AppenderRef ref="CONSOLE_ERR" level="ERROR"/>
<AppenderRef ref="FILE" level="DEBUG"/>
</Root>
</Loggers>
</Configuration>
The error message you are getting is coming from Log4J 1. This indicates Log4J 2 isn’t being used, which would explain why it can’t find the pulsar appender.
If you want to use Log4J 2 you need to re-examine your dependencies.
This also indicates it isn’t using your logging configuration.
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 am working on a Spring-MVC application in which I am trying to get logging working again. Unfortunately, sometime back it just stopped working, I don't know what is causing that. I tried some suggestions on net, but nothing useful. Any suggestions?
Pom.xml :
<packaging>war</packaging>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.1.6.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate.version>4.3.9.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<springsecurity.version>4.0.1.RELEASE</springsecurity.version>
<spring-platform.version>1.1.3.RELEASE</spring-platform.version>
<jetty.version>9.2.9.v20150224</jetty.version>
</properties>
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.3.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- Spring framework dependencies -->
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!-- Spring security dependencies -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.1</version>
</dependency>
I have both log4j.xml and logback.xml :
log4j.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="org.cometd">
<level value="debug"/>
</logger>
<!-- Root Logger -->
<root>
<priority value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
logback.xml :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.cometd" level="debug"/>
<logger name="com.tooltank.spring.chat.ChatServiceImpl" level="info"/>
</configuration>
During server startup I get this :
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home//WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:43:29,174 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/path/to/logback.xml]
13:43:29,292 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:43:29,298 |-ERROR in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [org/springframework/boot/logging/logback/base.xml]
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.cometd] to DEBUG
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.tooltank.spring.chat.ChatServiceImpl] to INFO
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:43:29,300 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#59474a9e - Registering current configuration as safe fallback point
What am I missing? Thank you.
You should avoid using log4j and logback in one application. If you have both jars in your classpath the classloader will pick either one of them (kind of random..)
That is indicated by this log statement:
SLF4J: Class path contains multiple SLF4J bindings.
If you intend to use logback you need to place the logback.xml file in the classpath of your application. From documentation:
logback will try to configure itself using the files logback-test.xml or logback.xml if found on the class path.
You can also check out this link: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-logback-for-logging
Edit:
I guess you also need to add an appender to the console such as:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
and then add the appender to your logger:
<logger name="org.cometd" additivity="false">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
</logger>
Best regards!
I had the same issue.
I had mixed both <groupId>org.slf4j</groupId> & <groupId>ch.qos.logback</groupId> dependencies together.
After I remove <groupId>org.slf4j</groupId> and keep only <groupId>ch.qos.logback</groupId> logs are started to written.