Last active
October 10, 2021 16:21
-
-
Save koyo922/df1a9d35f70f00277c68f1534341a181 to your computer and use it in GitHub Desktop.
logging snippet of Baidu, 参考百度编码规范 http://styleguide.baidu.com/style/python/index.html#%E7%BC%96%E7%A8%8B%E5%AE%9E%E8%B7%B5id9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# vim: tabstop=4 shiftwidth=4 expandtab number | |
################################################################################ | |
# | |
# Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved | |
# | |
################################################################################ | |
""" | |
log.py | |
# 参考百度编码规范 http://styleguide.baidu.com/style/python/index.html#%E7%BC%96%E7%A8%8B%E5%AE%9E%E8%B7%B5id9 | |
Authors: qianweishuo(qianweishuo@baidu.com) | |
Date: 2018/7/23 下午2:19 | |
""" | |
import os | |
import logging | |
import logging.handlers | |
class _BelowWarningFilter(logging.Filter): | |
def filter(self, record): | |
return record.levelno < logging.WARNING | |
# noinspection PyIncorrectDocstring | |
def init_log(log_path, level=logging.INFO, when="D", backup=365, to_console=True, show_logger_src=False, | |
fmt="%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)s", | |
datefmt="%m-%d %H:%M:%S"): | |
""" | |
init_log - initialize log module | |
Args: | |
log_path - Log file path prefix. | |
Log data will go to two files: log_path.log and log_path.log.wf | |
Any non-exist parent directories will be created automatically | |
level - msg above the level will be displayed in *.log(always WARN for *.log.wf, DEBUG for stdout) | |
DEBUG < INFO < WARNING < ERROR < CRITICAL | |
the default value is logging.INFO | |
when - how to split the log file by time interval | |
'S' : Seconds | |
'M' : Minutes | |
'H' : Hours | |
'D' : Days | |
'W' : Week day | |
default value: 'D' | |
format - format of the log | |
default format: | |
%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)s | |
> INFO: 12-09 18:02:42: log.py:40 * 139814749787872 HELLO WORLD | |
backup - how many backup file to keep | |
default value: 7 | |
Raises: | |
OSError: fail to create log directories | |
IOError: fail to open log file | |
""" | |
if show_logger_src: | |
fmt = fmt.replace('%(filename)s:%(lineno)d', '[%(name)s@%(pathname)s]%(filename)s:%(lineno)d') | |
formatter = logging.Formatter(fmt, datefmt) | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
del logger.handlers[:] | |
log_dir = os.path.dirname(log_path) | |
if not os.path.isdir(log_dir): | |
os.makedirs(log_dir) | |
handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log", | |
when=when, | |
backupCount=backup) | |
handler.setLevel(level) | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log.wf", | |
when=when, | |
backupCount=backup) | |
handler.setLevel(logging.WARNING) | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
if to_console: | |
import sys | |
handler_stdout = logging.StreamHandler(sys.stdout) | |
handler_stdout.setLevel(logging.DEBUG) | |
handler_stdout.addFilter(_BelowWarningFilter()) # 低于WARNING的打到 stdout | |
handler_stdout.setFormatter(formatter) | |
logger.addHandler(handler_stdout) | |
handler_stderr = logging.StreamHandler(sys.stderr) | |
handler_stderr.setLevel(logging.WARNING) # >= WARNING的打到 stderr | |
handler_stderr.setFormatter(formatter) | |
logger.addHandler(handler_stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import log | |
import logging | |
def main(): | |
log.init_log("./log/my_program") # 日志保存到./log/my_program.log和./log/my_program.log.wf,按天切割,保留365天 | |
logging.info("Hello World!!!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment