From 36ed913ce8d70d01c66ded3ebc74a5dbfb513b8d Mon Sep 17 00:00:00 2001 From: DevilXD Date: Fri, 7 Jan 2022 19:22:38 +0100 Subject: [PATCH] Add an option of logging into a file --- constants.py | 9 ++++++++- gui.py | 6 ++---- main.py | 9 ++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/constants.py b/constants.py index 8b8c89b..25dfc7d 100644 --- a/constants.py +++ b/constants.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging from copy import copy from enum import Enum, auto from datetime import timedelta @@ -23,8 +24,9 @@ USER_AGENT = ( "Chrome/96.0.4664.45 Safari/537.36" ) # Paths -SETTINGS_PATH = "settings.json" +LOG_PATH = "log.txt" COOKIES_PATH = "cookies.jar" +SETTINGS_PATH = "settings.json" # Intervals and Delays PING_INTERVAL = timedelta(minutes=3) PING_TIMEOUT = timedelta(seconds=10) @@ -32,6 +34,11 @@ ONLINE_DELAY = timedelta(seconds=60) WATCH_INTERVAL = timedelta(seconds=58.7) # Tags DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b" +FORMATTER = logging.Formatter( + "{asctime}.{msecs:<03}:\t{levelname:>7}:\t{message}", + style='{', + datefmt="%Y-%m-%d %H:%M:%S", +) class State(Enum): diff --git a/gui.py b/gui.py index 8c38de7..13fa2f3 100644 --- a/gui.py +++ b/gui.py @@ -14,7 +14,7 @@ from typing import ( ) from version import __version__ -from constants import WS_TOPICS_LIMIT, MAX_WEBSOCKETS, State +from constants import FORMATTER, WS_TOPICS_LIMIT, MAX_WEBSOCKETS, State if TYPE_CHECKING: from twitch import Twitch @@ -687,9 +687,7 @@ class GUIManager: root.minsize(width=0, height=root.winfo_reqheight()) # register logging handler handler = TKOutputHandler(self) - handler.setFormatter( - logging.Formatter("{asctime}: {levelname}: {message}", style='{', datefmt="%H:%M:%S") - ) + handler.setFormatter(FORMATTER) logging.getLogger("TwitchDrops").addHandler(handler) # https://stackoverflow.com/questions/56329342/tkinter-treeview-background-tag-not-working diff --git a/main.py b/main.py index 8aa5cfa..a364144 100644 --- a/main.py +++ b/main.py @@ -6,12 +6,14 @@ from typing import Optional from twitch import Twitch from version import __version__ +from constants import FORMATTER, LOG_PATH class ParsedArgs(argparse.Namespace): _verbose: int _debug_ws: bool _debug_gql: bool + log: bool game: Optional[str] @property @@ -55,14 +57,19 @@ parser.add_argument("-v", dest="_verbose", action="count", default=0) parser.add_argument("--debug-ws", dest="_debug_ws", action="store_true") parser.add_argument("--debug-gql", dest="_debug_gql", action="store_true") parser.add_argument("-g", "--game", default=None) +parser.add_argument("-l", "--log", action="store_true") options: ParsedArgs = parser.parse_args(namespace=ParsedArgs()) # handle logging stuff if options.logging_level > logging.DEBUG: # redirect the root logger into a NullHandler, effectively ignoring all logging calls - # that aren't ours. This always runs, unless the main logging level is DEBUG or below. + # that aren't ours. This always runs, unless the main logging level is DEBUG or lower. logging.getLogger().addHandler(logging.NullHandler()) logger = logging.getLogger("TwitchDrops") logger.setLevel(options.logging_level) +if options.log: + handler = logging.FileHandler(LOG_PATH) + handler.setFormatter(FORMATTER) + logger.addHandler(handler) logging.getLogger("TwitchDrops.gql").setLevel(options.debug_gql) logging.getLogger("TwitchDrops.websocket").setLevel(options.debug_ws) # client run