1

StreamHandler と FileHandler が追加されたルート ロガーを取得したとします。子ロガーはいずれもルート ロガーを継承します。たとえば、ルートロガー用に構成されたものとは異なる形式でメッセージを保存するなど、子ロガーを修正できる方法はありますか。

[<logging.StreamHandler instance at 0x0884B850>, <logging.FileHandler instance at 0x0C7A84E0>
4

1 に答える 1

4

You can attach a new handler on a child logger, that has it's own formatter.

You should be able to create a handler that writes to the same file as the root handler, as the flush operation uses a lock.

handler = logging.FileHandler(filename)
formatter = logging.Formatter(newformat)
handler.setFormatter(formatter)

log = logging.getLogger(yourloggername)
log.setHandler(handler)
log.propagate = False   # don't send messages to the root, handle it all here
于 2013-01-17T22:11:01.827 に答える