Skip to content

logging

zeus.utils.logging

Utilities for logging.

FileAndConsole

Like tee, but for Python prints.

Source code in zeus/utils/logging.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class FileAndConsole:
    """Like tee, but for Python prints."""

    def __init__(self, filepath: Path) -> None:
        """Initialize the object."""
        self.file = open(filepath, "w")
        self.stdout = sys.stdout

    def write(self, message):
        """Write message."""
        self.file.write(message)
        self.stdout.write(message)
        self.file.flush()
        self.stdout.flush()

    def flush(self):
        """Flush both log file and stdout."""
        self.file.flush()
        self.stdout.flush()

__init__

__init__(filepath)
Source code in zeus/utils/logging.py
12
13
14
15
def __init__(self, filepath: Path) -> None:
    """Initialize the object."""
    self.file = open(filepath, "w")
    self.stdout = sys.stdout

write

write(message)

Write message.

Source code in zeus/utils/logging.py
17
18
19
20
21
22
def write(self, message):
    """Write message."""
    self.file.write(message)
    self.stdout.write(message)
    self.file.flush()
    self.stdout.flush()

flush

flush()

Flush both log file and stdout.

Source code in zeus/utils/logging.py
24
25
26
27
def flush(self):
    """Flush both log file and stdout."""
    self.file.flush()
    self.stdout.flush()

get_logger

get_logger(name, level=logging.INFO, propagate=False)

Get a logger with the given name with some formatting configs.

Source code in zeus/utils/logging.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def get_logger(
    name: str,
    level: int = logging.INFO,
    propagate: bool = False,
) -> logging.Logger:
    """Get a logger with the given name with some formatting configs."""
    if name in logging.Logger.manager.loggerDict:
        return logging.getLogger(name)

    logger = logging.getLogger(name)
    logger.propagate = propagate
    logger.setLevel(os.environ.get("ZEUS_LOG_LEVEL", level))
    formatter = logging.Formatter(
        "[%(asctime)s] [%(name)s](%(filename)s:%(lineno)d) %(message)s"
    )
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger