diff --git a/helpers/logging_manager.py b/helpers/logging_manager.py index dbc00b0..25cd74b 100644 --- a/helpers/logging_manager.py +++ b/helpers/logging_manager.py @@ -1,7 +1,10 @@ import logging +from logging.handlers import RotatingFileHandler from helpers.os_environment import resolve_external_file_path import os +LOG_MAX_SIZE_MB = 20 +LOG_BACKUP_COUNT = 4 class LoggingManager(): @@ -10,6 +13,16 @@ class LoggingManager(): def __init__(self, name: str): self.logger = logging.getLogger(name) + + logpath: str = resolve_external_file_path("/logs") + if not os.path.isdir(logpath): + try: + # Try creating the directory. + os.mkdir(logpath) + except: + print("Failed to create log directory.") + return + filename: str = resolve_external_file_path("/logs/" + name + ".log") if not os.path.isfile(filename): @@ -17,12 +30,12 @@ class LoggingManager(): # Try creating the file. open(filename, "x") except: - print("Failed to create log file") + print("Failed to create log file.") return self.logger.setLevel(logging.INFO) - fh = logging.FileHandler(filename) + fh = RotatingFileHandler(filename, maxBytes=LOG_MAX_SIZE_MB*(1024**2), backupCount=LOG_BACKUP_COUNT) formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s') fh.setFormatter(formatter) # add the handler to the logger diff --git a/logs/.gitignore b/logs/.gitignore index bf0824e..e5ebf25 100644 --- a/logs/.gitignore +++ b/logs/.gitignore @@ -1 +1 @@ -*.log \ No newline at end of file +*.log* diff --git a/player.py b/player.py index 7c4852e..5b6fdd0 100644 --- a/player.py +++ b/player.py @@ -504,11 +504,11 @@ class Player(): self.running = True self.out_q = out_q - self.logger = LoggingManager("channel" + str(channel)) + self.logger = LoggingManager("Player" + str(channel)) self.api = MyRadioAPI(self.logger) - self.state = StateManager("channel" + str(channel), self.logger, + self.state = StateManager("Player" + str(channel), self.logger, self.__default_state, self.__rate_limited_params) self.state.add_callback(self._send_status)