Java SyslogOutputStream-class And Method Code Example

Here is an example of how to use the class to send log events to a syslog server using a specified protocol and format:


public class Main {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getByName("");
            SyslogOutputStream syslog = new SyslogOutputStream(address, SyslogOutputStream.LOG_LOCAL0, SyslogOutputStream.LOG_INFO);
            syslog.write("This is a log message".getBytes());
        } catch (IOException e) {

In this example, an instance of SyslogOutputStream is created, connecting to the syslog server at "", with a facility of "LOG_LOCAL0" and a level of "LOG_INFO" using the default protocol(UDP). The write method is used to write a log message to the syslog server, the flush method is called to ensure the message is sent and the close method is called to close the syslog connection.

It's important to note that the SyslogOutputStream uses the syslog protocol over UDP or TCP based on the provided constructor. The protocol can also be specified explicitly by passing in a SyslogWriter to the constructor. And the SyslogOutputStream uses the BSD syslog protocol, which includes a priority level based on the facility and level of the log event.

Keep in mind that you need a running syslog server to receive the log messages.

Please ensure to close the outputstream after use as it releases the underlying resources.