python loggin库的使用

基本使用

import logging
import traceback

logging.basicConfig(
    level=logging.DEBUG,
    # asctiome 表示当前时间  levelname 表示日志等级  filename 当前文件名  lineno 行  message 信息
    format='%(asctime)s %(levelname)s %(filename)s %(lineno)s:%(message)s',
    filename='./py.log',
    encoding='utf-8'
)

logging.debug('this is a debug message')
logging.info('this is a info message')
logging.warning('this is a warning message')
logging.error('this is a error message')
logging.critical('this is a critical message')


try:
    logging.info('这是try中的代码')
    1/0
except Exception as e:
    logging.error(f'异常,内容为:{e}')
    logging.error(traceback.format_exc())

四组件使用


import logging

# 创建一个logger
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG) # 定义日志等级

# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('my_log.log')
fh.setLevel(logging.DEBUG)

# 创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# 创建一个formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# 为handler添加formatter
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 为logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)

# 记录一些信息
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

# 记录一些格式化的信息
logger.debug('This is a debug message with %s', 'format')
logger.info('This is an info message with %s', 'format')
logger.warning('This is a warning message with %s', 'format')
logger.error('This is an error message with %s', 'format')
logger.critical('This is a critical message with %s', 'format')

# 记录一些包含 traceback 的信息
try:
    raise Exception('This is a test exception')
except Exception as e:
    logger.error('This is an error message with traceback', exc_info=True)

# 关闭 handler
fh.close()
ch.close()

# 重命名 logger
new_logger = logging.getLogger('new_logger')
new_logger.setLevel(logging.DEBUG)
new_logger.addHandler(fh)
new_logger.addHandler(ch)

通过logconfig的方式调用

[loggers]
keys = root

[handlers]
keys = fileHandlers,streamHandlers

[formatters]
keys = simpleFormatter

[logger_root]
level = DEBUG
handlers = fileHandlers,streamHandlers

[handler_fileHandlers]
class = FileHandler
level = WARNING
formatter = simpleFormatter
args = ('./log/app.log','a','utf-8')

[handler_streamHandlers]
class = StreamHandler
level = DEBUG
formatter = simpleFormatter
stream = ext://sys.stdout

[formatter_simpleFormatter]
class=logging.Formatter
format = %(asctime)s - %(name)s - %(levelname)s %(filename)s - %(lineno)s:%(message)s


# 封装日志
def getLogger():
    # file 反向解析获取绝对路径 __file__  得到当前python文件的路径
    file_path = pathlib.Path(__file__).parents[0].resolve() / 'log_conf.ini'
    print(file_path)
    logging.config.fileConfig(file_path)
    logger = logging.getLogger('root')
    return logger

logger = getLogger()
logger.info('Starting')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.debug('This is a debug message')

posted @ 2024-07-09 10:20  DreamDZhu  阅读(7)  评论(0编辑  收藏  举报