EDITLogBack Syslog not working java

2019-07-27 04:37发布

问题:

I have wrote a simple project to run the log in Ubuntu by follow this example.

ApplicationStarter

package com.javacodegeeks.examples.logbacksyslogexample;

import com.javacodegeeks.examples.logbacksyslogexample.message.IMessageTransmitter;
import com.javacodegeeks.examples.logbacksyslogexample.message.kiwi.KiwiMessageTransmitterImpl;
import com.javacodegeeks.examples.logbacksyslogexample.message.rsyslog.RsyslogMessageTransmitterImpl;

public class ApplicationStarter {

    /**
     * Main method
     */
    public static void main( final String[] args ) {

        final IMessageTransmitter kiwiMessageTransmitter = new KiwiMessageTransmitterImpl();
        kiwiMessageTransmitter.send( "I am learning to send message to Syslog server" );

        final IMessageTransmitter rsyslogMessageTransmitter = new RsyslogMessageTransmitterImpl();
        rsyslogMessageTransmitter.send( "Logback can easily send message to Syslog server" );
    }
}

KiwiMessageTransmitterImpl

package com.javacodegeeks.examples.logbacksyslogexample.message.kiwi;

import com.javacodegeeks.examples.logbacksyslogexample.message.IMessageTransmitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KiwiMessageTransmitterImpl implements IMessageTransmitter {

    private static final Logger LOGGER  = LoggerFactory.getLogger( KiwiMessageTransmitterImpl.class );

    @Override
    public void send( final String message ) {
        LOGGER.info( "Hello! My message is : {}", message );
    }
}

RsyslogMessageTransmitterImpl

package com.javacodegeeks.examples.logbacksyslogexample.message.rsyslog;

import com.javacodegeeks.examples.logbacksyslogexample.message.IMessageTransmitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RsyslogMessageTransmitterImpl implements IMessageTransmitter {

    private static final Logger LOGGER  = LoggerFactory.getLogger( RsyslogMessageTransmitterImpl.class );

    @Override
    public void send( final String message ) {
        LOGGER.info( "Hello! My message is : {}", message );
    }
}

IMessageTransmitter

package com.javacodegeeks.examples.logbacksyslogexample.message;

public interface IMessageTransmitter {

    void send(final String message);
}

When I run my project , I get the same things! The message from Java are not sending to syslog !

Here the logback.xml. I put in resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="KIWI" class="ch.qos.logback.classic.net.SyslogAppender">
        <syslogHost>localhost</syslogHost>
        <facility>LOCAL0</facility>
        <suffixPattern>%thread: %-5level %logger{36} - %msg%n</suffixPattern>
    </appender>

    <appender name="RSYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
        <!-- Ubuntu Server host name -->
        <syslogHost>centol</syslogHost>
        <facility>LOCAL1</facility>
        <suffixPattern>%thread: %-5level %logger{36} - %msg%n</suffixPattern>
    </appender>

    <logger name="com.javacodegeeks.examples.logbacksyslogexample.message.kiwi" level="INFO">
        <appender-ref ref="KIWI" />
    </logger>

    <logger name="com.javacodegeeks.examples.logbacksyslogexample.message.rsyslog" level="INFO">
        <appender-ref ref="RSYSLOG" />
    </logger>

</configuration>

I use this command to examine syslog. The syslog output looked correctly now. Unfortunately, the message send from Netbeans(JAVA) still not receiving in the syslog.

Try cat /var/log/syslog (small part of the output)

Jun  6 23:37:26 xxx whoopsie[1040]: [23:37:26] online
Jun  6 23:37:26 xxx avahi-daemon[1023]: Registering new address record for 2001:e68:4424:afab:c31f:c843:2351:c58 on wlp6s0.*.
Jun  6 23:37:28 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 4340ms.
Jun  6 23:37:32 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 9080ms.
Jun  6 23:37:41 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 17540ms.
Jun  6 23:37:59 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 34190ms.
Jun  6 23:38:09 xxx NetworkManager[1013]: <warn>  [1496763489.9447] dhcp6 (wlp6s0): request timed out
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9448] dhcp6 (wlp6s0): state changed unknown -> timeout
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): canceled DHCP transaction, DHCP client pid 19397
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): state changed timeout -> done
user@xxx:/$ 

Not sure is it because of the syslogHost and facility.

回答1:

FINALLY I get it to work! What a nightmare...

I follow Rsyslog : Output Logs to Remote Host to configure Syslog server.

Go to rsyslog.conf

sudo vim etc/rsyslog.conf

Uncomment these two lines.

module(load="imtcp")
input(type="imtcp" port="514")

Save and exit. Then Restart service.

sudo service rsyslog restart

MAGIC !