forked from Election-Tech-Initiative/electionguard-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.py
179 lines (136 loc) · 4.92 KB
/
logs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import inspect
import logging
import os.path
import sys
from typing import Any, List, Tuple
from logging.handlers import RotatingFileHandler
from .singleton import Singleton
FORMAT = "[%(process)d:%(asctime)s]:%(levelname)s:%(message)s"
class ElectionGuardLog(Singleton):
"""
A singleton log for the library
"""
__logger: logging.Logger
def __init__(self) -> None:
super().__init__()
self.__logger = logging.getLogger("electionguard")
self.__logger.addHandler(get_stream_handler())
@staticmethod
def __get_call_info() -> Tuple[str, str, int]:
stack = inspect.stack()
# stack[0]: __get_call_info
# stack[1]: __formatted_message
# stack[2]: (log method, e.g. "warn")
# stack[3]: Singleton
# stack[4]: caller <-- we want this
filename = stack[4][1]
line = stack[4][2]
funcname = stack[4][3]
return filename, funcname, line
def __formatted_message(self, message: str) -> str:
filename, funcname, line = self.__get_call_info()
message = f"{os.path.basename(filename)}.{funcname}:#L{line}: {message}"
return message
def add_handler(self, handler: logging.Handler) -> None:
"""
Adds a logger handler
"""
self.__logger.addHandler(handler)
def remove_handler(self, handler: logging.Handler) -> None:
"""
Removes a logger handler
"""
self.__logger.removeHandler(handler)
def handlers(self) -> List[logging.Handler]:
"""
Returns all logging handlers
"""
return self.__logger.handlers
def debug(self, message: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a debug message
"""
self.__logger.debug(self.__formatted_message(message), *args, **kwargs)
def info(self, message: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a info message
"""
self.__logger.info(self.__formatted_message(message), *args, **kwargs)
def warn(self, message: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a warning message
"""
self.__logger.warning(self.__formatted_message(message), *args, **kwargs)
def error(self, message: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a error message
"""
self.__logger.error(self.__formatted_message(message), *args, **kwargs)
def critical(self, message: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a critical message
"""
self.__logger.critical(self.__formatted_message(message), *args, **kwargs)
def get_stream_handler() -> logging.StreamHandler:
"""
Get a Stream Handler, sends only warnings and errors to stdout.
"""
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(logging.Formatter(FORMAT))
return stream_handler
def get_file_handler() -> logging.FileHandler:
"""
Get a File System Handler, sends verbose logging to a file, `electionguard.log`.
When that file gets too large, the logs will rotate, creating files with names
like `electionguard.log.1`.
"""
# TODO: add file compression, save a bunch of space.
# https://medium.com/@rahulraghu94/overriding-pythons-timedrotatingfilehandler-to-compress-your-log-files-iot-c766a4ace240 # pylint: disable=line-too-long
file_handler = RotatingFileHandler(
"electionguard.log", "a", maxBytes=10_000_000, backupCount=10
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(FORMAT))
return file_handler
LOG = ElectionGuardLog()
def log_add_handler(handler: logging.Handler) -> None:
"""
Adds a handler to the logger
"""
LOG.add_handler(handler)
def log_remove_handler(handler: logging.Handler) -> None:
"""
Removes a handler from the logger
"""
LOG.remove_handler(handler)
def log_handlers() -> List[logging.Handler]:
"""
Returns all logger handlers
"""
return LOG.handlers()
def log_debug(msg: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a debug message to the console and the file log.
"""
LOG.debug(msg, *args, **kwargs)
def log_info(msg: str, *args: Any, **kwargs: Any) -> None:
"""
Logs an information message to the console and the file log.
"""
LOG.info(msg, *args, **kwargs)
def log_warning(msg: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a warning message to the console and the file log.
"""
LOG.warn(msg, *args, **kwargs)
def log_error(msg: str, *args: Any, **kwargs: Any) -> None:
"""
Logs an error message to the console and the file log.
"""
LOG.error(msg, *args, **kwargs)
def log_critical(msg: str, *args: Any, **kwargs: Any) -> None:
"""
Logs a critical message to the console and the file log.
"""
LOG.critical(msg, *args, **kwargs)