2

I have a json config file and to register the custom logger inside of it, i do the following

logging.config.dictConfig(config)  
myLogger = logging.getLogger("myCustomLogger") 

When I debug, I see that the first code line above calls getLogger on the logger object and registers a console handler with it. I need a handle to the same logger object and thus, I call getLogger again which I think registers the same handler again.

when I log, the same log entry gets written twice on console.

I don't see a way to get a handle of logger object from logging.config.dictConfig.

The strange thing that I observed while debugging is that when the break point goes past the second line and I check the number of handlers associated with myLogger, it is only 1 handler. This is how I check this on python console that comes with pydev eclipse plugin

myLogger.handlers[1]

It gives me an error saying index doesn't exist.

 myLogger.handlers[0] works fine

Pretty brand new to python, so out of ideas at this point. Does anyone know how to get around this problem ?

EDIT:

Here is the dict config json file

{
    "version": 1,
    "disable_existing_loggers": true,

    "formatters": {
        "custom": {
            "format": "%(asctime)s %(levelname)s [%(applicationName)s,%(applicationState)s] %(message)s"
        }

    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "custom"
        }
    },

    "root": {
        "level": "INFO",
        "handlers": ["console"]
    },

    "loggers": {
        "myCustomLogger": {
            "level": "INFO",
            "handlers": ["console"],
            "propagate": "false"
        }
    }
}
12
  • Can you post your config? Commented May 25, 2017 at 17:45
  • i have shared it now. Commented May 25, 2017 at 17:47
  • There's only one "handler" in your config. Commented May 25, 2017 at 17:51
  • Can you post an example duplicated output as well? It might be both root and console are logging, you could add "formatter": "custom" to root and see the levels. Commented May 25, 2017 at 17:51
  • documentation says that every time you call getLogger, it adds a handler to that logger. So i am suspecting my 2 lines of code in the Question are adding the same handler twice to myCustomerLogger Commented May 25, 2017 at 17:55

1 Answer 1

1

Try this

{
    "version": 1,
    "disable_existing_loggers": true,

    "formatters": {
        "custom": {
            "format": "%(asctime)s %(levelname)s [%(applicationName)s,%(applicationState)s] %(message)s"
        }

    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "custom"
        }
    },

    "root": {
        "level": "INFO",
        # this is the only difference from your original config file
        "handlers": []
    },

    "loggers": {
        "myCustomLogger": {
            "level": "INFO",
            "handlers": ["console"],
            "propagate": "false"
        }
    }
}

As for your question: "So it root logger going to act no matter what?" If it's not printing to the screen, then it's effectively doing nothing. The logging message is still being generated, but your other handler will need it anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.