forked from MissouriMRR/IARC-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
40 lines (31 loc) · 1.36 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
"""Logging configuration and functions"""
import logging
from datetime import datetime
from logging import Formatter, FileHandler, StreamHandler
from logging.handlers import QueueHandler, QueueListener
from colorlog import ColoredFormatter
LOG_FILE: str = f"logs/{datetime.now()}.log"
COLOR_LOG_FORMAT: str = "%(log_color)s%(levelname)s | %(asctime)s @ %(processName)s:%(funcName)s > %(message)s%(reset)s"
LOG_FORMAT: str = "%(levelname)s | %(asctime)s @ %(processName)s:%(funcName)s > %(message)s"
LOG_LEVEL = logging.DEBUG
def init_logger(queue):
"""
Creates a QueueListener that will process all log messages throughout
the application
"""
console_formatter: Formatter = ColoredFormatter(COLOR_LOG_FORMAT)
file_formatter: Formatter = logging.Formatter(LOG_FORMAT)
file: FileHandler = logging.FileHandler(LOG_FILE, "a")
file.setFormatter(file_formatter)
console: StreamHandler = logging.StreamHandler()
console.setFormatter(console_formatter)
return QueueListener(queue, file, console)
def worker_configurer(queue):
"""
When this is run, it configures the logger of this process to submit
logs to the logging process (QueueListener)
"""
queue_handler: QueueHandler = QueueHandler(queue) # Just the one handler needed
root = logging.getLogger()
root.addHandler(queue_handler)
root.setLevel(LOG_LEVEL)