-
Notifications
You must be signed in to change notification settings - Fork 20
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
Implement the logger-log4j
module using log4j apis directly
#756
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<!DOCTYPE suppressions PUBLIC | ||
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" | ||
"https://checkstyle.org/dtds/suppressions_1_2.dtd"> | ||
<!-- IMPORTANT ECLIPSE NOTE: If you change this file, you must restart Eclipse | ||
for your changes to take effect in its Checkstyle integration. --> | ||
<suppressions> | ||
<!-- | ||
Logger facades delegate to logger implementations. Unfortunately we | ||
cannot opt out of specific custom rules, otherwise we would only | ||
opt out of 'BanLoggingImplementations'. | ||
--> | ||
<suppress files="[/\\]logger-.*Bridge\.java" checks="IllegalImport" /> | ||
</suppressions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Implement the `logger-log4j` module using log4j apis directly | ||
links: | ||
- https://github.com/palantir/safe-logging/pull/756 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
org.gradle.parallel=true | ||
# Avoid formatting copyrights, which are formatted in a | ||
# slightly non-standard way due to javapoet codegen. | ||
com.palantir.baseline-format.copyright=false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will opt us out of unintentionally formatting generated classes that we check in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apply plugin: 'com.palantir.external-publish-jar' | ||
|
||
dependencies { | ||
api project(':logger-spi') | ||
implementation 'com.google.errorprone:error_prone_annotations' | ||
compileOnly 'org.apache.logging.log4j:log4j-api' | ||
compileOnly 'com.google.code.findbugs:jsr305' | ||
compileOnly 'com.google.auto.service:auto-service-annotations' | ||
annotationProcessor 'com.google.auto.service:auto-service' | ||
} | ||
|
||
// No great way to use a block comment for copyright statements | ||
tasks.spotlessJavaCheck.enabled = false |
61 changes: 61 additions & 0 deletions
61
...r-log4j/src/main/java/com/palantir/logsafe/logger/log4j/Log4JSafeLoggerFactoryBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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 com.palantir.logsafe.logger.log4j; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.palantir.logsafe.Safe; | ||
import com.palantir.logsafe.logger.spi.SafeLoggerBridge; | ||
import com.palantir.logsafe.logger.spi.SafeLoggerFactoryBridge; | ||
import org.apache.logging.log4j.LogManager; | ||
|
||
@AutoService(SafeLoggerFactoryBridge.class) | ||
public final class Log4JSafeLoggerFactoryBridge implements SafeLoggerFactoryBridge { | ||
|
||
private static final int DEFAULT_PRIORITY = 500; | ||
|
||
private final int priority; | ||
|
||
/** ServiceLoader requires a public no-arg constructor. */ | ||
public Log4JSafeLoggerFactoryBridge() { | ||
// priority is relatively high when log4j-core is available on the classpath but low when it is not. | ||
this.priority = isLog4jCoreAvailable() ? DEFAULT_PRIORITY : -DEFAULT_PRIORITY; | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return priority; | ||
} | ||
|
||
@Override | ||
public SafeLoggerBridge get(@Safe Class<?> clazz) { | ||
return new Log4jSafeLoggerBridge(LogManager.getLogger(clazz)); | ||
} | ||
|
||
@Override | ||
public SafeLoggerBridge get(@Safe String name) { | ||
return new Log4jSafeLoggerBridge(LogManager.getLogger(name)); | ||
} | ||
|
||
private static boolean isLog4jCoreAvailable() { | ||
try { | ||
Class.forName("org.apache.logging.log4j.core.LoggerContext"); | ||
return true; | ||
} catch (Throwable ignored) { | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this suppression given this project is explicitly is dealing with loggers, but one workaround would be to use/generate fully qualified type names where used for the things we consider illegal so they aren't imported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an option, but it would impact readability of the generated code, so I opted against it.