Python如何發(fā)送Syslog日志
更新時間:2023年09月06日 08:43:07 作者:飛Link
這篇文章主要介紹了Python如何發(fā)送Syslog日志問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Python發(fā)送Syslog日志
使用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協(xié)議發(fā)送syslog日志 client = pysyslogclient.SyslogClientRFC5424('192.168.10.101', 514, proto="UDP") # 通過TCP協(xié)議發(fā)送syslog日志 # client = pysyslogclient.SyslogClientRFC5424('192.168.10.101', 514, proto="TCP") client.log("test message")
python實現(xiàn)syslog客戶端
測試日志服務(wù)器是否可以正常收到日志,可以自由編輯日志內(nèi)容后來發(fā)送。
# -*- coding: utf-8 -*- from socket import * host=input('請輸入syslog服務(wù)器IP:') port=input('請輸入SYSLOG服務(wù)器端口(默認(rèn)為514):') or '514' s=socket(AF_INET,SOCK_DGRAM) s.connect((host,int(port))) while True: content = input('請輸入發(fā)送的測試日志內(nèi)容:') s.send(bytes(content,encoding='utf8')) print('發(fā)送完成!')
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中resample函數(shù)實現(xiàn)重采樣和降采樣代碼
今天小編就為大家分享一篇python中resample函數(shù)實現(xiàn)重采樣和降采樣代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python的collections模塊中的OrderedDict有序字典
字典是無序的,但是collections的OrderedDict類為我們提供了一個有序的字典結(jié)構(gòu),名副其實的Ordered+Dict,下面通過兩個例子來簡單了解下Python的collections模塊中的OrderedDict有序字典:2016-07-07Python中json格式數(shù)據(jù)的編碼與解碼方法詳解
這篇文章主要介紹了Python中json格式數(shù)據(jù)的編碼與解碼方法,詳細(xì)分析了Python針對json格式數(shù)據(jù)的編碼轉(zhuǎn)換操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-07-07