-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.py
More file actions
42 lines (31 loc) · 1.23 KB
/
logging.py
File metadata and controls
42 lines (31 loc) · 1.23 KB
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
# encoding: utf-8
__all__ = ["setup_logging"]
import contextlib
import logging
import lifesaver
# The idea of having setup_logging as a context manager is from RoboDanny, by Danny:
# https://github.com/Rapptz/RoboDanny/blob/1013d990b2e8fb343389e5ec7ffeeda4a8c449da/launcher.py#L25
@contextlib.contextmanager
def setup_logging(config: lifesaver.bot.config.BotLoggingConfig):
"""A context manager that sets up logging according to a :class:`lifesaver.bot.config.BotLoggingConfig`.
Gracefully destroys handlers when exited.
"""
root_logger = None
try:
root_logger = logging.getLogger()
root_logger.setLevel(config.level)
formatter = logging.Formatter(config.format, config.time_format, style="{")
file_stream = logging.FileHandler(config.file, encoding="utf-8")
stream = logging.StreamHandler()
stream.setFormatter(formatter)
file_stream.setFormatter(formatter)
root_logger.addHandler(stream)
root_logger.addHandler(file_stream)
yield
finally:
if root_logger is None:
return
handlers = root_logger.handlers[:]
for handler in handlers:
handler.close()
root_logger.removeHandler(handler)