• python logger日志工具类


    pytest命令行执行默认不会打印log信息,需要加‘-s’参数或者 ‘–capture=no’,即pytest -s

    
    
    #! /usr/bin/env python
    # coding=utf-8
    import logging, os
    
    
    class logger:
        def __init__(self, path, clevel=logging.DEBUG, Flevel=logging.DEBUG):
            self.logger = logging.getLogger(path)
            self.logger.setLevel(logging.DEBUG)
            fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
            # 设置CMD日志
            sh = logging.StreamHandler()
            sh.setFormatter(fmt)
            sh.setLevel(clevel)
            # 设置文件日志
            fh = logging.FileHandler(path, encoding='utf-8')
            fh.setFormatter(fmt)
            fh.setLevel(Flevel)
            self.logger.addHandler(sh)
            self.logger.addHandler(fh)
    
        def debug(self, message):
            self.logger.debug(message)
    
        def info(self, message):
            self.logger.info(message)
    
        def war(self, message):
            self.logger.warn(message)
    
        def error(self, message):
            self.logger.error(message)
    
        def cri(self, message):
            self.logger.critical(message)
    
    
    if __name__ == '__main__':
        logyyx = logger('yyx.log', logging.DEBUG, logging.DEBUG)
        logyyx.debug('一个debug信息')
        logyyx.info('一个info信息')
        logyyx.war('一个warning信息')
        logyyx.error('一个error信息')
        logyyx.cri('一个致命critical信息')
  • 相关阅读:
    几种芯片封装
    Finder 快捷键
    Linux dnsmasq 服务
    java: private, protected, public
    java final
    Jenkins 搭建
    一款工作记录软件
    MacOS 修改主机名
    Ubuntu 静态IP
    adb tcp 调试
  • 原文地址:https://www.cnblogs.com/zipon/p/8289316.html
Copyright © 2020-2023  润新知