Java : log syslog avec log4j

Boris HUISGEN December 19, 2015

développement java

package syslog;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.net.SyslogAppender;

public class test {
    private static final Logger log = Logger.getLogger(test.class);

    public static void main(String[] args) {
        Logger rootLogger = Logger.getRootLogger();
        
        rootLogger.setLevel(Level.DEBUG);

        PatternLayout layout = new PatternLayout("%t: %p: %m%n");
        SyslogAppender syslogAppender = new SyslogAppender(layout, "localhost",
                SyslogAppender.LOG_USER);
        
        Logger.getRootLogger().addAppender(syslogAppender);

        log.info("log message");
    }
}

Ne pas oublier d’activer l’écoute réseau du serveur syslog local :

# vim /etc/rsyslog.conf

# provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

See also

Git : annuler le dernier commit amend
Read more
Docker : activer les restrictions RAM et swap
Read more
Debian : fix startup Docker 1.7 et kernel 4.1
Read more