一個簡單的pythonLog Service器

來源:互聯網
上載者:User

一個簡單的pythonLog Service器,主要目的是搜集各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 osimport pickleimport loggingimport logging.handlersimport structfrom 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 loggerdef 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.filenamedef 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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.