-
Notifications
You must be signed in to change notification settings - Fork 48
/
log_configuration.py
65 lines (54 loc) · 2 KB
/
log_configuration.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
"""manages configuring logging for the app."""
import logging
import logging.handlers
import os
from logging import Logger, StreamHandler
from logging.handlers import RotatingFileHandler
from typing import Union
class LogConfiguration:
"""handle common logging options for the entire project."""
def __init__(self, folder: str, filename: str, debug: bool = False) -> None:
# create console handler
self._consoleLogger = logging.StreamHandler()
self._consoleLogger.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
if debug:
self._consoleLogger.setLevel(logging.DEBUG)
else:
self._consoleLogger.setLevel(logging.INFO)
# create a file handler
path = os.path.join(folder, 'logs', f'{filename}.log')
self._fileLogger = logging.handlers.RotatingFileHandler(path, maxBytes=1024 * 1024, backupCount=20)
self._fileLogger.setLevel(logging.DEBUG)
# create a logging format
formatter = logging.Formatter('[%(name)s][%(levelname)s][%(asctime)s] %(message)s')
self._fileLogger.setFormatter(formatter)
@property
def file_logger(self) -> RotatingFileHandler:
"""
Get file logger.
:return: file logger
:rtype: logging.handlers.RotatingFileHandler
"""
return self._fileLogger
@property
def console_logger(self) -> StreamHandler:
"""
Get console logger.
:return: console logger
:rtype: logging.StreamHandler
"""
return self._consoleLogger
def create_logger(self, name: str) -> Logger:
"""
Create new logger for the app.
:param name: name for the logger
:type name: str
:return: logger
:rtype: logging.Logger
"""
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# add the handlers to the logger
logger.addHandler(self.file_logger)
logger.addHandler(self.console_logger)
return logger