ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

wukong-robot 日志模块

2021-06-12 11:03:46  阅读:231  来源: 互联网

标签:logger logging log lines robot len path 日志 wukong


wukong-robot 日志模块

日志文件:

import logging
import os
from robot import constants
from logging.handlers import RotatingFileHandler

PAGE = 4096

DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR

def tail(filepath, n=10):
    """
    实现 tail -n
    """
    res = ""
    with open(filepath, 'rb') as f:
        f_len = f.seek(0, 2)
        rem = f_len % PAGE
        page_n = f_len // PAGE
        r_len = rem if rem else PAGE
        while True:
            # 如果读取的页大小>=文件大小,直接读取数据输出
            if r_len >= f_len:
                f.seek(0)
                lines = f.readlines()[::-1]
                break

            f.seek(-r_len, 2)
            # print('f_len: {}, rem: {}, page_n: {}, r_len: {}'.format(f_len, rem, page_n, r_len))
            lines = f.readlines()[::-1]
            count = len(lines) -1   # 末行可能不完整,减一行,加大读取量

            if count >= n:  # 如果读取到的行数>=指定行数,则退出循环读取数据
                break
            else:   # 如果读取行数不够,载入更多的页大小读取数据
                r_len += PAGE
                page_n -= 1

    for line in lines[:n][::-1]:
        res += line.decode('utf-8')
    return res

def getLogger(name):
    """ 
    作用同标准模块 logging.getLogger(name) 
    
    :returns: logger
    """
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s - %(funcName)s - line %(lineno)s - %(levelname)s - %(message)s')

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    
    # FileHandler
    file_handler = RotatingFileHandler(os.path.join(constants.TEMP_PATH, 'wukong.log'), maxBytes=1024*1024,backupCount=5)
    file_handler.setLevel(level=logging.DEBUG)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger

def readLog(lines=200):
    """ 
    获取最新的指定行数的 log

    :param lines: 最大的行数
    :returns: 最新指定行数的 log
    """
    log_path = os.path.join(constants.TEMP_PATH, 'wukong.log')
    if os.path.exists(log_path):
        return tail(log_path, lines)
    return ''
    

日志文件中包含三个函数:tail、getLogger和readLog

这里主要说明一下getLogger.

getLogger:在其他每一个每一个文件中调用,这个函数将配置该文件中的logger模块。这里配置输出格式、文件输出和终端输出。这样每一个文件都会实例化log模块

回显

try:
    open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
    raise
except Exception, e:
    logger.error('Failed to open file', exc_info=True)

exc_info这个置位TRUE,则在异常时会将内容输出文件和终端上,如果置为FALSE,则不会

这里记录一下,好的日志模块的https://github.com/ydf0509/nb_log 路径

标签:logger,logging,log,lines,robot,len,path,日志,wukong
来源: https://www.cnblogs.com/chenfengyijiu/p/14877826.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有