BAPSicle/helpers/logging_manager.py

38 lines
952 B
Python
Raw Normal View History

2020-10-30 22:00:30 +00:00
import logging
from helpers.os_environment import resolve_external_file_path
import os
2020-10-30 22:00:30 +00:00
class LoggingManager():
2020-12-19 14:57:37 +00:00
logger: logging.Logger
2020-10-30 22:00:30 +00:00
2020-12-19 14:57:37 +00:00
def __init__(self, name: str):
2020-10-30 22:00:30 +00:00
self.logger = logging.getLogger(name)
filename: str = resolve_external_file_path("/logs/" + name + ".log")
if not os.path.isfile(filename):
try:
# Try creating the file.
open(filename, "x")
2020-11-09 00:10:36 +00:00
except:
print("Failed to create log file")
return
2020-10-30 22:00:30 +00:00
logging.basicConfig(
filename=filename,
2020-10-30 22:00:30 +00:00
format='%(asctime)s | %(levelname)s | %(message)s',
level=logging.INFO,
filemode='a'
)
2020-10-30 23:59:58 +00:00
self.logger.info("** LOGGER STARTED **")
2020-10-30 22:00:30 +00:00
def __del__(self):
2020-10-30 23:59:58 +00:00
self.logger.info("** LOGGER EXITING **")
2020-10-30 22:00:30 +00:00
logging.shutdown()
@property
2020-12-19 14:57:37 +00:00
def log(self) -> logging.Logger:
2020-10-30 22:00:30 +00:00
return self.logger