Last active
July 12, 2016 04:32
-
-
Save weaming/15502d8e779bd0d930af to your computer and use it in GitHub Desktop.
python 的 logging 模块
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
# coding:utf-8 | |
""" | |
CRITICAL = 50 | |
FATAL = CRITICAL | |
ERROR = 40 | |
WARNING = 30 | |
WARN = WARNING | |
INFO = 20 | |
DEBUG = 10 | |
NOTSET = 0 | |
""" | |
#-------------------------------------------------- | |
# 日志格式 | |
#-------------------------------------------------- | |
# %(asctime)s 年-月-日 时-分-秒,毫秒 2013-04-26 20:10:43,745 | |
# %(filename)s 文件名,不含目录 | |
# %(pathname)s 目录名,完整路径 | |
# %(funcName)s 函数名 | |
# %(levelname)s 级别名 | |
# %(lineno)d 行号 | |
# %(module)s 模块名 | |
# %(message)s 消息体 | |
# %(name)s 日志模块名 | |
# %(process)d 进程id | |
# %(processName)s 进程名 | |
# %(thread)d 线程id | |
# %(threadName)s 线程名 | |
import os, shutil | |
import logging, logging.handlers, logging.config | |
LOG_FILE_NAME = 'rpac.log' | |
_here = os.path.dirname(__file__) | |
LOG_DIR = os.path.join(_here, '../log') | |
log_path = os.path.join(LOG_DIR, LOG_FILE_NAME) | |
def check_path(file_path, mode='w', encoding='utf-8', owner=None): | |
if not os.path.exists(file_path): | |
__log_path = os.path.dirname(file_path) | |
if not os.path.exists(__log_path): | |
os.makedirs(__log_path) | |
open(file_path, 'a').close() | |
if owner: | |
shutil.chown(file_path, *owner) | |
def file_handler(file_path, mode='w', encoding='utf-8', owner=None): | |
# file_path must be absolute path | |
# eg: file_handler(file_path, 'w') | |
rv = logging.FileHandler(file_path, mode, encoding) | |
return rv | |
def rsize_handler(file_path, maxBytes=1024*1024, backupCount=10): | |
# file_path must be absolute path | |
rv = logging.handlers.RotatingFileHandler( | |
file_path, maxBytes=maxBytes, backupCount=backupCount) | |
return rv | |
def rtime_handler(file_path, when='h', interval=6, backupCount=4*7, encoding='utf-8', delay=False, utc=False): | |
# file_path must be absolute path | |
rv = logging.handlers.TimedRotatingFileHandler( | |
file_path, when, interval, backupCount, encoding, delay, utc) | |
return rv | |
def myLogger(filename=LOG_FILE_NAME, htype='', handler=None): | |
# 自定义handler 或 htype='size' or 'time' 或 不填 | |
# 对同一个文件的handler只能创建一次,否则会占用文件锁,导致读写错误 | |
file_path = os.path.join(LOG_DIR, filename) | |
logger = logging.getLogger('') | |
logger.setLevel(logging.DEBUG) | |
_console_handler = logging.StreamHandler() | |
_console_handler.setLevel(logging.INFO) | |
_fmt = logging.Formatter('%(name)-15s: %(levelname)-8s %(message)s') | |
_console_handler.setFormatter(_fmt) | |
logger.addHandler(_console_handler) | |
if handler: | |
file_handler = handler | |
elif htype: | |
if htype == 'size': | |
file_handler = rsize_handler(file_path) | |
elif htype == 'time': | |
file_handler = rtime_handler(file_path) | |
else: | |
file_handler = file_handler(file_path) | |
pass | |
FMT_STR = '%(asctime)s - %(name)s - %(levelname)s:\n %(message)s' | |
_fmt = logging.Formatter(FMT_STR) | |
file_handler.setFormatter(_fmt) | |
file_handler.setLevel(logging.DEBUG) | |
logger.addHandler(file_handler) | |
return logger | |
# Test | |
# handler1 = rsize_handler(log_path, maxBytes=500, backupCount=5) | |
handler2 = rtime_handler(log_path, when='s', interval=1) | |
# logger = myLogger(htype='size') | |
logger = myLogger(handler=handler2) | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warn('warn message') | |
logger.error('error message') | |
logger.critical('critical message') | |
for i in xrange(10000): | |
logger.info('%s%d'% ('#'*20, i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment