java 将Log4j2 的日志内容输出到udp上

在Maven 项目中 pom.xml 中添加Log4j2 的依赖 

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.14.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-1.2-api</artifactId>
      <version>2.14.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-jul</artifactId>
      <version>2.14.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>2.14.1</version>
    </dependency>
    <!-- 添加 Log4j2 SocketAppender 所需的依赖 -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.14.1</version>
      <classifier>udp</classifier>
    </dependency>

log4j2的配置文件  log4j2.xml 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Socket name="UdpAppender" host="localhost" port="8101" protocol="udp">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" charset="UTF-8"/>
        </Socket>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="UdpAppender"/>
        </Root>
    </Loggers>
</Configuration>

 

log4j2.xml 位置

 调用方法

package com.MyLogHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class App 
{
    private static final Logger logger = LogManager.getLogger(App.class);

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.warn("This is a warn message");
        logger.error("This is an error message");
        logger.fatal("This is a fatal message");
    }
}

这样就可以将Log4j2日志内容输出到 localhost 的 8101 端口上

 

监听 UDP 的接收端 代码

package com.UdpListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/***/
public class App {
    private static final int PORT = 8101; // 监听的端口号
    private static final int BUFFER_SIZE = 1024; // 缓冲区大小

    public static void main(String[] args) throws Exception {
        // 创建DatagramSocket实例,指定监听的端口号
        try (DatagramSocket socket = new DatagramSocket(PORT)) {
            System.out.println("UDP监听器启动,监听端口: " + PORT);
            // 创建接收数据的缓冲区
            byte[] buffer = new byte[BUFFER_SIZE];
            // 创建DatagramPacket实例,用于接收数据
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            // 循环接收数据
            while (true) {
                // 接收数据报文
                socket.receive(packet);
                // 从数据报文中获取数据
                String message = new String(buffer, 0, packet.getLength(), "UTF-8");
                // 输出接收到的消息
                //System.out.println("接收到消息: " + message + " 来自 " + packet.getAddress().getHostAddress() + ":" + packet.getPort());
                // 可以在这里添加处理接收到的消息的逻辑
                System.out.println("" + message );
                // 重置packet的长度,以便下次接收
                packet.setLength(buffer.length);
            }
        }
    }

}

 

posted @ 2024-10-28 15:53  海乐学习  阅读(9)  评论(0编辑  收藏  举报