Skip to content

Add optional arg encoding = None for ignite.utils.setup_logger #3239

@sjiang95

Description

@sjiang95

🚀 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 file

In the .log file, garbled text is printed.

�й���׼ʱ��.

Solution

This can be addressed by simply passing encoding = "utf-8" to the filehandler

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions