【Python】logging 学习

目录

logging

logging.config


logging

基础logging,在运行脚本中打印信息

import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

logger.info('This is a logger info')
logger.debug('Debugging')
logger.warning('Warning exists')
logger.info('Finish')
logging.info('This is a logging info')
logging.error('Error', exc_info=True)



####
# 2023-01-03 11:37:04,802 - __main__ - INFO - This is a logger info
# 2023-01-03 11:37:04,803 - __main__ - WARNING - Warning exists
# 2023-01-03 11:37:04,803 - __main__ - INFO - Finish
# 2023-01-03 11:37:04,803 - root - INFO - This is a logging info
# 2023-01-03 11:37:04,803 - root - ERROR - Error
# NoneType: None

logging.config

将运行的日志存入'result.log'文件中

import logging.config

logger = logging.getLogger('main')
logger.setLevel(level=logging.DEBUG)

# Handler
# 将运行的日志存入'result.log'文件中
handler = logging.FileHandler('result.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info('Main Info')
logger.debug('Main Debug')
logger.error('Main Error', exc_info=True)

result.log中内容如下,

2023-01-03 11:36:20,624 - main - INFO - Main Info
2023-01-03 11:36:20,624 - main - ERROR - Main Error
NoneType: None

正式使用时

-| ./logs/
       -| config.ini
       -| global_log.py
-| ./utils/
       -| main.py

config.ini文件

[loggers]
keys = root,mqlog

[handlers]
keys = rotatingFileHandler,errorHandler

[formatters]
keys = simpleFmt

[logger_root]
level = INFO
handlers=rotatingFileHandler,errorHandler
;,streamHandler
[logger_mqlog]
level = INFO
;,streamHandler
handlers = rotatingFileHandler,errorHandler
qualname = mqlog
;propagate=0

[handler_rotatingFileHandler]
class = handlers.TimedRotatingFileHandler
level = INFO
formatter = simpleFmt
;args = ('/export/Logs/mq_report.log', "midnight", 1, 6,'utf-8')
;mq的正常日志
args = ('Logs/mq_report.log', "midnight", 1, 6,'utf-8')


[handler_errorHandler]
class = handlers.TimedRotatingFileHandler
level = ERROR
formatter = simpleFmt
;args = ('/export/Logs/mq_error.log', "midnight", 1, 6,'utf-8')
;mq的报错日志
args = ('Logs/mq_error.log', "midnight", 1, 6,'utf-8')

[formatter_simpleFmt]
;format=%(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s
format = %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
datefmt = '%a, %d %b %Y %H:%M:%S'


;[handler_streamHandler]
;level = INFO
;class = StreamHandler
;formatter = simpleFmt
;args = (sys.stdout,)

 global_log.py

import logging
import logging.config
import os

def get_logger(name='mqlog'):
    conf_log = "config.ini"
    conf_log = os.path.join(os.path.dirname(os.path.abspath(__file__)), conf_log)
    logging.config.fileConfig(conf_log)
    print("init loggers")
    return logging.getLogger(name)

loggers = get_logger(__name__)

main.py调用日志文件

from logs.global_log import loggers

# 正常打印日志
loggers.info("5. {} copy detection result: {}".format(q_vid, query_res))

# 报错日志
loggers.error("delete data file failed, because of {}.".format(delete_status[1]), exc_info=True)

你可能感兴趣的:(编程语言,python)