Python syslog demo 实现本地调试

client端代码

import logging
from logging.handlers import SysLogHandler
import time

# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Ensure the logger level is set to INFO

# Create a SysLogHandler
syslog_handler = SysLogHandler(address=('127.0.0.1', 5514))  # Adjust the address and port as needed

# Set the level of the handler to INFO
syslog_handler.setLevel(logging.INFO)

# Add the handler to the logger
logger.addHandler(syslog_handler)

# Define the data to send
data = {
    "title": "test",
    "Server_ip": "0.0.0.0",
    "client": "test",
    "client_ip": "1.1.1.1",
    "class": "test",
}

# Convert the data to a string
data_str = str(data)

# Send the data to the syslog server
logger.info(data_str)
print(f"Sent log: {data_str}")

Server段代码

import socketserver
import threading


# Create a simple syslog server for testing
class SyslogUDPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        data = bytes.decode(self.request[0].strip())
        print(f"Received log: {data}")


def start_syslog_server(host='127.0.0.1', port=5514):
    server = socketserver.UDPServer((host, port), SyslogUDPHandler)
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    return server


# Start the local syslog server
syslog_server = start_syslog_server()
print("Syslog server started.")

# Keep the main thread alive to allow continuous listening
try:
    while True:
        threading.Event().wait(1)
except KeyboardInterrupt:
    print("Syslog server stopped.")

运行顺序

先启动服务端代码。

再启动客户端代码。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值