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

时间:2016-02-11 01:27:23

标签: java logging syslog rsyslog syslog4j

重新启动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重新启动。此时,系统日志中不再显示任何消息,尽管一切似乎都有效。

1 个答案:

答案 0 :(得分: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);
            }
        }
    };