python日志

日志作用

  日志记录一些信息

  用户个性化操作的记录

  开发人员查bug

 

logging 模块

函数版

import logging    应用日志logging模块
logging.basicConfig(
    level = logging.DEBUG,     #修改日志级别
    format = '%(asctime)s - %(name)s - [%(lineno)d] - %(message)s'
)  
def func(x):
    logging.debug("问题报告")
    name = x
    logging.info("执行信息")
    if name > 10:
        logging.info("判断执行")
    else:
        logging.warning("问题警告")
    ma = x + 1
    logging.error('error报告错误')
    print(ma)
    logging.critical("crmaitical报告严重错误")

func(5)

# 2019-01-16 15:58:05,553 - root - [7] - debug问题报告
# 2019-01-16 15:58:05,553 - root - [9] - info执行信息
# 2019-01-16 15:58:05,554 - root - [13] - warning问题警告
# 2019-01-16 15:58:05,554 - root - [15] - error报告错误
# 2019-01-16 15:58:05,554 - root - [17] - crmaitical报告严重错误
# 6

 

 

函数名   意义 等级 等级
logging.debug 程序排bug时记录 问题 level=10 level = logging.DEBUG
logging.info 程序正常运行时记录 信息 level=20 level = logging.INFO
logging.warning 程序警告提示 警告 level=30 level = logging.WARNING
logging.error 程序出错误时记录 错误 level=40 level = logging.ERROR
logging.crmatical 严重、危险,程序不能执行时 危险 level=50 level = logging.CRMAITICAL

 

修改日志记录级别

import logging    《修改记录级别》
logging.basicConfig(
    level = logging.DEBUG
)
def func():
    logging.debug("debug")
    logging.info("info")
    logging.warning("warning")
    logging.error('error')
    logging.critical("crmaitical")
func()

# DEBUG:root:debug
# INFO:root:info
# WARNING:root:warning
# ERROR:root:error
# CRITICAL:root:crmaitical

 

 

import logging        《格式化打印》
logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s - %(name)s - [%(lineno)d] - %(message)s'
)        #时间      用户     行数          记录信息
def func():
    logging.debug("debug")
    logging.info("info")
    logging.warning("warning")
    logging.error('error')
    logging.critical("crmaitical")
func()
# 2019-01-16 16:35:47,700 - root - [7] - debug
# 2019-01-16 16:35:47,700 - root - [8] - info
# 2019-01-16 16:35:47,700 - root - [9] - warning
# 2019-01-16 16:35:47,700 - root - [10] - error
# 2019-01-16 16:35:47,700 - root - [11] - crmaitical
               毫秒

 

 

import logging
logging.basicConfig(
    level= 10,   #设置记录级别
        # logging.DEBUG         10
        # logging.INFO          20
        # logging.WARNING       30
        # logging.ERROR         40
        # logging.CRITICAL      50

    format= "%(asctime)s - %(message)s",    #输出格式
        # %(name)s              Logger的名字
        # %(levelno)s           数字形式的日志级别
        # %(levelname)s         文本形式的日志级别
        # %(pathname)s          调用日志输出函数的模块的完整路径名,可能没有
        # %(filename)s          调用日志输出函数的模块的文件名
        # %(module)s            调用日志输出函数的模块名
        # %(funcName)s          调用日志输出函数的函数名
        # %(lineno)d            调用日志输出函数的语句所在的代码行
        # %(created)f           当前时间,用UNIX标准的表示时间的浮 点数表示
        # %(relativeCreated)d   输出日志信息时的,自Logger创建以 来的毫秒数
        # %(asctime)s           字符串形式的当前时间。
        #                       默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
        # %(thread)d            线程ID。可能没有
        # %(threadName)s        线程名。可能没有
        # %(process)d           进程ID。可能没有
        # %(message)s           用户输出的消息

    # filename= "text",         #指定文件名储存日志,创建FiledHandler 编码gbk
    # filemode= "a",            #指定文件名后使用,默认为“a”,可指定“w”
    datefmt= "%a, %d %b %Y %H:%M:%S",     #
    # stream=
        # 用指定的stream创建StreamHandler。
        # 可以指定输出到sys.stderr,sys.stdout
        # 或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。
        # 若同时列出了filename和stream两个参数,则stream参数会被忽略。
)
logging.debug(
"debug") logging.info("info") logging.warning("warning") logging.error('error') logging.critical("crmaitical")

 

 

logger对象配置

 

import logging

logger = logging.getLogger(“name”)

fh = logging.FileHandler("test.log",encoding="utf-8")
fh2 = logging.FileHandler("test2.log",encoding="utf-8")
sh= logging.StreamHandler()

format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
fh.setFormatter(format)
fh2.setFormatter(logging.Formatter('%(asctime)s \ %(name)s \ %(message)s'))
sh.setFormatter(logging.Formatter('%(asctime)s \ %(name)s \ %(message)s'))

logger.setLevel(logging.WARNING)

logger.addHandler(fh)
logger.addHandler(sh)
logger.addHandler(fh2)

logger.debug('D')
logger.info('I')
logger.warning('W')
logger.error('E')
logger.critical('C')

logger.removeHandler(fh,sh,fh2)

 

 

 

 

 

 

 

日志

posted on 2019-01-16 18:43  六月_海动  阅读(85)  评论(0)    收藏  举报