I agree that you should use the registration module, but you cannot do it correctly with just a format string, as shown in some other answers, since they do not affect the situation when you register a message containing a comma.
If you need a solution that will correctly remove any special characters in the message (or other fields, I suppose), you will need to write your own formatter and install it.
logger = logging.getLogger() formatter = MyCsvFormatter() handler = logging.FileHandler(filename, "w") handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(level)
You will obviously have to implement the MyCsvFormatter class, which should inherit from logging.Formatter and override the format () method
class MyCsvFormatter(logging.Formatter): def __init__(self): fmt = "%(levelname)s,%(message)s"
Note. I already did something similar before, but have not tested this sample code
Regarding the actual screening of the message, here is one of the possible approaches: Python - write data in csv format as a string (not a file)
Will
source share