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.
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.
community!
I've been trying to set up a common logback config: one appender to console and another one to a file. The console appender seems to be working ok but the file appender is not. Only an empty log file is created at start up, but that's all. I started setting up a Rolling File Appender, then made it simpler to a basic File Appender and has the same behavior. I use maven to build the application and the application uses spring (not boot). So I'm not sure if I'm missing a configuration...
What logback outputs when starting (with debug set to true):
20:10:26,380 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
20:10:26,505 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
20:10:26,520 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
20:10:26,653 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
20:10:26,663 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
20:10:26,666 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
20:10:26,668 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [./logs/mylog.log]
20:10:26,674 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
20:10:26,675 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
20:10:26,676 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
20:10:26,676 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
20:10:26,678 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator#17550481 - Registering current configuration as safe fallback point
The logback.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration debug="true">
<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} [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>./logs/mylog.log</file>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<!-- My first sad attempt with a file appender -->
<!--<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/mylog.log</file>
<immediateFlush>true</immediateFlush>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>./logs/mylog-%d.%i.zip</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
</encoder>
</appender> -->
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Some related dependencies in my pom.xml file
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
I've been looking at other logback questions here on stackoverflow, and most seem to point out some missing config in the logback.xml file, but it doesn't seem to be the case here.
EDIT:
I tried to modify the console appender, just to try to isolate the problem, 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} DEBUG [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
Modifying the encoder like that should add the DEBUG word in every line, but I don't see such change being applied. So, it seems like the BasicConfiguration is still being used (despite finding the config as shown in log).
EDIT 2: After Spring application context is loaded, the logging config is set back to null.
15:12:24.590 [main] INFO com.my.package.SomeClass [SomeCodeFile.java:83] - Logback used 'null' as the configuration file.
15:12:24.591 [main] INFO com.my.package.SomeClass [SomeCodeFile.java:85] - Root logger level is: INFO
Any ideas would be greatly appreciated!
Thank you!
Ok, I just found that as part of a library that I can't touch, there was a section that loaded a logback context from an specific file. Since it wasn't found, it removed the one set before the spring application context was loaded.
Try to use below reference logback configuration
<appender name="DebugFileAppender"
class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/log.debug</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern> %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
</appender>
Like this you can define your appender to use
<logger name="debugAppender" level="debug" additivity="false">
<appender-ref ref="DebugFileAppender" />
</logger>
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
Hopefully a simple question but My google foo is failing me - I've got a maven project where we're using SLF4J with Log4J 1.2 bindings.
We now want to move to Log4j 2 particularly for the performance improvement - however, I can't for the life of me find the maven dependency for the log4j 2.0 binding. I've found some notes at http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/ but no mention of any dependency info.
I'm also slightly confused by the fact there's apparently two ways to put slf4j on top of log4j2 (binding or adaptor)
What's the correct way to bind slf4j with log4j2 and how do I define the maven dependencies?
Editing to add some code following the 1st answer below, I'm getting exception:
Exception in thread "main" java.lang.NoSuchMethodError:
org/apache/logging/log4j/spi/AbstractLoggerWrapper.(Lorg/apache/logging/log4j/spi/AbstractLogger;Ljava/lang/String;)V
at org.slf4j.impl.SLF4JLogger.(SLF4JLogger.java:48)
POM:
<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>logging.test</groupId>
<artifactId>logtest2</artifactId>
<version>0.0.1</version>
<name>logtest2</name>
<description>logtest2</description>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j.adapters</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0-beta3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta9</version>
</dependency>
</dependencies>
My log4j.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="A1" class="org.apache.log4j.FileAppender">
<param name="File" value="c:/logtest2.0/log.txt" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %X{sessionId} %c MSG: %m%n" />
</layout>
</appender>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %X{sessionId} %c MSG: %m%n" />
</layout>
</appender>
<category name="org.apache.log4j.xml">
<priority value="debug" />
<appender-ref ref="A1" />
</category>
<root>
<priority value="debug" />
<appender-ref ref="STDOUT" />
</Root> </log4j:configuration>
and my test java class:
package loggertest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerTest {
public static final Logger LOGGER = LoggerFactory.getLogger(LoggerTest.class);
public static void main(final String[] p_args) throws InterruptedException {
LOGGER.debug("Logger test");
}
}
"org.apache.logging.log4j:log4j-slf4j-impl:2.0-beta9" -
LOG4J implementation of SLF4J API
"org.apache.logging.log4j:log4j-core:2.0-beta9" - Core LOG4J implementation
This, plus a valid log4j2.xml on the classpath should get you started.
If you like to divert jars that use commons logging, log4j1 and others as well redirected to slf4j and finally use log4j2 implementation for logging.. here is the set that need to be used...
Explanation: First two are excluding the log4j1 and commons-logging not there in classpath. log4j-over-slf4j and jcl-over-slf4j provide proxies that redirect to slf4j api. Remaining are log4j2 implementation dependencies.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.7</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>
<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-layout-template-json</artifactId>
<version>2.17.1</version>
</dependency>