简体   繁体   中英

Log4j2 Socket Appender “connect java.net.ConnectException: Connection refused: connect”

I'm trying to use the new log4j2 with a Socket Appender but I'm a bit unlucky. Here is my XML configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
        <Socket name="socket" host="localhost" port="9600">
            <SerializedLayout />
        </Socket>
      </Appenders>
  <Loggers>
    <Logger name="com.mycorp" level="info" />
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="socket"/>
        </Root>
  </Loggers>
</Configuration>

Here is my Java code:

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.io.*;
import java.sql.SQLException;
import org.apache.logging.log4j.core.net.*;

public class SyslogLogger
{
    private static final Logger LOG = LogManager.getLogger(SyslogLogger.class);

    public static void main (String[] args)throws IOException,SQLException
    {
        LOG.info("commit(). Query {}", "commit(). Query {}");
    }
}

When executing the code I'm getting:

2016-06-29 17:13:42,426 main ERROR Unable to write to stream TCP:127.0.0.1:9600 for appender socket: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:9600 socket not available
2016-06-29 17:13:42,426 main ERROR An exception occurred processing Appender socket org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:9600 socket not available

Should I create a TCP socket explicitly? or Log4j2 does it for me? I saw a few posts about Logstash, is that required here? Please note that I just want to sent the messages, without actually catching them at this time. In addition, I'm experiencing similiar issues with Syslog Adapter as well.

You need a server side to this client which logs the events to socket appender. Your server side would look something like :

public static void main(String args[])
{
    TcpSocketServer server = null;
    try {
        server = new TcpSocketServer(9600,new ObjectInputStreamLogEventBridge());
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    server.run();
}

Here, a Tcp Socket is open on 9600 and keeps listening for log events as long as this server runs. You would also need a log4j2 configuration corresponding to this server.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyAppServer" packages="">
  <Appenders>
     <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console
  </Appenders>
  <Loggers>
     <Logger name="com.mycorp" level="info" />
        <Root level="info">
        <AppenderRef ref="Console"/>
        </Root>
  </Loggers>
</Configuration>

Now, all the log events written by any logger which is a child to com.mycorp would be appended to console where your server runs.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM