Github user revans2 commented on a diff in the pull request:
https://github.com/apache/storm/pull/2475#discussion_r159742835
--- Diff: storm-client/src/jvm/org/apache/storm/utils/ShellLogHandler.java
---
@@ -0,0 +1,113 @@
+/**
+ * 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.storm.utils;
+
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.storm.multilang.ShellMsg;
+import org.apache.storm.task.TopologyContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class ShellLogHandler {
+ public static final Logger LOG =
LoggerFactory.getLogger(ShellLogHandler.class);
+
+ public static final String ID = "id";
+ public static final String NAME = "name";
+ public static final String PARENT = "parent";
+ public static final String PID = "pid";
+ public static final String TASK = "task";
+
+ private ShellLogHandler() {
+ }
+
+ private static void putIfNotNull(String key, Object value) {
+ if (value != null) {
+ ThreadContext.put(key, value.toString());
+ }
+ }
+
+ /**
+ * Update the {@link ThreadContext} with information about the logged
+ * message, including the pid and task.
+ *
+ * @param shellMsg
+ * - the {@link ShellMsg} containing the ID.
+ * @param process
+ * - the current {@link ShellProcess}.
+ * @param context
+ * - the current {@link TopologyContext}.
+ */
+ private static void updateContext(ShellMsg shellMsg, ShellProcess
process, TopologyContext context) {
+ putIfNotNull(ID, shellMsg.getId());
+ // Calling this only once allows the same parent ID to be attached
to
+ // all log messages from a tuple tree
+ if (!ThreadContext.containsKey(PARENT)) {
+ putIfNotNull(PARENT, shellMsg.getId());
+ }
+ if (process != null) {
+ putIfNotNull(NAME, process.getComponentName());
+ putIfNotNull(PID, process.getPid());
+ }
+ if (context != null) {
+ ThreadContext.put(TASK,
Integer.toString(context.getThisTaskId()));
+ }
+ }
+
+ /**
+ * Log the given message and update the {@link ThreadContext} with some
+ * contextual information, including the pid and task.
+ *
+ * @param shellMsg
+ * - the {@link ShellMsg} containing the ID. Required.
+ * @param process
+ * - the current {@link ShellProcess}. Optional.
+ * @param context
+ * - the current {@link TopologyContext}. Optional.
+ */
+ public static void handleLog(ShellMsg shellMsg, ShellProcess process,
TopologyContext context) {
+ if (shellMsg == null) {
+ throw new IllegalArgumentException("shellMsg is required");
+ }
+
+ updateContext(shellMsg, process, context);
--- End diff --
Could we try and optimize the overhead on this? Users should not be
logging much if anything, but it might be nice to check if the current log
level is enabled before we try to update the context, as I don't know how
expensive it is.
We can probably make that happen by making some changes to the
ShellMsg.ShellLogLevel enum to know more about log4j
---