Skip to content

logging

zeus.util.logging

Utilities for logging.

FileAndConsole

Like tee, but for Python prints.

Source code in zeus/util/logging.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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/util/logging.py
26
27
28
29
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/util/logging.py
31
32
33
34
35
36
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/util/logging.py
38
39
40
41
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/util/logging.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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