python tornado 框架使用 (1)

(1)日志系统代码

 

   common_logging.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.config
import os
from unipath import Path

logging.config.fileConfig(Path(Path(__file__).ancestor(1), './conf/logging.conf'), \
        defaults=None, disable_existing_loggers=True)

 

conf目录下的配置:

logging.conf

[loggers]
keys=root

[handlers]
keys=consoleHandler, fileHandler

[formatters]
keys=root

[logger_root]
#handlers=consoleHandler, fileHandler
handlers=fileHandler
propagate=True

[handler_consoleHandler]
class=StreamHandler
level=ERROR
#level=DEBUG
formatter=root
args=(sys.stdout,)

[handler_fileHandler]
#class=handlers.TimedRotatingFileHandler
class=handlers.WatchedFileHandler
#level=WARN
level=ERROR
interval=midnight
backupCount=5
formatter=root
#args=('./logs/my.log', 'd',1, 0, 'utf-8',False, False,)
args=('./logs/my.log', 'a', 'utf-8', )

[formatter_root]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
~                                                                 

 

ps.

class=handlers.WatchedFileHandler 由于tornado使用多进程,如果使用自动切割日志,跨天时日志会混乱,如果使用WatchedFileHandler,则只要定时拷贝日志,自己进行切割,就不会有问题。

 

你可能感兴趣的:(python常用)