Passed
Pull Request — main (#746)
by Yunguan
01:23
created

deepreg.log   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 17
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A get() 0 31 1
1
"""Module for logger."""
2
import logging
3
import os
4
import sys
5
6
7
def get(name: str) -> logging.Logger:
8
    """
9
    Configure the logger with formatter and handlers.
10
11
    The log level depends on the environment variable `DEEPREG_LOG_LEVEL`.
12
13
    - 0: NOTSET, will be set to DEBUG
14
    - 1: DEBUG
15
    - 2: INFO (default)
16
    - 3: WARNING
17
    - 4: ERROR
18
    - 5: CRITICAL
19
20
    https://docs.python.org/3/library/logging.html#levels
21
22
    :param name: module name.
23
    :return: configured logger.
24
    """
25
    logger = logging.getLogger(name=name)
26
    logger.propagate = False
27
    log_level = os.environ.get("DEEPREG_LOG_LEVEL", "2")
28
    log_level_int = max(int(log_level) * 10, 10)
29
    logger.setLevel(log_level_int)
30
    formatter = logging.Formatter(
31
        fmt="%(asctime)s | %(levelname)-8s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
32
    )
33
    stdout_handler = logging.StreamHandler(stream=sys.stdout)
34
    stdout_handler.setFormatter(formatter)
35
    stdout_handler.setLevel(log_level_int)
36
    logger.addHandler(stdout_handler)
37
    return logger
38