-
Notifications
You must be signed in to change notification settings - Fork 82
Add SLF4J MDC support for JUL #79
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c3be2e
Add SLF4J MDC support for JUL
NickWemekamp bf4b196
Remove unneeded constructor
NickWemekamp 88e837e
Use Collections.emptyMap() instead of new HashMap()
NickWemekamp 84fd627
Refer to Available.INSTANCE with Class.forname so as to prevent loadi…
NickWemekamp 60d8c9e
Slf4j jdk 14 dependency is only needed for unittests.
NickWemekamp a6fb690
No need to mark test dependency as optional
felixbarny 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 hidden or 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 hidden or 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
79 changes: 79 additions & 0 deletions
79
jul-ecs-formatter/src/main/java/co/elastic/logging/jul/MdcSupplier.java
This file contains hidden or 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,79 @@ | ||
/*- | ||
* #%L | ||
* Java ECS logging | ||
* %% | ||
* Copyright (C) 2019 - 2020 Elastic and contributors | ||
* %% | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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. | ||
* #L% | ||
*/ | ||
package co.elastic.logging.jul; | ||
|
||
import org.slf4j.MDC; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public interface MdcSupplier { | ||
Map<String, String> getMDC(); | ||
|
||
enum Resolver { | ||
INSTANCE; | ||
|
||
MdcSupplier resolve() { | ||
try { | ||
Class.forName("org.slf4j.MDC"); | ||
|
||
// The SLF4J API dependency does not contain an MDC binder by itself, the MDC binders come from an SLF4j | ||
// implementation. When no MDC bindings are available calls to MDC.put will be ignored by slf4j. | ||
// That is why we want to ensure that the StaticMDCBinder exists | ||
Class.forName("org.slf4j.impl.StaticMDCBinder"); | ||
return (MdcSupplier) Class.forName("co.elastic.logging.jul.MdcSupplier$Available").getEnumConstants()[0]; | ||
} catch (Exception e) { | ||
return Unavailable.INSTANCE; | ||
} catch (LinkageError e ) { | ||
return Unavailable.INSTANCE; | ||
} | ||
} | ||
} | ||
|
||
enum Available implements MdcSupplier { | ||
INSTANCE; | ||
|
||
@Override | ||
public Map<String, String> getMDC() { | ||
Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap(); | ||
if (copyOfContextMap != null ) { | ||
return copyOfContextMap; | ||
} | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
|
||
enum Unavailable implements MdcSupplier { | ||
INSTANCE; | ||
|
||
Unavailable() { | ||
} | ||
|
||
@Override | ||
public Map<String, String> getMDC() { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.