Add Slf4JLogMessageHandler
This commit is contained in:
parent
c3596f9ae4
commit
dedabdde2e
@ -26,7 +26,7 @@
|
||||
<dependency>
|
||||
<groupId>it.tdlight</groupId>
|
||||
<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>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user