内置库 Python logging及高级使用

python 日志模块

目录

  • 日志作用
  • 日志的级别
  • 日志的用法

日志作用

  • 调试
  • 辅助定位问题
  • 数据分析

日志的级别

日志的用法

https://docs.python.org/zh-cn/3/howto/logging.html

image

设置日志的级别

logging.basicConfig(level=logging.INFO)
import logging
# logging 默认设置的级别 是warning
# 设置成哪个级别 ,就会打印这个级别 及以上级别的日志
logging.basicConfig(level=logging.DEBUG)

logging.debug("这是debug日志")
logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything
logging.error("这是一条error级别的日志")

保存日志到文件

logging.basicConfig(filename='myapp.log', level=logging.INFO)
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)
logging.debug('This message should go to the log file')

logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

设置时间格式

logging.basicConfig(filename='myapp.log', level=logging.INFO,format='%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)', datefmt='%m/%d/%Y %I:%M:%S %p')
import logging
# logging.basicConfig(format='%(asctime)s %(message)s')
# logging.warning('is when this event was logged.')

# logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
# logging.warning('is when this event was logged.')

logging.basicConfig(filename='myapp.log',
                    level=logging.INFO,
                    format='%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')
logging.debug("这是debug日志")
logging.info("这是debug日志")

python 日志进阶用法

  • 日志记录的流程
  • 封装日志公共模块
  • 日志配置文件

python 日志进阶

python 日志记录流程

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
# 流处理器
ch = logging.StreamHandler()
# 文件处理器
# ch = logging.FileHandler("mylog.log",encoding='utf-8')
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)



# 定义一个文件处理器
# 文件处理器
ch_file = logging.FileHandler("mylog.log",encoding='utf-8')
ch_file.setLevel(logging.DEBUG)

# create formatter
formatter1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch_file.setFormatter(formatter1)

# add ch to logger
logger.addHandler(ch_file)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

python 日志定义

官网:https://docs.python.org/zh-cn/3/howto/logging.html

import logging
import os
# create logger 创建一个logger 记录器
logger = logging.getLogger(os.path.basename(__file__))
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

封装日志公共函数

https://ceshiren.com/t/topic/13564

import logging
import os

# 定义一个记录器
def get_logger():
    # create logger
    print(os.path.basename(__file__))
    # 记录器
    logger = logging.getLogger(os.path.basename(__file__))
    logger.setLevel(logging.DEBUG)
    # create console handler and set level to debug
    # 文件处理器
    ch = logging.FileHandler(filename='mylog.log', encoding="utf-8")
    ch.setLevel(logging.DEBUG)
    # create formatter
    # 定义格式器
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)
    return  logger

logger = get_logger()


def log_info(message):
    logger.info(message)

logger.debug('debug message')
# logger.info('info message')
log_info(" log info 方法")
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

日志配置文件
https://ceshiren.com/t/topic/13564