From 181d0368aff5ed6e23f555bc13aa7bc4aded788b Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Mon, 11 Oct 2021 10:49:21 +0200 Subject: [PATCH] auto_clear logs in settings :) --- README.md | 1 + TwitchChannelPointsMiner/logger.py | 35 ++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1fcec521..73138fb1 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ You can combine all priority but keep in mind that use `ORDER` and `POINTS_ASCEN | `file_level` | level | logging.DEBUG | Level of logs in file save - If you think the log file it's too big, use logging.INFO | | `emoji` | bool | For Windows is False else True | On Windows, we have a problem printing emoji. Set to false if you have a problem | | `colored` | bool | True | If you want to print colored text [#45](https://github.com/Tkd-Alex/Twitch-Channel-Points-Miner-v2/issues/45) [#82](https://github.com/Tkd-Alex/Twitch-Channel-Points-Miner-v2/issues/82) | +| `auto_clear` | bool | True | Create a file rotation handler with interval = 1D and backupCount = 7 [#215](https://github.com/Tkd-Alex/Twitch-Channel-Points-Miner-v2/issues/215) | | `color_palette` | ColorPalette | All messages are Fore.RESET except WIN and LOSE bet (GREEN and RED) | Create your custom color palette. Read more above. | #### Color Palette diff --git a/TwitchChannelPointsMiner/logger.py b/TwitchChannelPointsMiner/logger.py index 895e3fe8..e2912ac3 100644 --- a/TwitchChannelPointsMiner/logger.py +++ b/TwitchChannelPointsMiner/logger.py @@ -1,6 +1,7 @@ import logging import os import platform +from datetime import datetime from logging.handlers import TimedRotatingFileHandler from pathlib import Path @@ -104,6 +105,7 @@ def __init__( file_level: int = logging.DEBUG, emoji: bool = platform.system() != "Windows", colored: bool = False, + auto_clear: bool = True, color_palette: ColorPalette = ColorPalette(), ): self.save = save @@ -112,6 +114,7 @@ def __init__( self.file_level = file_level self.emoji = emoji self.colored = colored + self.auto_clear = auto_clear self.color_palette = color_palette @@ -142,18 +145,26 @@ def configure_loggers(username, settings): if settings.save is True: logs_path = os.path.join(Path().absolute(), "logs") Path(logs_path).mkdir(parents=True, exist_ok=True) - logs_file = os.path.join( - logs_path, - f"{username}.log", - ) - file_handler = TimedRotatingFileHandler( - logs_file, - when="D", - interval=1, - backupCount=7, - encoding="utf-8", - delay=False, - ) + if settings.auto_clear is True: + logs_file = os.path.join( + logs_path, + f"{username}.log", + ) + file_handler = TimedRotatingFileHandler( + logs_file, + when="D", + interval=1, + backupCount=7, + encoding="utf-8", + delay=False, + ) + else: + logs_file = os.path.join( + logs_path, + f"{username}.{datetime.now().strftime('%Y%m%d-%H%M%S')}.log", + ) + file_handler = logging.FileHandler(logs_file, "w", "utf-8") + file_handler.setFormatter( logging.Formatter( fmt="%(asctime)s - %(levelname)s - %(name)s - [%(funcName)s]: %(message)s",