Logging¶
SAPI generates log output at different log levels. It requires registration of a custom log handler to invoke the logging framework of your choice.
A call to sapiRegisterLogHandler is required to register the log handler.
// Signature of the log callback
void logMessage(int level, const char *category, const char *message);
// Call that will register log handler
sapiRegisterLogHandler(logMessage);
// Signature of the log callback
void logMessage(int level, const char *category, const char *message)
{
printf("%d [%s] %s\n", level, category, message);
}
A call to Kxs.RegisterLogHandler is required to register the log handler.
internal class Program
{
private static void Main()
{
// Register the C# log method with the Kxs SDK.
Kxs.RegisterLogHandler(LogMessage);
// Create Kxs instance
Kxs kxs = new();
...
}
// Signature of the log callback
public static void LogMessage(int logLevel = 1, string logCategory = "", string logMessage = "")
{
Console.WriteLine($"{logLevel} [{logCategory}] {logMessage}");
}
}
Java uses slf4j to faciliate logging. The log handler is registered automatically.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Example log
public class Execute {
private static final Logger LOG = LoggerFactory.getLogger(Execute.class);
static final Logger logger = LoggerFactory.getLogger("My logger");
...
}
import logging
# Logging setup
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[logging.FileHandler("sapi.log", "a", "utf-8"), logging.StreamHandler()],
)
sapi = SAPI()
# Register the Python log method with the Kxs SDK.
sapi.register_log_handler(log_message)
# Signature of the log callback
def log_message(level: int, cat: str, msg: str) -> None:
logging.getLogger("CLIENT").log(level, f"[{cat}] {msg}")