-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.py
74 lines (56 loc) · 1.98 KB
/
log.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
import os
from rich.logging import RichHandler
class LoggerWriter:
"""Taken from https://docs.python.org/3.11/howto/logging-cookbook.html"""
def __init__(self, logger: logging.Logger, level: int) -> None:
self.logger = logger
self.level = level
def write(self, message: str) -> None:
if message != "\n": # avoid printing bare newlines, if you like
self.logger.log(self.level, message)
def flush(self) -> None:
# doesn't actually do anything, but might be expected of a file-like
# object - so optional depending on your situation
pass
def close(self) -> None:
# doesn't actually do anything, but might be expected of a file-like
# object - so optional depending on your situation. You might want
# to set a flag so that later calls to write raise an exception
pass
class LogFilter(logging.Filter):
"""
Print all messages that do not include "dealloc" (GPU warning message).
"""
def filter(self, record: logging.LogRecord):
if "dealloc" in str(getattr(record, "msg")):
return False
return True
def __repr__(self):
return "LogFilter"
def create_logger(name: str) -> logging.Logger:
"""Create a `Logger` to be used in different modules
Parameters:
-----------
name: str
The name of the logger. Will usually be passed in from the module as `__name__`.
Returns:
--------
log: logging.Logger
The `Logger` object that will be used to create log statements in the terminal.
"""
if (log_level := os.environ.get("LOGLEVEL")) is None:
log_level = "INFO"
FORMAT = "%(message)s"
rh = RichHandler()
rh.addFilter(LogFilter())
handlers = [rh]
logging.basicConfig(
level=log_level,
format=FORMAT,
datefmt="[%m/%d/%Y %I:%M:%S %p]",
handlers=handlers,
)
log = logging.getLogger(name)
log.setLevel(log_level)
return log