3

重启rsyslog守护进程后,syslog4j 0.9.46无法在不重启 java 进程的情况下成功写入 syslog。没有抛出异常,我看不到通过 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重新启动。此时系统日志中不再出现任何消​​息,尽管一切似乎都正常工作。

4

1 回答 1

1

与此类似的破解应该会有所帮助:

    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);
            }
        }
    };
于 2016-02-11T20:59:02.137 回答