-
Notifications
You must be signed in to change notification settings - Fork 1
/
logs.py
42 lines (31 loc) · 1.35 KB
/
logs.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
import logging
import os
from logging.handlers import RotatingFileHandler
from utils import resource_path
class Logger:
def __init__(self, log_file=resource_path("LOG_INFO.log"), max_bytes=2097152, backup_count=5):
self.log_file = log_file
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
# Create a rotating file handler and set the level to DEBUG
file_handler = RotatingFileHandler(self.log_file, maxBytes=max_bytes, backupCount=backup_count)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
# Create a console handler and set the level to INFOs
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
self.logger.addHandler(console_handler)
def log_info(self, message):
self.logger.info(message)
def log_variable(self, var, out):
message = f"{var}: {out}"
self.logger.info(message)
def log_warning(self, message):
self.logger.warning(message)
def log_error(self, message):
self.logger.error(message)
def log_debug(self, message):
self.logger.debug(message)