Skip to content
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
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
184 changes: 184 additions & 0 deletions common/utils/src/main/java/org/apache/spark/internal/Logger.java
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) {
Copy link
Contributor Author

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,
image
But I think the parameter void error(String msg, MDC... mdcs) is more in line with the style of Java? WDYT @gengliangwang

Copy link
Member

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

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check the log level before calling withLogContext. For example, if the log level is ERROR, we don't need to enter the method withLogContext

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ case class MDC(key: LogKey, value: Any) {
"the class of value cannot be MessageWithContext")
}

object MDC {
def of(key: LogKey, value: Any): MDC = MDC(key, value)
}

/**
* Wrapper class for log messages that include a logging context.
* This is used as the return type of the string interpolator `LogStringContext`.
Expand Down