File size: 1,169 Bytes
d5d7329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import sys
from logging import DEBUG, INFO, StreamHandler, basicConfig, captureWarnings, getLogger
from pathlib import Path

from rich.logging import RichHandler

LOGGER_INIT = False


def init_logger() -> None:
    global LOGGER_INIT
    if LOGGER_INIT:
        return

    IS_TEST = "test" in Path.cwd().stem
    package_name = sys.modules[__name__].__package__
    basicConfig(
        level=INFO,
        format="%(asctime)s %(message)s",
        datefmt="[%X]",
        handlers=[
            StreamHandler() if is_notebook() else RichHandler(),
            # FileHandler(f"{package_name}.log"),
        ],
    )
    if IS_TEST:
        getLogger(package_name).setLevel(DEBUG)
    captureWarnings(True)
    LOGGER_INIT = True


def is_notebook():
    try:
        from IPython import get_ipython

        if "IPKernelApp" not in get_ipython().config:  # pragma: no cover
            raise ImportError("console")
            return False
        if "VSCODE_PID" in os.environ:  # pragma: no cover
            raise ImportError("vscode")
            return False
    except Exception:
        return False
    else:  # pragma: no cover
        return True