bmccool / pyMcCool

Reusable Python utilities
MIT License
0 stars 0 forks source link

Can't log unicode characters #30

Closed bmccool closed 11 months ago

bmccool commented 11 months ago

Attempting to log a unicode character gives error:

Message: ' (3, 2): ┃}' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\brend\AppData\Local\Programs\Python\Python312\Lib\logging__init__.py", line 1163, in emit stream.write(msg + self.terminator) File "C:\Users\brend\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'charmap' codec can't encode character '\u2503' in position 88: character maps to

This can be fixed by adding utf-8 encoding to the file handlers. But unicode shouild be added as a unit test as well.

bmccool commented 11 months ago
    # Rotating file handler for debug messages
    debug_file_handler = RotatingFileHandler(
        filename=f'Logs/Debug/{info.app_name}_debug.log',
        maxBytes=1000000,
        backupCount=100,
        encoding='utf-8')
    debug_file_handler.setLevel(logging.DEBUG)
    debug_file_handler.setFormatter(formatter)

    # Rotating file handler for info messages
    info_file_handler = RotatingFileHandler(
        filename=f'Logs/Info/{info.app_name}_info.log',
        maxBytes=1000000,
        backupCount=100,
        encoding='utf-8')
    info_file_handler.setLevel(logging.INFO)
    info_file_handler.setFormatter(formatter)