aboutsummaryrefslogtreecommitdiff
path: root/plip/basic/logger.py
blob: f15650ea9e7b3d61cfb3d0f12292e2703fbf5054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import inspect
import logging


def get_logger():
    """
    Configures a base logger and returns a module-specific sub-logger of the calling module.
    """
    frame = inspect.stack()[1]
    module_name = inspect.getmodule(frame[0]).__name__
    if module_name != "__main__":
        logger = logging.getLogger(module_name)
        if not logger.parent.handlers:
            ch = logging.StreamHandler()
            formatter = logging.Formatter(
                "%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(name)s: %(message)s"
            )
            ch.setFormatter(formatter)
            logger.parent.addHandler(ch)
    else:
        logger = logging.getLogger("plip")

    return logger