2

私は次のloggerクラスを持っています(logger.pyとして):

import logging, logging.handlers
import config

log = logging.getLogger('myLog')

def start():
    "Function sets up the logging environment."
    log.setLevel(logging.DEBUG)
    formatter = logging.Formatter(fmt='%(asctime)s [%(levelname)s] %(message)s', datefmt='%d-%m-%y %H:%M:%S')

    if config.logfile_enable:
        filehandler = logging.handlers.RotatingFileHandler(config.logfile_name, maxBytes=config.logfile_maxsize,backupCount=config.logfile_backupCount)
        filehandler.setLevel(logging.DEBUG)
        filehandler.setFormatter(formatter)
        log.addHandler(filehandler)

    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    console.setFormatter(logging.Formatter('[%(levelname)s] %(message)s')) # nicer format for console
    log.addHandler(console)

    # Levels are: debug, info, warning, error, critical.
    log.debug("Started logging to %s [maxBytes: %d, backupCount: %d]" % (config.logfile_name, config.logfile_maxsize, config.logfile_backupCount))

def stop():
    "Function closes and cleans up the logging environment."
    logging.shutdown()

ロギングの場合、一度起動してから、任意のプロジェクトファイルlogger.start()にインポートします。from logger import logそれから私は必要なときに使うだけlog.debug()ですlog.error()。スクリプトのどこからでも正常に機能しますが(さまざまなクラス、関数、ファイル)、マルチプロセッシングクラスを介して実行されるさまざまなプロセスでは機能しません。

次のエラーが発生します:No handlers could be found for logger "myLog"

私に何ができる?

4

1 に答える 1

9

Pythonドキュメントから:logging to a single file from multiple processes is not supported, because there is no standard way to serialize access to a single file across multiple processes in Python.

参照:http ://docs.python.org/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes

ところで、この状況で私がしているのは、TCP経由でログに記録する分散ログアグリゲーターであるScribeを使用することです。これにより、すべてのプロセスだけでなく、必要なすべてのサーバーを同じ場所に記録できます。

このプロジェクトを参照してください:http://pypi.python.org/pypi/ScribeHandler

于 2012-05-19T13:02:28.750 に答える