-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
61 lines (45 loc) · 1.68 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
"""Generic logger module that wraps around Python's built-in logger.
"""
from pathlib import Path
import sys
import logging
from logging.handlers import TimedRotatingFileHandler
# Use a config file for these, in larger projects:
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
FORMATTER = logging.Formatter(LOG_FORMAT)
LOG_DIR = Path("logs")
ACTIVE_LOG_NAME = "logger.log"
def get_log_path():
if LOG_DIR.exists():
return Path(LOG_DIR, ACTIVE_LOG_NAME)
raise FileNotFoundError(2, "Log output directory could not be found!")
def get_console_handler() -> logging.StreamHandler:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler
def get_file_handler() -> TimedRotatingFileHandler:
file_handler = TimedRotatingFileHandler(get_log_path(), when="midnight")
file_handler.setFormatter(FORMATTER)
return file_handler
class NullHandler(logging.Handler):
"""Silent handler
Add this in `get_logger()` to silence the logger.
"""
def emit(self, record):
pass
def get_logger(logger_name: str) -> logging.Logger:
logger = logging.getLogger(logger_name)
logger.addHandler(get_console_handler())
file_handler = get_file_handler()
logger.addHandler(file_handler)
try:
logger.setLevel(logging.DEBUG)
logger.debug(f"Fetching logger for {logger_name}")
except PermissionError:
logger.removeHandler(file_handler)
file_handler.doRollover()
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG) # Modify this on production!
return logger
def shutdown_logger() -> None:
logging.shutdown()