-
-
Notifications
You must be signed in to change notification settings - Fork 692
Add optional arg encoding = None for ignite.utils.setup_logger #3239
Copy link
Copy link
Closed
Labels
Description
🚀 Feature
Add optional arg encoding = None for ignite.utils.setup_logger
Motivation
I encountered garbled text when loggering messages that contains CJK characters using logger.info(). For example,
from ignite.utils import setup_logger
logger = setup_logger(name="theName", filepath='filename.log')
logger.info('你好') # garbled text in the written .log fileIn the .log file, garbled text is printed.
�й���ʱ��.
Solution
This can be addressed by simply passing encoding = "utf-8" to the filehandler
Line 268 in f431e60
| fh = logging.FileHandler(filepath) |
def setup_logger(
name: Optional[str] = "ignite",
level: int = logging.INFO,
stream: Optional[TextIO] = None,
format: str = "%(asctime)s %(name)s %(levelname)s: %(message)s",
filepath: Optional[str] = None,
encoding: Optional[str] = None, # add this optional arg
distributed_rank: Optional[int] = None,
reset: bool = False,
) -> logging.Logger:
if filepath is not None:
fh = logging.FileHandler(filepath, encoding = encoding) # pass encoding to the file handler
fh.setLevel(level)
fh.setFormatter(formatter)
logger.addHandler(fh)Looking forward to a discussion about this before I create a PR.
Reactions are currently unavailable