Skip to content

Add SLF4J to Log4j API bridge #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 70 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
121da92
Make new home for Log4j2
rgoers Feb 6, 2012
c40faf8
Rename `log4j-slf4j20-impl` to `log4j-slf4j2-impl`
ppkarwasz Sep 4, 2022
8fce30b
[maven-release-plugin] prepare release log4j-2.19.0-rc1
rgoers Sep 9, 2022
09fe7e0
[maven-release-plugin] prepare for next development iteration
rgoers Sep 9, 2022
fd186df
[maven-release-plugin] prepare release log4j-2.19.0-rc1
rgoers Sep 9, 2022
b25d512
[maven-release-plugin] prepare for next development iteration
rgoers Sep 9, 2022
f7bc40e
Fix log4j-slf4j*-impl documentation
ppkarwasz Sep 10, 2022
346ff82
Add missing test scopes
ppkarwasz Sep 12, 2022
ac9a6ee
[maven-release-plugin] prepare release log4j-2.19.0-rc2
rgoers Sep 13, 2022
a9da5dc
[maven-release-plugin] prepare for next development iteration
rgoers Sep 13, 2022
5121672
Fix new snapshot version
ppkarwasz Sep 16, 2022
6bca6f3
Sort dependencies in POMs
ppkarwasz Sep 18, 2022
5319a5b
Move tests to `log4j-api-test`
ppkarwasz Sep 23, 2022
b29e236
Add `test` packages as in `master`
ppkarwasz Sep 24, 2022
07ae1ad
Move additional `log4j-core-test` classes to `main`
ppkarwasz Sep 25, 2022
9ff3310
Move exported test classes under `org.apache.logging.log4j.core.test`
ppkarwasz Sep 25, 2022
10834cc
Delegates plugin versions to ASF parent
ppkarwasz Sep 18, 2022
12e51e6
Centralize plugin versions in main POM
ppkarwasz Oct 2, 2022
3ff7007
Use BOMs in `<dependencyManagement>`
ppkarwasz Sep 25, 2022
e7d6b4a
Remove no-op plugin execution
ppkarwasz Oct 18, 2022
c93462b
Fix NPE in Log4jMarker
ppkarwasz Dec 25, 2022
90a72b1
[LOG4J2-3598] Lazily evaluate the level in `Log4jEventBuilder`
ppkarwasz Sep 14, 2022
a21da98
LOG4J2-3628 Replace `maven-changes-plugin` with `log4j-changelog` (#1…
vy Jan 13, 2023
80cb238
Update exception message to refer to log4j-slf4j2-impl
afs Sep 20, 2022
b802fdc
This spotless config is fucking annoying
jvz Jan 14, 2023
93c9c89
Simplify site generation (#1166)
vy Jan 16, 2023
e271929
[LOG4J2-3601] log4j-slf4j2-impl: depend on log4j-core scope=runtime
afs Sep 18, 2022
d9c4d73
Fix Javadoc generation and links for the site
vy Feb 12, 2023
d5a625b
Remove Maven `<reporting>` blocks, since reporting is disabled
vy Feb 14, 2023
b637d47
[maven-release-plugin] prepare release log4j-2.20.1-rc1
rgoers Feb 18, 2023
38b24ed
[maven-release-plugin] prepare for next development iteration
rgoers Feb 18, 2023
48f3b99
[maven-release-plugin] prepare release log4j-2.20.1-rc1
rgoers Feb 18, 2023
8e5465b
Revert version update
rgoers Feb 18, 2023
eaf8210
[maven-release-plugin] prepare release log4j-2.20.0-rc1
rgoers Feb 18, 2023
ef4db9a
[maven-release-plugin] prepare for next development iteration
rgoers Feb 18, 2023
ce0a25f
[maven-release-plugin] rollback the release of log4j-2.20.0-rc1
rgoers Feb 18, 2023
4f7cd9c
[maven-release-plugin] prepare release log4j-2.20.0-rc1
rgoers Feb 18, 2023
4e70764
[maven-release-plugin] prepare for next development iteration
rgoers Feb 18, 2023
f3d5e9e
Sort POMs
ppkarwasz Mar 4, 2023
a804a5e
Apply Spotless to all files
Apr 25, 2023
5b45112
Use `final` whenever possible
Jun 4, 2023
5c9fff3
#1367 Added missing activation policy
N1k145 Apr 13, 2023
fc9d5b5
Allow to override fqcn in Log4jEventBuilder
oshai Jul 3, 2023
dd627f7
Bump version to 2.21.0-SNAPSHOT
ppkarwasz Sep 20, 2023
87cc754
Remove references to `maven-bundle-plugin`
ppkarwasz Sep 25, 2023
d54fbb8
Replace explicit versions with property
ppkarwasz Sep 26, 2023
8fef799
Add BND configuration to bridges
ppkarwasz Sep 29, 2023
409c566
Restored `Log4jMarker` visibility in SLF4J adapters (#1414)
vy Oct 5, 2023
364c908
Migrate changelog, site, and docs to the new infra
vy Oct 5, 2023
af18391
Swap `/pom.xml` and `/log4j-bom/pom.xml`
vy Oct 5, 2023
beaf3b8
Remove `log4j-core` from some processor paths
ppkarwasz Oct 6, 2023
12ea7e6
Add `log4j-core` to some modules processor path
ppkarwasz Oct 6, 2023
423a886
Add OSGi package versioning and fix API breaking changes
ppkarwasz Oct 8, 2023
453a82a
Bump SLF4j to version 2.0.9
ppkarwasz Oct 30, 2023
303051b
Remove unused Maven properties
vy Nov 1, 2023
acee6cc
Apply Palantir formatter
vy Nov 16, 2023
503fbf5
Removes support for `EventData`
ppkarwasz Dec 8, 2023
80b867c
Remove `slf4j-ext` dependency and tests
ppkarwasz Dec 12, 2023
9f90e4a
Remove deprecated `packages` attribute from test configs
ppkarwasz Jan 3, 2024
d0cdad1
Copy `log4j2-config.xml` to `log4j-slf4j2-impl`
ppkarwasz Sep 3, 2022
e546b9d
Expand wildcard imports
ppkarwasz Jan 15, 2024
8005a78
Fix `MarkerTest` assertions
ppkarwasz Jan 15, 2024
b0564a7
Simplify configuration after BND 7.x upgrade
ppkarwasz Dec 21, 2023
5372a17
Add Logback's throwable-consuming semantics as an option
ppkarwasz Mar 14, 2024
80753f5
Move SLF4J dependencies to SLF4J-related modules
ppkarwasz Mar 30, 2024
871cf66
Improve name/description of SLF4J-related modules (#1462)
vy Apr 25, 2024
099f401
Bump org.slf4j:slf4j-api from 2.0.9 to 2.0.16
dependabot[bot] Aug 12, 2024
71b8110
Migrate `log4j-slf4j2-impl` to JUnit 5 (#3080)
AlbaHerrerias Oct 23, 2024
09ae74b
Rename `log4j-slf4j2-impl`
ppkarwasz Nov 6, 2024
849e4b8
Fix build
ppkarwasz Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Apache Logging JDK Bridges
Apache Logging SLF4J Bridges
Copyright 2024 The Apache Software Foundation

This product includes software developed at
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

<!-- Modules here must have a corresponding entry in `dependencyManagement > dependencies` block below! -->
<module>log4j-api-to-slf4j</module>
<module>slf4j-to-log4j-api</module>

</modules>

Expand Down Expand Up @@ -106,6 +107,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>slf4j-to-log4j-api</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
143 changes: 143 additions & 0 deletions slf4j-to-log4j-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-parent</artifactId>
<version>${revision}</version>
<relativePath>../parent</relativePath>
</parent>

<artifactId>slf4j-to-log4j-api</artifactId>

<name>SLF4J to Log4j API logging bridge</name>

<description>SLF4J 2 provider (binding) for the Apache Log4j API.
It forwards SLF4J 2 calls to the Log4j API.
This effectively allows using Log4j as an implementation of SLF4J 2.
(Refer to the `log4j-to-slf4j` artifact for forwarding the Log4j API to SLF4J.)</description>

<properties>
<!-- Dependency versions -->
<osgi.framework.version>1.8.0</osgi.framework.version>
</properties>

<dependencies>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.framework</artifactId>
<version>${osgi.framework.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<!-- Separate test execution to verify that the presence of both:
~ * `log4j-slf4j2-impl` (bridge from SLF4J to Log4j API)
~ * `log4j-to-slf4j` (bridge from Log4j API to SLF4J)
~ does not cause a stack overflow.
-->
<execution>
<id>loop-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<additionalClasspathDependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</additionalClasspathDependencies>
<includes>
<include>**/OverflowTest.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>default-test</id>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/OverflowTest.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.apache.logging.log4j.BridgeAware;
import org.apache.logging.log4j.CloseableThreadContext;
import org.apache.logging.log4j.CloseableThreadContext.Instance;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogBuilder;
import org.apache.logging.log4j.Logger;
import org.slf4j.Marker;
import org.slf4j.spi.CallerBoundaryAware;
import org.slf4j.spi.LoggingEventBuilder;

public class Log4jEventBuilder implements LoggingEventBuilder, CallerBoundaryAware {

private static final String FQCN = Log4jEventBuilder.class.getName();

private final Log4jMarkerFactory markerFactory;
private final Logger logger;
private final List<Object> arguments = new ArrayList<>();
private String message = null;
private org.apache.logging.log4j.Marker marker = null;
private Throwable throwable = null;
private Map<String, String> keyValuePairs = null;
private final Level level;
private String fqcn = FQCN;

public Log4jEventBuilder(final Log4jMarkerFactory markerFactory, final Logger logger, final Level level) {
this.markerFactory = markerFactory;
this.logger = logger;
this.level = level;
}

@Override
public LoggingEventBuilder setCause(final Throwable cause) {
this.throwable = cause;
return this;
}

@Override
public LoggingEventBuilder addMarker(final Marker marker) {
this.marker = markerFactory.getLog4jMarker(marker);
return this;
}

@Override
public LoggingEventBuilder addArgument(final Object p) {
arguments.add(p);
return this;
}

@Override
public LoggingEventBuilder addArgument(final Supplier<?> objectSupplier) {
arguments.add(objectSupplier.get());
return this;
}

@Override
public LoggingEventBuilder addKeyValue(final String key, final Object value) {
if (keyValuePairs == null) {
keyValuePairs = new HashMap<>();
}
keyValuePairs.put(key, String.valueOf(value));
return this;
}

@Override
public LoggingEventBuilder addKeyValue(final String key, final Supplier<Object> valueSupplier) {
if (keyValuePairs == null) {
keyValuePairs = new HashMap<>();
}
keyValuePairs.put(key, String.valueOf(valueSupplier.get()));
return this;
}

@Override
public LoggingEventBuilder setMessage(final String message) {
this.message = message;
return this;
}

@Override
public LoggingEventBuilder setMessage(final Supplier<String> messageSupplier) {
this.message = messageSupplier.get();
return this;
}

@Override
public void log() {
final LogBuilder logBuilder = logger.atLevel(level).withMarker(marker).withThrowable(throwable);
if (logBuilder instanceof BridgeAware) {
((BridgeAware) logBuilder).setEntryPoint(fqcn);
}
if (keyValuePairs == null || keyValuePairs.isEmpty()) {
logBuilder.log(message, arguments.toArray());
} else {
try (final Instance c = CloseableThreadContext.putAll(keyValuePairs)) {
logBuilder.log(message, arguments.toArray());
}
}
}

@Override
public void log(final String message) {
setMessage(message);
log();
}

@Override
public void log(final String message, final Object arg) {
setMessage(message);
addArgument(arg);
log();
}

@Override
public void log(final String message, final Object arg0, final Object arg1) {
setMessage(message);
addArgument(arg0);
addArgument(arg1);
log();
}

@Override
public void log(final String message, final Object... args) {
setMessage(message);
for (final Object arg : args) {
addArgument(arg);
}
log();
}

@Override
public void log(final Supplier<String> messageSupplier) {
setMessage(messageSupplier);
log();
}

@Override
public void setCallerBoundary(String fqcn) {
this.fqcn = fqcn;
}
}
Loading
Loading