logging 模块

import logging
https://www.cnblogs.com/yuanchenqi/articles/5732581.html
https://www.cnblogs.com/qianyuliang/articles/7234217.html#top

import logging
#logging=logging.getLogger('AAA')
logging.basicConfig(level=logging.DEBUG,
                    format='%(name)s %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    #filename='/tmp/test.log',
                    #filemode=
                     )
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')


logging.notset()  0
logging.debug()   10
logging.info()    20
logging.warning() 30
logging.error()   40
logging.critical()50


logging.basicConfig(**kwargs)
filename     指定记录日志的文件名
filemode     指定文件的打开方式默认'a'
format       指定日志格式
datefmt      指定时间格式
level        设置根日志级别
stream       不能和filename 同时使用,否则stream不生效

format可识别的变量
%(name)s    记录器的名称
%(levelno)s 数字形式的日志级别
%(levename)s 文本形式的日志级别
%(pathname)s 日志的路径名
%(filename)s 日志名
%(funcName)s 执行日志调用的函数名
%(module)s   执行日志调用的模块名
%(lineno)d   执行日志调用的行号
%(created)f  日志记录时的时间
%(asctime)s  日志调用的时间
%(thread)d   线程id
%(threadName)s 线程名
%(process)d  进程id
%(message)s   记录的信息

import logging
'''
1.创建logger对象
2.创建格式 
3.设置级别
4.创建handel

'''
import logging
#
logger=logging.getLogger(name="测试")
#
formatter=logging.Formatter('%(name)s-%(asctime)s-%(message)s')
#
logger.setLevel(level=logging.INFO)
#
fhander=logging.StreamHandler()
#
fhander.setFormatter(formatter)
#
logger.addHandler(hdlr=fhander)
#


def f():
    logging.warning('AAAAA')
a=f()
==============
自定义一个log模块
import logging

def log(filename,LoggerLevel=logging.INFO,LoggerName=None):
    #
    logger=logging.getLogger(LoggerName)
    #
    logger.setLevel(LoggerLevel)
    #
    fhandel=logging.FileHandler(filename, mode='a', encoding=None, delay=False)
    shandel=logging.StreamHandler()
    #
    logger.addHandler(fhandel)
    logger.addHandler(shandel)
    #
    formater=logging.Formatter(fmt="%(asctime)s \033[34m %(levelname)s \033[0m %(filename)s[line:%(lineno)d] %(message)s",
                               datefmt="%Y%m%d%H:%M:%S",
                               style='%',
                               validate=True)
    fhandel.setFormatter(formater)
    shandel.setFormatter(formater)
    #
    return logger
if __name__=='__main__':
    log=log('log.txt')
    log.warning("this is a waring")

import logging  # 引入logging模块
import os.path
import time
# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/Logs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
# 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')
posted @ 2022-01-12 14:41  mingtian是吧  阅读(9)  评论(0编辑  收藏  举报