Log4j2 SecureTcpSocketServer设置 [英] Log4j2 SecureTcpSocketServer Setup

查看:396
本文介绍了Log4j2 SecureTcpSocketServer设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个log4j2 SecureTcpSocketServer,它将接收来自远程应用程序的日志(使用log4j2进行日志记录).我已经使用TcpSocketServer对此进行了原型设计,可以通过提供端口号和配置文件从命令行运行TcpSocketServer.但是,SecureTcpSocketServer需要构造其他信息,例如"LogEventBridge"对象和"SslConfiguration"对象.我看不到如何向命令行提供这些对象,所以我决定编写一个Java程序来初始化和运行服务器.这是我的代码:

I'm trying to set up a log4j2 SecureTcpSocketServer that will receive logs from a remote application (logging with log4j2). I've already prototyped this using the TcpSocketServer, which can be run from the command line by providing a port number and configuration file. The SecureTcpSocketServer however requires additional information to construct, such as a 'LogEventBridge' object and a 'SslConfiguration' object. I don't see how I could provide these objects to the command line so I decided to write a Java program to initialize and run the server. Here is my code:

public class log4j2SocketServer {

    public static void main(String[] args) {

        KeyStoreConfiguration keyStoreConf = null;
        TrustStoreConfiguration trustStoreConf = null;
        SecureTcpSocketServer<ObjectInputStream> myServer = null;
        try {
            keyStoreConf = new KeyStoreConfiguration("keystore.jks", "password", null, null);
        } catch (StoreConfigurationException e) {
            e.printStackTrace();
        }
        try {
            trustStoreConf = new TrustStoreConfiguration("keystore.jks", "password", null, null);
        } catch (StoreConfigurationException e) {
            e.printStackTrace();
        }
        SslConfiguration sslConf = SslConfiguration.createSSLConfiguration("SSL", keyStoreConf, trustStoreConf);
        try {
            myServer = new SecureTcpSocketServer<ObjectInputStream>(5514,new ObjectInputStreamLogEventBridge(),sslConf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true){}
    }
}

使用此代码,服务器似乎启动并在端口5514上侦听(我使用netstat进行验证).在客户端,我的log4j2应用程序似乎已连接,但我看不到任何日志通过.这是我的客户代码:

Using this code, the server seems to start up and listen on port 5514 (I used netstat to verify). On the client side, my log4j2 application seems to connect, but I don't see any of the logs coming through. Here is my client code:

Xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="remoteLogging" packages="">
  <Appenders>
    <Socket name="socket" host="xx.xx.xx.xx" port="5514">
      <SerializedLayout />
      <SSL>
        <KeyStore location="keystore.jks" password="password"/>
        <TrustStore location="keystore.jks" password="password"/>
      </SSL>
    </Socket>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="socket"/>
    </Root>
  </Loggers>
</Configuration>

这是我的Java代码:

Here is my Java code:

public class remoteLogging {
    static final Logger log = LogManager.getLogger(remoteLogging.class.getName());
    public static void main(String[] args) {
        log.debug("Hello this is a debug message");
        log.info("Hello this is an info message");
        log.warn("Hello this is a warn message");
        log.error("Hello this is a error message");
    }
}

我最关心的是我编写服务器代码的方式.当我开始编写代码时,我想我需要做的就是实例化SecureTcpSocketServer.我这样做了,然后(显然)程序运行了,然后...结束,并且垃圾回收了我的服务器.因此,我决定在此之后立即进行无限循环,以使服务器保持活动状态.我严重怀疑这是执行此操作的正确方法,但是我对编码还很陌生,我不确定最好的方法是什么.我的问题是,这种实施可行吗?我的意思是有明显出乱子,因为我没有收到日志...这是循环造成的问题?还是我没有考虑过其他问题?

My primary concern is the way I wrote my server code. When I started writing the code, I figured all I'd need to do is instantiate a SecureTcpSocketServer. I did this, and then (obviously) the program ran, and then...ended, and garbage collected my server. So then I decided to put a infinite loop in right afterwards, to keep the server alive. I seriously doubt this is the proper way to do this, but I'm fairly new to coding and I'm not sure what the best approach is. My question is, is this implementation okay? I mean there's obviously something going wrong, since I'm not receiving logs...Is this loop causing problems? Or is there something else wrong that I haven't considered?

一两件事:我希望我的客户端应用程序来运行,连接到服务器并发送其日志,然后终止.现在,它似乎已连接,但似乎未发送任何日志,然后它挂起,直到我手动终止它为止.这使我认为服务器端无限循环正在以某种方式进行干扰?

One more thing: I would expect my client application to run, connect to the server and send its logs, and then terminate. Right now, it seems to connect, but no logs seem to be sent, and then it just hangs until I manually terminate it. This makes me think the server-side infinite loop is interfering somehow?

推荐答案

SecureTcpSocketServer及其超类TcpSocketServer的源代码,我认为仅构造一个实例不足以启动服务器.您需要调用其run()方法以使其开始接受连接.

Looking at the source for SecureTcpSocketServer and its superclass TcpSocketServer, I think just constructing an instance is not enough to start the server. You need to call its run() method to get it to start accepting connections.

在您的log4j2SocketServer类中,将while(true){}替换为myServer.run();.该方法将一直阻塞,直到active设置为false或关闭serverSocket.

In your log4j2SocketServer class, replace while(true){} with myServer.run();. This method will block until active is set to false or until the serverSocket is closed.

这篇关于Log4j2 SecureTcpSocketServer设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆