Skip to content

Logging

RuntimeFormatter(*args, **kwargs)

Bases: logging.Formatter

Auxiliary class for runtime formatting in the logger.

Source code in src/deeponto/utils/logging.py
26
27
28
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.start_time = time.time()

formatTime(record, datefmt=None)

Record relative runtime in hr:min:sec format。

Source code in src/deeponto/utils/logging.py
30
31
32
33
34
def formatTime(self, record, datefmt=None):
    """Record relative runtime in hr:min:sec format。"""
    duration = datetime.datetime.utcfromtimestamp(record.created - self.start_time)
    elapsed = duration.strftime("%H:%M:%S")
    return "{}".format(elapsed)

create_logger(model_name, saved_path)

Create logger for both console info and saved info.

The pre-existed log file will be cleared before writing into new messages.

Source code in src/deeponto/utils/logging.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def create_logger(model_name: str, saved_path: str):
    """Create logger for both console info and saved info.

    The pre-existed log file will be cleared before writing into new messages.
    """
    logger = logging.getLogger(model_name)
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler(f"{saved_path}/{model_name}.log", mode="w")  # "w" means clear the log file before writing
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)
    # create formatter and add it to the handlers
    formatter = RuntimeFormatter("[Time: %(asctime)s] - [PID: %(process)d] - [Model: %(name)s] \n%(message)s")
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)
    logger.propagate = False
    return logger

banner_message(message, sym='^')

Print a banner message surrounded by special symbols.

Source code in src/deeponto/utils/logging.py
61
62
63
64
65
66
67
68
69
70
def banner_message(message: str, sym="^"):
    """Print a banner message surrounded by special symbols."""
    print()
    message = message.upper()
    banner_len = len(message) + 4
    message = " " * ((banner_len - len(message)) // 2) + message
    message = message + " " * (banner_len - len(message))
    print(message)
    print(sym * banner_len)
    print()

Last update: February 1, 2023
Created: January 14, 2023
GitHub: @Lawhy   Personal Page: yuanhe.wiki