I've got the following code running on each request of a wsgi (web2py) application:
import logging, logging.handlers
from logging import StreamHandler, Formatter
def get_configured_logger(name):
logger = logging.getLogger(name)
if (len(logger.handlers) == 0):
# This logger has no handlers, so we can assume it hasn't yet been configured (Django uses similiar trick)
# === Configure logger ===
# Create Formatted StreamHandler:
FORMAT = "%(process)s %(thread)s: %(message)s"
formatter = logging.Formatter(fmt=FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug('CONFIGURING LOGGER')
return logger
# Get app specific logger:
logger = get_configured_logger(request.application)
logger.debug("TEST")
It's meant to configure the logger once, with the formatted handler I want. It works, except that I'm getting double entries in my stdout:
81893 4329050112: CONFIGURING LOGGER
DEBUG:dummy:CONFIGURING LOGGER
81893 4329050112: TEST
DEBUG:dummy:TEST
How do I use my new formatted handler and get rid of/hide the default one?
.debug(..)
creates log entries at the debug level, while setting the logging level tells the handler to handle log entries at that level. – Vixenlogging.basicConfig
anywhere? If so, comment that out. – Misplace