diff --git a/CHANGELOG.md b/CHANGELOG.md index 380d7832..14b4556d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## placeholder +* Update sdk loggin logics so all level logs are recored into kusto. ([#170](https://github.com/microsoft/durabletask-java/pull/170)) + ## v1.4.0 ### Updates diff --git a/client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java b/client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java index 92e2bda4..576193cf 100644 --- a/client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java +++ b/client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java @@ -9,6 +9,7 @@ import com.microsoft.durabletask.implementation.protobuf.OrchestratorService.WorkItem.RequestCase; import com.microsoft.durabletask.implementation.protobuf.TaskHubSidecarServiceGrpc.*; +import com.microsoft.durabletask.log.LoggerManager; import io.grpc.*; import java.time.Duration; @@ -22,7 +23,7 @@ */ public final class DurableTaskGrpcWorker implements AutoCloseable { private static final int DEFAULT_PORT = 4001; - private static final Logger logger = Logger.getLogger(DurableTaskGrpcWorker.class.getPackage().getName()); + private static final Logger logger = LoggerManager.getLogger(); private static final Duration DEFAULT_MAXIMUM_TIMER_INTERVAL = Duration.ofDays(3); private final HashMap orchestrationFactories = new HashMap<>(); diff --git a/client/src/main/java/com/microsoft/durabletask/OrchestrationRunner.java b/client/src/main/java/com/microsoft/durabletask/OrchestrationRunner.java index 13904901..19bc70af 100644 --- a/client/src/main/java/com/microsoft/durabletask/OrchestrationRunner.java +++ b/client/src/main/java/com/microsoft/durabletask/OrchestrationRunner.java @@ -5,6 +5,7 @@ import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.StringValue; import com.microsoft.durabletask.implementation.protobuf.OrchestratorService; +import com.microsoft.durabletask.log.LoggerManager; import java.time.Duration; import java.util.Base64; @@ -18,7 +19,7 @@ * caller must provide orchestration state as serialized protobuf bytes. */ public final class OrchestrationRunner { - private static final Logger logger = Logger.getLogger(OrchestrationRunner.class.getPackage().getName()); + private static final Logger logger = LoggerManager.getLogger(); private static final Duration DEFAULT_MAXIMUM_TIMER_INTERVAL = Duration.ofDays(3); private OrchestrationRunner() { diff --git a/client/src/main/java/com/microsoft/durabletask/log/FunctionKustoHandler.java b/client/src/main/java/com/microsoft/durabletask/log/FunctionKustoHandler.java new file mode 100644 index 00000000..0acf9e2a --- /dev/null +++ b/client/src/main/java/com/microsoft/durabletask/log/FunctionKustoHandler.java @@ -0,0 +1,37 @@ +package com.microsoft.durabletask.log; + +import java.io.PrintStream; +import java.util.Arrays; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +class FunctionKustoHandler extends Handler { + private static final String FUNCTIONSKUSTOPREFIX = "LanguageWorkerConsoleLog"; + private static final String DURABLEPREFIX = "DURABLE_JAVA_SDK"; + @Override + public void publish(LogRecord record) { + if (record != null && record.getLevel() != null) { + PrintStream output = record.getLevel().intValue() <= Level.INFO.intValue() ? System.out : System.err; + output.printf("%s%s [%s] {%s.%s}: %s%n", + FUNCTIONSKUSTOPREFIX, + DURABLEPREFIX, + record.getLevel(), + record.getSourceClassName(), + record.getSourceMethodName(), + record.getMessage()); + if (record.getThrown() != null) { + output.printf("%s%s%s%n", FUNCTIONSKUSTOPREFIX, DURABLEPREFIX, Arrays.toString(record.getThrown().getStackTrace())); + } + } + } + + @Override + public void flush() { + System.out.flush(); + System.err.flush(); + } + + @Override + public void close() throws SecurityException { } +} diff --git a/client/src/main/java/com/microsoft/durabletask/log/LoggerManager.java b/client/src/main/java/com/microsoft/durabletask/log/LoggerManager.java new file mode 100644 index 00000000..1a6be749 --- /dev/null +++ b/client/src/main/java/com/microsoft/durabletask/log/LoggerManager.java @@ -0,0 +1,34 @@ +package com.microsoft.durabletask.log; + +import java.util.logging.*; + +public class LoggerManager { + private static final Logger logger = initLogger(); + + private static Logger initLogger() { + Logger logger = Logger.getAnonymousLogger(); + logger.setUseParentHandlers(false); + logger.setLevel(Level.ALL); + logger.addHandler(new FunctionKustoHandler()); + return logger; + } + + public static void setHandler(Handler handler) { + clearHandler(); + addHandler(handler); + } + + public static void addHandler(Handler handler) { + logger.addHandler(handler); + } + + private static void clearHandler() { + for (Handler handler : logger.getHandlers()) { + logger.removeHandler(handler); + } + } + + public static Logger getLogger() { + return logger; + } +}