EDITLogBack 系统日志不工作 java
EDITLogBack Syslog not working java
我已经写了一个简单的项目 运行 登录 Ubuntu 按照这个 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);
}
当我 运行 我的项目时,我得到了同样的东西!这
来自 Java 的消息未发送至 syslog
!
这里是logback.xml。我放在资源文件夹中。
<?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>
我用这个命令来检查syslog
。 syslog
输出现在看起来正确了。不幸的是,从 Netbeans(JAVA) 发送的消息在 syslog
中仍未收到。
尝试cat /var/log/syslog
(小部分输出)
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:/$
不确定是不是因为 syslogHost 和 facility。
终于我让它工作了!真是一场噩梦...
我按照Rsyslog : Output Logs to Remote Host配置Syslog
服务器。
转到rsyslog.conf
sudo vim etc/rsyslog.conf
取消注释这两行。
module(load="imtcp")
input(type="imtcp" port="514")
保存并退出。然后重启服务。
sudo service rsyslog restart
神奇!
我已经写了一个简单的项目 运行 登录 Ubuntu 按照这个 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);
}
当我 运行 我的项目时,我得到了同样的东西!这
来自 Java 的消息未发送至 syslog
!
这里是logback.xml。我放在资源文件夹中。
<?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>
我用这个命令来检查syslog
。 syslog
输出现在看起来正确了。不幸的是,从 Netbeans(JAVA) 发送的消息在 syslog
中仍未收到。
尝试cat /var/log/syslog
(小部分输出)
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:/$
不确定是不是因为 syslogHost 和 facility。
终于我让它工作了!真是一场噩梦...
我按照Rsyslog : Output Logs to Remote Host配置Syslog
服务器。
转到rsyslog.conf
sudo vim etc/rsyslog.conf
取消注释这两行。
module(load="imtcp")
input(type="imtcp" port="514")
保存并退出。然后重启服务。
sudo service rsyslog restart
神奇!