Add Slf4JLogMessageHandler

This commit is contained in:
Andrea Cavalli 2023-08-30 13:28:24 +02:00
parent c3596f9ae4
commit dedabdde2e
5 changed files with 60 additions and 25 deletions

View File

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId> <artifactId>tdlight-java-bom</artifactId>
<version>3.1.2+td.1.8.16</version> <version>3.1.3+td.1.8.16</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

@ -1,11 +1,13 @@
package it.tdlight.example; package it.tdlight.example;
import it.tdlight.Log;
import it.tdlight.client.*; import it.tdlight.client.*;
import it.tdlight.client.AuthenticationSupplier; import it.tdlight.client.AuthenticationSupplier;
import it.tdlight.client.CommandHandler; import it.tdlight.client.CommandHandler;
import it.tdlight.client.SimpleTelegramClient; import it.tdlight.client.SimpleTelegramClient;
import it.tdlight.client.TDLibSettings; import it.tdlight.client.TDLibSettings;
import it.tdlight.Init; import it.tdlight.Init;
import it.tdlight.Slf4JLogMessageHandler;
import it.tdlight.jni.TdApi.AuthorizationState; import it.tdlight.jni.TdApi.AuthorizationState;
import it.tdlight.jni.TdApi.Chat; import it.tdlight.jni.TdApi.Chat;
import it.tdlight.jni.TdApi.FormattedText; 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;
import it.tdlight.jni.TdApi.SendMessage; import it.tdlight.jni.TdApi.SendMessage;
import it.tdlight.jni.TdApi.TextEntity; import it.tdlight.jni.TdApi.TextEntity;
import it.tdlight.util.UnsupportedNativeLibraryException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -36,6 +37,9 @@ public final class Example {
// Initialize TDLight native libraries // Initialize TDLight native libraries
Init.init(); Init.init();
// Set the log level
Log.setLogMessageHandler(1, new Slf4JLogMessageHandler());
// Create the client factory // Create the client factory
try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) { try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) {
// Obtain the API token // Obtain the API token

View File

@ -54,27 +54,8 @@ public final class Init {
ConstructorDetector.init(); ConstructorDetector.init();
try { try {
NativeClientAccess.execute(new SetLogVerbosityLevel(3)); NativeClientAccess.execute(new SetLogVerbosityLevel(3));
Log.setLogMessageHandler(3, (verbosityLevel, message) -> { Log.setLogMessageHandler(3, new Slf4JLogMessageHandler());
switch (verbosityLevel) { Log.setLogStream(null);
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;
}
});
NativeClientAccess.execute(new SetLogStream(new LogStreamEmpty())); NativeClientAccess.execute(new SetLogStream(new LogStreamEmpty()));
} catch (Throwable ex) { } catch (Throwable ex) {
LOG.error("Can't set verbosity level on startup", ex); LOG.error("Can't set verbosity level on startup", ex);
@ -82,4 +63,5 @@ public final class Init {
} }
} }
} }
} }

View File

@ -1,15 +1,18 @@
package it.tdlight; package it.tdlight;
import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.LogStream;
import it.tdlight.jni.TdApi.LogStreamDefault; import it.tdlight.jni.TdApi.LogStreamDefault;
import it.tdlight.jni.TdApi.LogStreamEmpty;
import it.tdlight.jni.TdApi.LogStreamFile; import it.tdlight.jni.TdApi.LogStreamFile;
import it.tdlight.jni.TdApi.SetLogStream;
import it.tdlight.jni.TdApi.SetLogVerbosityLevel; import it.tdlight.jni.TdApi.SetLogVerbosityLevel;
import it.tdlight.tdnative.NativeClient.LogMessageHandler; import it.tdlight.tdnative.NativeClient.LogMessageHandler;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; 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 { public final class Log {
@ -86,6 +89,11 @@ public final class Log {
updateLog(); updateLog();
} }
public static void disable() {
setLogMessageHandler(0, null);
setLogStream(null);
}
/** /**
* *
* Sets the log message handler * Sets the log message handler
@ -96,6 +104,14 @@ public final class Log {
* @param logMessageHandler handler * @param logMessageHandler handler
*/ */
public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) { 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()));
} }
} }

View File

@ -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;
}
}
}