zoukankan      html  css  js  c++  java
  • 多进程写日志

    import logging
    from cloghandler import ConcurrentRotatingFileHandler
    
    import os
    
    
    class ColoredFormatter(logging.Formatter):
        def __init__(self, fmt=None):
            logging.Formatter.__init__(self, fmt=fmt)
    
        def format(self, record):
            COLORS = {
                'Black': '0;30',
                'Red': '0;31',
                'Green': '0;32',
                'Brown': '0;33',
                'Blue': '0;34',
                'Purple': '0;35',
                'Cyan': '0;36',
                'Light_Gray': '0;37',
    
                'Dark_Gray': '1;30',
                'Light_Red': '1;31',
                'Light_Green': '1;32',
                'Yellow': '1;33',
                'Light_Blue': '1;34',
                'Light_Purple': '1;35',
                'Light_Cyan': '1;36',
                'White': '1;37',
            }
            COLOR_SEQ = "33[%sm"
            RESET_SEQ = "33[0m"
    
            message = logging.Formatter.format(self, record)
    
            if record.levelno == logging.DEBUG:
                message = COLOR_SEQ % COLORS['White'] + message + RESET_SEQ
            elif record.levelno == logging.INFO:
                message = COLOR_SEQ % COLORS['Green'] + message + RESET_SEQ
                pass
            elif record.levelno == logging.WARNING:
                message = COLOR_SEQ % COLORS['Brown'] + message + RESET_SEQ
            elif record.levelno == logging.ERROR:
                message = COLOR_SEQ % COLORS['Red'] + message + RESET_SEQ
            elif record.levelno == logging.CRITICAL:
                message = COLOR_SEQ % COLORS['Purple'] + message + RESET_SEQ
            return message
    
    
    import logging.handlers
    
    
    def get_logger(log_name="",
                   log_path='logs',
                   single_log_file_size=1024  * 1024 * 500,
                   log_to_file=True,
                   backup_count=1):
        """:return a logger"""
    
        if not os.path.exists(log_path):
            try:
                os.makedirs(log_path)
            except Exception as e:
                print(str(e))
    
        logger = logging.getLogger("{}".format(log_name))
        logger.setLevel(logging.DEBUG)
    
        if log_name and log_to_file:
            # file
            log_file = "{}/{}.log".format(log_path, log_name)
            fh = logging.handlers.ConcurrentRotatingFileHandler(log_file, maxBytes=single_log_file_size, backupCount=backup_count)
            color_formatter = ColoredFormatter(fmt='%(asctime)s %(funcName)s[line:%(lineno)d] [%(levelname)s]: %(message)s')
            fh.setFormatter(color_formatter)
            fh.setLevel(logging.DEBUG)
            logger.addHandler(fh)
    
        # stdout
        sh = logging.StreamHandler()
        color_formatter = ColoredFormatter(fmt='%(asctime)s %(funcName)s[line:%(lineno)d] [%(levelname)s]: %(message)s')
        sh.setFormatter(color_formatter)
        sh.setLevel(logging.DEBUG)
        logger.addHandler(sh)
    
        return logger
    
    
    # logger = get_logger("app.log", '/tmp/api_service')
    logger = get_logger("app.log")
    
    if __name__ == '__main__':
        while True:
            logger.info(f"{list(range(300000,400000))}")
    
  • 相关阅读:
    pip 使用代理
    npm i -S -D -g 区别
    javascript JSON. 转换 注意事项
    hexo 博客
    kotlin 注意的地方
    VMware-workstation-full-10.0.1-1379776 CN
    分析公司shareaholic报告:Chrome浏览器使用量居首
    搜狗高速浏览器4.2正式版发布
    中行用户购买KIS2014 68元/3年,时间:2013.10.18-2013.11.18
    1元抢卡巴KAV_不限量疯抢即日起至2013.10.31截止
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10594849.html
Copyright © 2011-2022 走看看