I'm using the python logging module with an YAML config file. In there I define 2 formatters. How can I access these formatters dynamically? I want to overwrite the standard formatter attached to one of my handlers dynamically, if a certain event occurs.
As requested, a small example:
my logging config:
version: 1
disable_existing_loggers: False
formatters:
console:
class: colorlog.ColoredFormatter
format: "%(log_color)s[%(asctime)s] [%(levelname)-8s] --- %(message)s (%(filename)s:%(lineno)s)"
datefmt: "%Y-%m-%d %H:%M:%S"
console_user:
class: colorlog.ColoredFormatter
format: "%(log_color)s[%(levelname)-8s] --- %(message)s"
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: console
stream: ext://sys.stdout
root:
level: DEBUG
handlers: [console]
in my script I have something like:
import logging
logging.config.dictConfig(logging_config)
logger = logging.getLogger()
Now I'd like to do something like
logger.handlers[0].setFormatter('console_user')
however, there seems to be no reference to any formatters not associated with a handler.