gpt4 book ai didi

java - EDITLogBack Syslog 不工作 java

转载 作者:行者123 更新时间:2023-11-30 06:40:57 25 4
gpt4 key购买 nike

我编写了一个简单的项目,按照这个 example 在 Ubuntu 中运行日志。

应用程序启动器

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>

我使用此命令来检查syslogsyslog 输出现在看起来正确了。不幸的是,从 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 和设施的原因。

最佳答案

终于我让它工作了!真是一场噩梦……

我关注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

神奇!

关于java - EDITLogBack Syslog 不工作 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44388068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com