twisted的log系統的級別level

首先你須要寫一個log觀察者類,而後給這觀察者一個級別來過濾你想要的log import logging from twisted.python import logpython

class LevelFileLogObserver(log.FileLogObserver):api

def __init__(self, f, level=logging.INFO):
    log.FileLogObserver.__init__(self, f)
    self.logLevel = level

def emit(self, eventDict):
    if eventDict['isError']:
        level = logging.ERROR
    elif 'level' in eventDict:
        level = eventDict['level']
    else:
        level = logging.INFO
    if level >= self.logLevel:
        log.FileLogObserver.emit(self, eventDict)

'''then you have to register it:''' from twisted.python import logfilethis

f = logfile.LogFile("someFile.log", '/some/path/', rotateLength=1000,maxRotatedFiles=100) logger = LevelFileLogObserver(f, logging.DEBUG) twisted.python.log.addObserver(logger.emit)code

If you use twistd you can pass it through --logger option:server

mylogger.py

import LevelFileLogObserver

from twisted.python import logfileit

f = logfile.LogFile("someFile.log", '/some/path/', rotateLength=1000, maxRotatedFiles=100) flobserver = LevelFileLogObserver(f) observer = flobserver.emitio

twistd invocation

twistd --logger=mylogger.observer Now you can use the new api you defined:event

log.msg('the level of this message is INFO') log.msg('the level of this message is INFO', level=logging.INFO) log.msg('the level of this message is DEBUG', level=logging.DEBUG) log.msg('the level of this message is ERROR', level=logging.ERROR) log.msg('the level of this message is ERROR', level=logging.WARNING) log.msg('the level of this message is ERROR', level=logging.CRITICAL) log.err('the level of this message is ERROR')class

級別共有五個,優先級關係是 '''level: DEBUG < INFO < WARNING < ERROR < CRITICAL''',若是級別爲CRITICAL 那麼小於它的級別的錯誤都不會打印出來import

相關文章
相關標籤/搜索