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
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 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'm having a problem with getting eclipse to recognize links in logging output. I have a standard maven project with classes in src/main folder and classes in a src/test folder. If I use the logging strategy outlined here:
How do I get Logback to work nicely with Eclipse?
The links work for java files in src/main but do NOT work for files in src/test. How do I get the links for files in src/test to work?
This is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myproject</groupId>
<artifactId>MyProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- JUNIT https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- JUNIT-TOOLBOX https://mvnrepository.com/artifact/com.googlecode.junit-toolbox/junit-toolbox -->
<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
<!-- SLF4J LOGBACK CLASSIC https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- HTTPCLIENT https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- ORG.JSON https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
</project>
This is my logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level (%file:%line\) - %message%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
This is the version of Eclipse I'm using:
Spring Tool Suite
Version: 3.9.4.RELEASE
Build Id: 201804120921
Platform: Eclipse Oxygen.3a (4.7.3a)
Copyright (c) 2007 - 2018 Pivotal Software, Inc.
All rights reserved. Visit http://spring.io/tools/sts
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>