一个简单的python日志服务器,主要目的是搜集各python logging记录的日志,将其简单汇总。源码如下:
# -*- coding: utf-8 -*- ''' Created on 2012-06-14 19:50 @summary: a simple logging server. use gevent and logging modules @author: JerryKwan ''' import os import pickle import logging import logging.handlers import struct from gevent.server import StreamServer # logger collection _loggers_ = {} def get_logger(address, name): address = address.replace(".", "_") logger_name = address + "_" + name if _loggers_.has_key(logger_name): return _loggers_[logger_name] else: print os.path.dirname(__file__) logs_dir = os.path.join(os.path.dirname(__file__), "logs", address) if os.path.exists(logs_dir) and os.path.isdir(logs_dir): pass else: os.makedirs(logs_dir) file_name = os.path.join(logs_dir, name+".log") # print file_name # define a rotating file handler rotatingFileHandler = logging.handlers.RotatingFileHandler(filename =file_name, maxBytes = 1024 * 1024 * 50, backupCount = 5) formatter = logging.Formatter("%(message)s") rotatingFileHandler.setFormatter(formatter) console = logging.StreamHandler() # console.setLevel(logging.NOTSET) # set a format which is simple for console use formatter = logging.Formatter("%(message)s") # tell the handler to use this format console.setFormatter(formatter) logger = logging.getLogger(logger_name) logger.addHandler(rotatingFileHandler) logger.addHandler(console) logger.setLevel(logging.DEBUG) _loggers_[logger_name] = logger return logger def handle_log_record(address, record): ''' @summary: handle log record @param address: address[ip] of the record generated from @param record: log record ''' # construct logger name process_name = record.processName process_id = record.process # print "process_name = %s, process_id = %s"%(process_name, process_id) logger_name = "%s_%s" %(process_name, process_id) logger = get_logger(address, logger_name) formatter = logging.Formatter( fmt="%(asctime)s %(name)-12s %(levelname)-8s %(message)s") record_str = formatter.format(record) # print record_str logger.info(record_str) # print "write" # print address, record # print record.filename def handle(socket, address): # get client ip try: while True: chunk = socket.recv(4) if len(chunk) < 4: break slen = struct.unpack(">L", chunk)[0] chunk = socket.recv(slen) while len(chunk) < slen: chunk += socket.recv(slen - len(chunk)) obj = pickle.loads(chunk) record = logging.makeLogRecord(obj) # handle_log_record(address[0], record) except Exception, e: pass finally: socket.close() # chunk = socket.recv(1024) # print "handle invoked" # socket.close() if __name__ == '__main__': port = 8888 server = StreamServer(('0.0.0.0', port), handle) print ('Starting logging service on port %s'%port) server.serve_forever()