From dedabdde2e7e4fb6c19493db6760388bb1504d46 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 30 Aug 2023 13:28:24 +0200 Subject: [PATCH] Add Slf4JLogMessageHandler --- example/pom.xml | 2 +- .../main/java/it/tdlight/example/Example.java | 6 +++- .../src/main/java/it/tdlight/Init.java | 24 ++------------ .../src/main/java/it/tdlight/Log.java | 20 +++++++++-- .../it/tdlight/Slf4JLogMessageHandler.java | 33 +++++++++++++++++++ 5 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 tdlight-java/src/main/java/it/tdlight/Slf4JLogMessageHandler.java diff --git a/example/pom.xml b/example/pom.xml index ed99df9..2817aae 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -26,7 +26,7 @@ it.tdlight tdlight-java-bom - 3.1.2+td.1.8.16 + 3.1.3+td.1.8.16 pom import diff --git a/example/src/main/java/it/tdlight/example/Example.java b/example/src/main/java/it/tdlight/example/Example.java index 43576dc..57d224e 100644 --- a/example/src/main/java/it/tdlight/example/Example.java +++ b/example/src/main/java/it/tdlight/example/Example.java @@ -1,11 +1,13 @@ package it.tdlight.example; +import it.tdlight.Log; import it.tdlight.client.*; import it.tdlight.client.AuthenticationSupplier; import it.tdlight.client.CommandHandler; import it.tdlight.client.SimpleTelegramClient; import it.tdlight.client.TDLibSettings; import it.tdlight.Init; +import it.tdlight.Slf4JLogMessageHandler; import it.tdlight.jni.TdApi.AuthorizationState; import it.tdlight.jni.TdApi.Chat; import it.tdlight.jni.TdApi.FormattedText; @@ -14,7 +16,6 @@ import it.tdlight.jni.TdApi.MessageContent; import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi.SendMessage; import it.tdlight.jni.TdApi.TextEntity; -import it.tdlight.util.UnsupportedNativeLibraryException; import java.nio.file.Path; import java.nio.file.Paths; @@ -36,6 +37,9 @@ public final class Example { // Initialize TDLight native libraries Init.init(); + // Set the log level + Log.setLogMessageHandler(1, new Slf4JLogMessageHandler()); + // Create the client factory try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) { // Obtain the API token diff --git a/tdlight-java/src/main/java/it/tdlight/Init.java b/tdlight-java/src/main/java/it/tdlight/Init.java index a63c77a..bd095e0 100644 --- a/tdlight-java/src/main/java/it/tdlight/Init.java +++ b/tdlight-java/src/main/java/it/tdlight/Init.java @@ -54,27 +54,8 @@ public final class Init { ConstructorDetector.init(); try { NativeClientAccess.execute(new SetLogVerbosityLevel(3)); - Log.setLogMessageHandler(3, (verbosityLevel, message) -> { - switch (verbosityLevel) { - case -1: - case 0: - case 1: - LOG.error(message); - break; - case 2: - LOG.warn(message); - break; - case 3: - LOG.info(message); - break; - case 4: - LOG.debug(message); - break; - default: - LOG.trace(message); - break; - } - }); + Log.setLogMessageHandler(3, new Slf4JLogMessageHandler()); + Log.setLogStream(null); NativeClientAccess.execute(new SetLogStream(new LogStreamEmpty())); } catch (Throwable ex) { LOG.error("Can't set verbosity level on startup", ex); @@ -82,4 +63,5 @@ public final class Init { } } } + } diff --git a/tdlight-java/src/main/java/it/tdlight/Log.java b/tdlight-java/src/main/java/it/tdlight/Log.java index f0e8e81..89050b6 100644 --- a/tdlight-java/src/main/java/it/tdlight/Log.java +++ b/tdlight-java/src/main/java/it/tdlight/Log.java @@ -1,15 +1,18 @@ package it.tdlight; import it.tdlight.jni.TdApi; +import it.tdlight.jni.TdApi.LogStream; import it.tdlight.jni.TdApi.LogStreamDefault; +import it.tdlight.jni.TdApi.LogStreamEmpty; import it.tdlight.jni.TdApi.LogStreamFile; +import it.tdlight.jni.TdApi.SetLogStream; import it.tdlight.jni.TdApi.SetLogVerbosityLevel; import it.tdlight.tdnative.NativeClient.LogMessageHandler; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; /** - * Class used for managing internal TDLib logging. Use TdApi.*Log* methods instead. + * Class used for managing internal TDLib logging. */ public final class Log { @@ -86,6 +89,11 @@ public final class Log { updateLog(); } + public static void disable() { + setLogMessageHandler(0, null); + setLogStream(null); + } + /** * * Sets the log message handler @@ -96,6 +104,14 @@ public final class Log { * @param logMessageHandler handler */ public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) { - NativeClientAccess.setLogMessageHandler(maxVerbosityLevel, logMessageHandler); + NativeClientAccess.setLogMessageHandler(logMessageHandler != null ? maxVerbosityLevel : Math.min(maxVerbosityLevel, 1), + logMessageHandler != null ? logMessageHandler : new Slf4JLogMessageHandler()); + } + + /** + * Sets the log stream + */ + public static void setLogStream(LogStream logStream) { + NativeClientAccess.execute(new SetLogStream(logStream != null ? logStream : new LogStreamEmpty())); } } diff --git a/tdlight-java/src/main/java/it/tdlight/Slf4JLogMessageHandler.java b/tdlight-java/src/main/java/it/tdlight/Slf4JLogMessageHandler.java new file mode 100644 index 0000000..78f6468 --- /dev/null +++ b/tdlight-java/src/main/java/it/tdlight/Slf4JLogMessageHandler.java @@ -0,0 +1,33 @@ +package it.tdlight; + +import it.tdlight.tdnative.NativeClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Slf4JLogMessageHandler implements NativeClient.LogMessageHandler { + + public static final Logger LOG = LoggerFactory.getLogger("it.tdlight.TDLight"); + + @Override + public void onLogMessage(int verbosityLevel, String message) { + switch (verbosityLevel) { + case -1: + case 0: + case 1: + LOG.error(message); + break; + case 2: + LOG.warn(message); + break; + case 3: + LOG.info(message); + break; + case 4: + LOG.debug(message); + break; + default: + LOG.trace(message); + break; + } + } +}