当 rsyslog 重新启动时,syslog4j 停止记录

syslog4j stops logging when rsyslog restarted

重新启动 rsyslog 守护进程后,syslog4j 0.9.46 在不重新启动 java 进程的情况下无法再成功写入系统日志。没有异常被抛出,我看不到通过 API 检查日志记录是否仍在工作的方式。有什么方法可以让 syslog4j 从 rsyslog 重启中恢复,或者至少抛出异常以便手动重启连接?

示例代码:

import java.util.Scanner;

import org.productivity.java.syslog4j.SyslogConstants;
import org.productivity.java.syslog4j.SyslogIF;
import org.productivity.java.syslog4j.impl.unix.socket.UnixSocketSyslog;
import org.productivity.java.syslog4j.impl.unix.socket.UnixSocketSyslogConfig;


public class SysLogging {

    public static void main(String[] args) {
        UnixSocketSyslogConfig cfg = new UnixSocketSyslogConfig();
        cfg.setFacility(SyslogConstants.FACILITY_LOCAL1);
        cfg.removeAllMessageModifiers();
        cfg.setIdent(null);
        cfg.setThrowExceptionOnWrite(true);
        cfg.setWriteRetries(5);
        SyslogIF log = new UnixSocketSyslog();
        log.initialize(SyslogConstants.UNIX_SOCKET, cfg);

        Scanner scanner = new Scanner(System.in);
        System.out.println("type quit to quit, anything else to log");
        String line = scanner.nextLine();
        while(!line.equals("quit")) {
            log.info(line);
            System.out.println("type quit to quit, anything else to log");
            line = scanner.nextLine();
        }
        scanner.close();
    }
}

rsyslog 重新启动之前,上面的代码可以正常工作。那时,系统日志中不再显示任何消息,尽管一切似乎都正常。

与此类似的技巧应该有所帮助:

    SyslogIF log = new UnixSocketSyslog() {
        @Override
        protected void write(int level, byte[] message) throws SyslogRuntimeException {
            if (this.fd == -1)
                connect();
            if (this.fd == -1)
                return;
            ByteBuffer byteBuffer = ByteBuffer.wrap(message);
            int ret = this.libraryInstance.write(this.fd,byteBuffer,message.length);
            if (ret < 0) {
                shutdown();
                connect();
                if (this.fd == -1)
                    return;
                ret = this.libraryInstance.write(this.fd,byteBuffer,message.length);
            }
        }
    };