#!/usr/bin/env python | |
## Tiny Syslog Server in Python. | |
## | |
## This is a tiny syslog server that is able to receive UDP based syslog | |
## entries on a specified port and save them to a file. | |
## That's it... it does nothing else... | |
## There are a few configuration parameters. | |
LOG_FILE = 'youlogfile.log' | |
HOST, PORT = "0.0.0.0", 514 | |
# | |
# NO USER SERVICEABLE PARTS BELOW HERE... | |
# | |
import logging | |
import SocketServer | |
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a') | |
class SyslogUDPHandler(SocketServer.BaseRequestHandler): | |
def handle(self): | |
data = bytes.decode(self.request[0].strip()) | |
socket = self.request[1] | |
print( "%s : " % self.client_address[0], str(data)) | |
logging.info(str(data)) | |
if __name__ == "__main__": | |
try: | |
server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler) | |
server.serve_forever(poll_interval=0.5) | |
except (IOError, SystemExit): | |
raise | |
except KeyboardInterrupt: | |
print ("Crtl+C Pressed. Shutting down.") |
1
kmvan 2014-07-06 18:27:17 +08:00
虽然不是很明白,但感觉很厉害的样子。
|
2
Livid MOD OP |
3
zorceta 2014-07-06 18:33:28 +08:00 via Android
这么看起来 syslog 就是个远程日志服务器……
|
4
mckelvin 2014-07-06 18:40:17 +08:00
类似日志收集的事情,pyzmq提供了 zmq.log.handlers.PUBHandler ,可以很方便扩展:
一个例子: https://github.com/zeromq/pyzmq/blob/master/examples/logger/zmqlogger.py 一篇很好的ZeroMQ介绍: http://nichol.as/zeromq-an-introduction |