- 使用logging模塊
import logging
import logging.handlers
# 日志對象的名稱
my_logger = logging.getLogger('chenjunfei_log')
my_logger.setLevel(logging.INFO)
# 日志格式
formatter = logging.Formatter("%(processName)s %(name)s %(message)s")
handler = logging.handlers.SysLogHandler(address=("192.168.10.101", 514))
handler.setFormatter(formatter)
my_logger.addHandler(handler)
my_logger.info("test")
- 使用pysyslogclient模塊
import pysyslogclient
# 通過UDP協議發送syslog日志
client = pysyslogclient.SyslogClientRFC5424('192.168.10.101', 514, proto="UDP")
# 通過TCP協議發送syslog日志
# client = pysyslogclient.SyslogClientRFC5424('192.168.10.101', 514, proto="TCP")
client.log("test message")