2016-02-20 11 views
6

ho il seguente ColoeredFormatter nel mio modulo.registrazione python dictConfig formattatore personalizzato non viene chiamato

Ma il formatter non viene chiamato. Mi aspetto "ciao" sulla console, ma ottengo "Questa è un'informazione del root logger".

Ho già eliminato tutti i file .pyc, ma non è stato d'aiuto.

MyModule/__ init__.py

from MyModule.ColoredFormatter import ColoredFormatter 

__all__ = ('ColoredFormatter') 

MyModule/ColoredFormatter.py

import logging 

class ColoredFormatter(logging.Formatter): 
    def __init__(self, default): 
     self.default = default 

    def format(self, record): 
     print("hi") 
     record.msg = "hi" 
     return self.default.format(record) 

mio script

import logging, logging.config, yaml 

conf=""" 
logging: 
    version: 1 
    disable_existing_loggers: true 
    root: 
    level: !!python/name:logging.NOTSET 
    handlers: [console] 
    handlers: 
    console: 
     class: logging.StreamHandler 
     stream: ext://sys.stdout 
     formatter: myFormatter 
     level: !!python/name:logging.NOTSET 

    formatters: 
    myFormatter: 
     class: !!python/name:MyModule.ColoredFormatter 
""" 
dict = yaml.parse(conf) 
logging.config.dictConfig(dict["logging"]) 

logging.info("This is an info of the root logger") 

risposta

7

nella sezione formattatore ho dovuto sostituire class da ()

import logging, logging.config, yaml 

conf=""" 
logging: 
    version: 1 
    disable_existing_loggers: true 
    root: 
    level: !!python/name:logging.NOTSET 
    handlers: [console] 
    handlers: 
    console: 
     class: logging.StreamHandler 
     stream: ext://sys.stdout 
     formatter: myFormatter 
     level: !!python/name:logging.NOTSET 

    formatters: 
    myFormatter: 
     '()': MyModule.ColoredFormatter 
""" 
dict = yaml.parse(conf) 
logging.config.dictConfig(dict["logging"]) 

logging.info("This is an info of the root logger") 
+1

Potete per favore elaborare come si è arrivati ​​a queste informazioni? Non ho potuto trovarlo ovunque – Hagai

+0

@Hagai alcune informazioni possono essere trovate nella [documentazione di registrazione Python] (https://docs.python.org/3/library/logging.config.html#dictionary-schema-details). – Evert

+0

Grazie a @Evert, è stato un po 'difficile da trovare, quindi per le prossime generazioni: puoi trovarlo nella sezione "oggetti definiti dall'utente" https://docs.python.org/2/library/logging.config .html # definiti dall'utente-oggetti – Hagai

Problemi correlati