Created
July 20, 2016 09:34
-
-
Save miminus/09ff578bfabdee78a0447ecf6b577b93 to your computer and use it in GitHub Desktop.
python的通用log文件,满足大部分的日志记录
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 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: log.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/06/30 13:53:19 | |
""" | |
import os | |
import logging | |
import logging.handlers | |
def init_log(log_path, level=logging.INFO, when="D", backup=7, | |
format="%(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 | |
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 | |
""" | |
formatter = logging.Formatter(format, datefmt) | |
logger = logging.getLogger() | |
logger.setLevel(level) | |
dir = os.path.dirname(log_path) | |
if not os.path.isdir(dir): | |
os.makedirs(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) | |
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 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: test_log.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/06/30 14:03:01 | |
""" | |
import logging | |
import log | |
def main(): | |
log.init_log("./log/my_program") | |
logging.info("Hello world!!!") | |
logging.warn("warn starting!!") | |
logging.error('error...') | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment