<jupyter_start><jupyter_text>Logger> Setups `logger`: name, level, format etc.<jupyter_code># export
import functools
import logging
import sys

from fastcore.all import ifnone
from pytorch_lightning.utilities import rank_zero_only
from termcolor import colored
# export
class _ColorfulFormatter(logging.Formatter):
    def __init__(self, *args, **kwargs):
        self._root_name = kwargs.pop("root_name") + "."
        self._abbrev_name = kwargs.pop("abbrev_name", "")
        if len(self._abbrev_name):
            self._abbrev_name = self._abbrev_name + "."
        super(_ColorfulFormatter, self).__init__(*args, **kwargs)

    def formatMessage(self, record):
        record.name = record.name.replace(self._root_name, self._abbrev_name)
        log = super(_ColorfulFormatter, self).formatMessage(record)
        if record.levelno == logging.WARNING:
            prefix = colored("WARNING", "red", attrs=["blink"])
        elif record.levelno == logging.ERROR or record.levelno == logging.CRITICAL:
            prefix = colored("ERROR", "red", attrs=["blink", "underline"])
        else:
            return log
        return prefix + " " + log
# export
@functools.lru_cache()  # so that calling setup_logger multiple times won't add many handlers
def setup_logger(distributed_rank=0, *, color=True, name="gale", level=logging.DEBUG):
    """
    Initialize the gale logger and set its verbosity level to `level`.
    """
    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.propagate = False

    abbrev_name = name

    plain_formatter = logging.Formatter(
        "[%(asctime)s] %(name)s %(levelname)s: %(message)s", datefmt="%m/%d %H:%M:%S"
    )

    # stdout logging: master only
    if distributed_rank == 0:
        ch = logging.StreamHandler(stream=sys.stdout)
        ch.setLevel(logging.DEBUG)
        if color:
            formatter = _ColorfulFormatter(
                colored("[%(asctime)s %(name)s]: ", "green") + "%(message)s",
                datefmt="%m/%d %H:%M:%S",
                root_name=name,
                abbrev_name=str(abbrev_name),
            )
        else:
            formatter = plain_formatter
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    return logger
setup_logger()

logger = logging.getLogger("gale.utils.logger")

logger.info("This is a INFO message")
logger.debug("This is a DEBUG message")
logger.warning("This is a WARNING message")
logger.error("This is a ERROR message")
# export
@rank_zero_only
def log_main_process(logger, lvl, msg):
    """
    Logs `msg` using `logger` only on the main process
    """
    logger.log(lvl, msg)
log_main_process(logger, logging.INFO, "This logs only on the main process")
log_main_process(logger, logging.ERROR, "This logs only on the main process")
log_main_process(logger, logging.WARNING, "This logs only on the main process")<jupyter_output>[32m[04/30 16:16:52 gale.utils.logger]: [0mThis logs only on the main process
[4m[5m[31mERROR[0m [32m[04/30 16:16:52 gale.utils.logger]: [0mThis logs only on the main process
[5m[31mWARNING[0m [32m[04/30 16:16:52 gale.utils.logger]: [0mThis logs only on the main process<jupyter_text>Export -<jupyter_code># hide
notebook2script("00_utils.logger.ipynb")<jupyter_output>Converted 00_utils.logger.ipynb.
{'fraction_non_alphanumeric': 0.12860175917500757, 'fraction_numerical': 0.01668183196845617, 'mean_word_length': 3.5552486187845305, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '585149', 'license': 'Apache-2.0', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1045, 'n_words': 251, 'path': 'nbs/00_utils.logger.ipynb', 'repo_name': 'benihime91/litcv', 'size': '3297'}