-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger.py
69 lines (54 loc) · 2.2 KB
/
logger.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
"""
This module provides utilities for logging messages with custom formatting.
It includes a custom logging formatter `CortexVortexLogger`
that adds a bullet point prefix to log messages based on their severity level.
It also provides a function `init_logger()` to initialize the logger
with a stream handler and set the logging level to INFO.
Example usage:
import logging_util
logging_util.init_logger()
logging.info("This is an information message.")
logging.debug("This is a debug message.")
logging.error("This is an error message.")
"""
import logging
import sys
class CortexVortexLogger(logging.Formatter):
"""
Custom logging formatter that adds a bullet point prefix based on log level.
The bullet points indicate the severity of the log message:
- [+] for INFO level messages
- [DEBUG]: for DEBUG level messages
- [!] for ERROR level messages
- [~] for WARNNING level messages
- [X] for other log messages
:param logging.Formatter: Parent class for formatting log messages.
"""
def __init__(self) -> None:
super().__init__("%(bullet)s %(asctime)s.%(msecs)03d %(message)s", "%Y-%m-%d %H:%M:%S")
def format(self, record):
"""
Formats the log record with a bullet point prefix based on the log level.
:param record: The log record to be formatted.
:return: The formatted log message.
"""
if record.levelno == logging.INFO:
record.bullet = '[+]'
elif record.levelno == logging.DEBUG:
record.bullet = '[DEBUG]:'
elif record.levelno == logging.ERROR:
record.bullet = '[!]'
elif record.levelno == logging.WARNING:
record.bullet = '[~]'
else:
record.bullet = '[X]'
return logging.Formatter.format(self, record)
def init_logger():
"""
Initializes the logger with a stream handler and sets the logging level to INFO.
:return: None
"""
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(CortexVortexLogger())
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.INFO)