简体   繁体   English

Log4j2 SecureTcpSocketServer安装程序

[英]Log4j2 SecureTcpSocketServer Setup

I'm trying to set up a log4j2 SecureTcpSocketServer that will receive logs from a remote application (logging with log4j2). 我正在尝试设置一个log4j2 SecureTcpSocketServer,它将从远程应用程序接收日志(使用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. 我已经使用TcpSocketServer对其进行了原型设计,可以通过提供端口号和配置文件从命令行运行。 The SecureTcpSocketServer however requires additional information to construct, such as a 'LogEventBridge' object and a 'SslConfiguration' object. 但是,SecureTcpSocketServer需要构造其他信息,例如“LogEventBridge”对象和“SslConfiguration”对象。 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. 我不知道如何将这些对象提供给命令行,所以我决定编写一个Java程序来初始化和运行服务器。 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){}
    }
}

Using this code, the server seems to start up and listen on port 5514 (I used netstat to verify). 使用此代码,服务器似乎启动并侦听端口5514(我使用netstat进行验证)。 On the client side, my log4j2 application seems to connect, but I don't see any of the logs coming through. 在客户端,我的log4j2应用程序似乎连接,但我没有看到任何日志通过。 Here is my client code: 这是我的客户端代码:

Xml Configuration file: 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>

Here is my Java code: 这是我的Java代码:

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");
    }
}

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. 当我开始编写代码时,我认为我需要做的就是实例化一个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? 这让我觉得服务器端的无限循环会以某种方式干扰?

Looking at the source for SecureTcpSocketServer and its superclass TcpSocketServer , I think just constructing an instance is not enough to start the server. 查看SecureTcpSocketServer及其超类TcpSocketServer的源代码,我认为构建实例并不足以启动服务器。 You need to call its run() method to get it to start accepting connections. 您需要调用其run()方法以使其开始接受连接。

In your log4j2SocketServer class, replace while(true){} with myServer.run(); 在你的log4j2SocketServer类中,用myServer.run();替换while(true){} myServer.run(); . This method will block until active is set to false or until the serverSocket is closed. 此方法将阻塞,直到active设置为false或者直到serverSocket关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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