-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
31 lines (22 loc) · 777 Bytes
/
main.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
import sys
import logging
from contextlib import contextmanager
logging.basicConfig(stream=sys.stdout, level=logging.WARNING)
@contextmanager
def log_level(level, name):
logger = logging.getLogger(name)
old_level = logger.getEffectiveLevel()
logger.setLevel(level)
try:
yield logger
finally:
logger.setLevel(old_level)
with log_level(logging.DEBUG, "my_log") as logger:
logger.debug(f" * This is a message for {logger.name}!")
logging.debug("This will not print")
logger = logging.getLogger("my_log")
logger.debug("Debug will not print")
logger.error("Error will print")
with log_level(logging.DEBUG, "other-log") as logger:
logger.debug(f"This is a message for {logger.name}!")
logging.debug("This will not print")