-
Notifications
You must be signed in to change notification settings - Fork 28k
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
[SPARK-48059][CORE] Implement the structured log framework on the java side #46301
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f256ca8
[SPARK-48059][CORE] Implement the structured log framework on the jav…
panbingkun 7216263
fix style
panbingkun 1efbd70
Merge branch 'master' into structured_logger_java
panbingkun 26dea93
fix
panbingkun d2f6aaf
update
panbingkun cbb1f1f
Merge branch 'master' into structured_logger_java
panbingkun b7747ee
Merge branch 'master' into structured_logger_java
panbingkun ee57438
update
panbingkun File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
common/utils/src/main/java/org/apache/spark/internal/Logger.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,184 @@ | ||
/* | ||
* 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.spark.internal; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import org.apache.logging.log4j.CloseableThreadContext; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.ParameterizedMessageFactory; | ||
|
||
public class Logger { | ||
|
||
private static final MessageFactory MESSAGE_FACTORY = ParameterizedMessageFactory.INSTANCE; | ||
private final org.slf4j.Logger slf4jLogger; | ||
|
||
Logger(org.slf4j.Logger slf4jLogger) { | ||
this.slf4jLogger = slf4jLogger; | ||
} | ||
|
||
public void error(String msg) { | ||
slf4jLogger.error(msg); | ||
} | ||
|
||
public void error(String msg, Throwable throwable) { | ||
slf4jLogger.error(msg, throwable); | ||
} | ||
|
||
public void error(String msg, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.error(msg); | ||
} else if (slf4jLogger.isErrorEnabled()) { | ||
withLogContext(msg, mdcs, null, mt -> slf4jLogger.error(mt.message)); | ||
} | ||
} | ||
|
||
public void error(String msg, Throwable throwable, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.error(msg, throwable); | ||
} else if (slf4jLogger.isErrorEnabled()) { | ||
withLogContext(msg, mdcs, throwable, mt -> slf4jLogger.error(mt.message, mt.throwable)); | ||
} | ||
} | ||
|
||
public void warn(String msg) { | ||
slf4jLogger.warn(msg); | ||
} | ||
|
||
public void warn(String msg, Throwable throwable) { | ||
slf4jLogger.warn(msg, throwable); | ||
} | ||
|
||
public void warn(String msg, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.warn(msg); | ||
} else if (slf4jLogger.isWarnEnabled()) { | ||
withLogContext(msg, mdcs, null, mt -> slf4jLogger.warn(mt.message)); | ||
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. Let's check the log level before calling 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. Done |
||
} | ||
} | ||
|
||
public void warn(String msg, Throwable throwable, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.warn(msg); | ||
} else if (slf4jLogger.isWarnEnabled()) { | ||
withLogContext(msg, mdcs, throwable, mt -> slf4jLogger.warn(mt.message, mt.throwable)); | ||
} | ||
} | ||
|
||
public void info(String msg) { | ||
slf4jLogger.info(msg); | ||
} | ||
|
||
public void info(String msg, Throwable throwable) { | ||
slf4jLogger.info(msg, throwable); | ||
} | ||
|
||
public void info(String msg, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.info(msg); | ||
} else if (slf4jLogger.isInfoEnabled()) { | ||
withLogContext(msg, mdcs, null, mt -> slf4jLogger.info(mt.message)); | ||
} | ||
} | ||
|
||
public void info(String msg, Throwable throwable, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.info(msg); | ||
} else if (slf4jLogger.isInfoEnabled()) { | ||
withLogContext(msg, mdcs, throwable, mt -> slf4jLogger.info(mt.message, mt.throwable)); | ||
} | ||
} | ||
|
||
public void debug(String msg) { | ||
slf4jLogger.debug(msg); | ||
} | ||
|
||
public void debug(String msg, Throwable throwable) { | ||
slf4jLogger.debug(msg, throwable); | ||
} | ||
|
||
public void debug(String msg, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.debug(msg); | ||
} else if (slf4jLogger.isDebugEnabled()) { | ||
withLogContext(msg, mdcs, null, mt -> slf4jLogger.debug(mt.message)); | ||
} | ||
} | ||
|
||
public void debug(String msg, Throwable throwable, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.debug(msg); | ||
} else if (slf4jLogger.isDebugEnabled()) { | ||
withLogContext(msg, mdcs, throwable, mt -> slf4jLogger.debug(mt.message, mt.throwable)); | ||
} | ||
} | ||
|
||
public void trace(String msg) { | ||
slf4jLogger.trace(msg); | ||
} | ||
|
||
public void trace(String msg, Throwable throwable) { | ||
slf4jLogger.trace(msg, throwable); | ||
} | ||
|
||
public void trace(String msg, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.trace(msg); | ||
} else if (slf4jLogger.isTraceEnabled()) { | ||
withLogContext(msg, mdcs, null, mt -> slf4jLogger.trace(mt.message)); | ||
} | ||
} | ||
|
||
public void trace(String msg, Throwable throwable, MDC... mdcs) { | ||
if (mdcs == null || mdcs.length == 0) { | ||
slf4jLogger.trace(msg); | ||
} else if (slf4jLogger.isTraceEnabled()) { | ||
withLogContext(msg, mdcs, throwable, mt -> slf4jLogger.trace(mt.message, mt.throwable)); | ||
} | ||
} | ||
|
||
private void withLogContext( | ||
String pattern, | ||
MDC[] mdcs, | ||
Throwable throwable, | ||
Consumer<MessageThrowable> func) { | ||
Map<String, String> context = new HashMap<>(); | ||
Object[] args = new Object[mdcs.length]; | ||
for (int index = 0; index < mdcs.length; index++) { | ||
MDC mdc = mdcs[index]; | ||
String value = (mdc.value() != null) ? mdc.value().toString() : null; | ||
if (Logging$.MODULE$.isStructuredLoggingEnabled()) { | ||
context.put(mdc.key().name(), value); | ||
} | ||
args[index] = value; | ||
} | ||
MessageThrowable messageThrowable = MessageThrowable.of( | ||
MESSAGE_FACTORY.newMessage(pattern, args).getFormattedMessage(), throwable); | ||
try (CloseableThreadContext.Instance ignored = CloseableThreadContext.putAll(context)) { | ||
func.accept(messageThrowable); | ||
} | ||
} | ||
|
||
private record MessageThrowable(String message, Throwable throwable) { | ||
static MessageThrowable of(String message, Throwable throwable) { | ||
return new MessageThrowable(message, throwable); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
common/utils/src/main/java/org/apache/spark/internal/LoggerFactory.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,26 @@ | ||
/* | ||
* 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.spark.internal; | ||
|
||
public class LoggerFactory { | ||
|
||
public static Logger getLogger(Class<?> clazz) { | ||
org.slf4j.Logger slf4jLogger = org.slf4j.LoggerFactory.getLogger(clazz); | ||
return new Logger(slf4jLogger); | ||
} | ||
} |
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
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.
Although the following design is shown in the document,
But I think the parameter
void error(String msg, MDC... mdcs)
is more in line with the style of Java? WDYT @gengliangwangThere 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.
Either way is fine to me