Java AbstractSSLSocketAppender-class And Method Code Example
Here is an example of how to use the logback.core.net.ssl.AbstractSSLSocketAppender class to send log events over a secure socket connection:
import ch.qos.logback.core.net.ssl.ConfigurableSSLSocketAppender;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
public static void main(String[] args) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
ConfigurableSSLSocketAppender sslAppender = new ConfigurableSSLSocketAppender();
sslAppender.setContext(context);
sslAppender.setName("ssl");
sslAppender.setRemoteHost("localhost");
sslAppender.setPort(12345);
sslAppender.setReconnectionDelay(5000);
sslAppender.start();
Logger logger = LoggerFactory.getLogger("Main");
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warning message");
logger.error("This is an error message");
sslAppender.stop();
}
}
In this example, an instance of ConfigurableSSLSocketAppender is created and configured to connect to a remote host at "localhost" on port 12345, with a reconnection delay of 5 seconds. Then a logger object is retrieved by LoggerFactory.getLogger("Main") and various log events are sent over the secure socket connection.
It will send the log events to the given remotehost and port in this example 'localhost:12345' and you should have SSL configured in the logback.xml for this to work properly.