python-coloredlogs icon indicating copy to clipboard operation
python-coloredlogs copied to clipboard

How do I control logging levels of other handles?

Open clsteam opened this issue 2 years ago • 0 comments

As stated in my title question, how should I change logging levels of other handles, like logging.FileHandler below.

    logging_format = '%(asctime)s %(hostname)s %(username)s %(programname)s %(name)s[%(process)d] %(levelname)s %(message)s'
    logging_level = "DEBUG"
    logging_file = "test.log"

    verboselogs.install()
    coloredlogs.install(level=logging_level, stream=sys.stdout, fmt=logging_format, milliseconds=True)
    # main logger
    logger = logging.getLogger("main")
    # add file handler
    logging_file_handler = logging.FileHandler(logging_file)
    logging_file_handler.setFormatter(coloredlogs.ColoredFormatter('%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s'))
    logging_file_handler.setLevel("SPAM")
    logger.addHandler(logging_file_handler)
    logger.debug("show in console")
    logger.spam("show in logging file only?")

In the above code, logging_file_handler.setLevel("SPAM") doesn't seem to work, the logging is same between console and file.

clsteam avatar Mar 22 '23 02:03 clsteam