I'm just starting to learn how to use structlog
and I'm having a difficult time trying to figure out how to turn off colored logging when it writes to files. Essentially what I did was take my old code that I had used with the standard logging module and converted it to work with structlog - this is what I came up with:
formatter = logging.Formatter(
fmt=LOGGER_OUTPUT_FORMAT,
datefmt=LOGGER_DATETIME_FORMAT,
)
handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(formatter)
logger.addHandler(handler)
# create a file handler
formatter = logging.Formatter(
fmt=LOGGER_OUTPUT_FORMAT,
datefmt=LOGGER_DATETIME_FORMAT,
)
handler = RotatingFileHandler(
filename=LOGFILE_PATH,
maxBytes=4000000,
backupCount=20,
)
handler.setLevel(level)
handler.setFormatter(formatter)
logger.addHandler(handler)
structlog.configure(logger_factory=LoggerFactory())
logger = structlog.getLogger('output_logger')
What I can't figure out how to do is where to insert lines to change the formatter such that it doesn't use colored output when saving the logging output to a file. I'm guessing I can uninstall colorama
, but that seems like it would be defeating the purpose of actually learning how to use structlog. I've dug through the structlog manual but it's just not making sense to me, and everything I seem to try throws errors, so any guidance would be much appreciated!