Repository: microsoft/qlib
File: qlib/finco/log.py (Python)

Code before:
"""
This module will base on Qlib's logger module and provides some interactive functions.
"""
import logging

from typing import Dict, List
from qlib.finco.utils import Singleton
from qlib.log import get_module_logger
from contextlib import contextmanager


class LogColors:
    """
    ANSI color codes for use in console output.
    """
    RED = '\033[91m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    BLUE = '\033[94m'
    MAGENTA = '\033[95m'
    CYAN = '\033[96m'
    BOLD = '\033[1m'
    END = '\033[0m'
    WHITE = '\033[97m'
    GRAY = '\033[90m'
    BLACK = '\033[30m'


@contextmanager
def formatting_log(logger, title="Info"):
    """
    a context manager, print liens before and after a function
    """
    length = {"Task": 120, "Info": 0}.get(title, 60)
    start = int((length - len(title)) / 2)
    pad = 1 if len(title) % 2 else 0
    color, bold = (LogColors.YELLOW, LogColors.BOLD) if title in ["Info", "Task"] else (LogColors.CYAN, '')
    logger.info("")
    logger.info(f"{color}{bold}{'-' * (start + pad)} {title} {'-' * start}{LogColors.END}")
    yield
    logger.info("")


class FinCoLog(Singleton):
    # TODO:
    # - config to file logger and save it into workspace
    def __init__(self) -> None:
        # self.logger = get_module_logger("interactive")
        self.logger = logging.Logger("interactive")

        handler = logging.StreamHandler()

Review comment (suggestion):
```suggestion
    BLACK = '\033[30m'
    # TODO: Provide  better interface to render text. (e.g. render(text, color.., style ..))
```

Code after:
"""
This module will base on Qlib's logger module and provides some interactive functions.
"""
import logging

from typing import Dict, List
from qlib.finco.utils import Singleton
from qlib.log import get_module_logger
from contextlib import contextmanager


class LogColors:
    """
    ANSI color codes for use in console output.
    """
    RED = "\033[91m"
    GREEN = "\033[92m"
    YELLOW = "\033[93m"
    BLUE = "\033[94m"
    MAGENTA = "\033[95m"
    CYAN = "\033[96m"
    BOLD = "\033[1m"
    END = "\033[0m"
    WHITE = "\033[97m"
    GRAY = "\033[90m"
    BLACK = "\033[30m"
    # TODO: Provide  better interface to render text. (e.g. render(text, color.., style ..))


@contextmanager
def formatting_log(logger, title="Info"):
    """
    a context manager, print liens before and after a function
    """
    length = {"Start": 120, "Task": 120, "Info": 60}.get(title, 60)
    color, bold = (LogColors.YELLOW, LogColors.BOLD) if title in ["Start", "Info", "Task"] else (LogColors.CYAN, "")
    logger.info("")
    logger.info(f"{color}{bold}{'-'} {title} {'-' * (length - len(title))}{LogColors.END}")
    yield
    logger.info("")


class FinCoLog(Singleton):
    # TODO:
    # - config to file logger and save it into workspace
    def __init__(self) -> None:
        # self.logger = get_module_logger("interactive")
        self.logger = logging.Logger("interactive")
        # TODO:  merge these with Qlib's default logger.
        #  We can do the same thing by changing the default log dict of Qlib.
        #  Reference: https://github.com/microsoft/qlib/blob/main/qlib/config.py#L155