Add an option of logging into a file

This commit is contained in:
DevilXD committed 2022-01-07 19:22:38 +01:00
1 parent cd71f676ca
commit 36ed913ce8
3 files changed
+18 -6

No files matched your search

+8 -1
View File
@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import logging
from copy import copy from copy import copy
from enum import Enum, auto from enum import Enum, auto
from datetime import timedelta from datetime import timedelta
@@ -23,8 +24,9 @@ USER_AGENT = (
"Chrome/96.0.4664.45 Safari/537.36" "Chrome/96.0.4664.45 Safari/537.36"
) )
# Paths # Paths
SETTINGS_PATH = "settings.json" LOG_PATH = "log.txt"
COOKIES_PATH = "cookies.jar" COOKIES_PATH = "cookies.jar"
SETTINGS_PATH = "settings.json"
# Intervals and Delays # Intervals and Delays
PING_INTERVAL = timedelta(minutes=3) PING_INTERVAL = timedelta(minutes=3)
PING_TIMEOUT = timedelta(seconds=10) PING_TIMEOUT = timedelta(seconds=10)
@@ -32,6 +34,11 @@ ONLINE_DELAY = timedelta(seconds=60)
WATCH_INTERVAL = timedelta(seconds=58.7) WATCH_INTERVAL = timedelta(seconds=58.7)
# Tags # Tags
DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b" 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): class State(Enum):
+2 -4
View File
@@ -14,7 +14,7 @@ from typing import (
) )
from version import __version__ 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: if TYPE_CHECKING:
from twitch import Twitch from twitch import Twitch
@@ -687,9 +687,7 @@ class GUIManager:
root.minsize(width=0, height=root.winfo_reqheight()) root.minsize(width=0, height=root.winfo_reqheight())
# register logging handler # register logging handler
handler = TKOutputHandler(self) handler = TKOutputHandler(self)
handler.setFormatter( handler.setFormatter(FORMATTER)
logging.Formatter("{asctime}: {levelname}: {message}", style='{', datefmt="%H:%M:%S")
)
logging.getLogger("TwitchDrops").addHandler(handler) logging.getLogger("TwitchDrops").addHandler(handler)
# https://stackoverflow.com/questions/56329342/tkinter-treeview-background-tag-not-working # https://stackoverflow.com/questions/56329342/tkinter-treeview-background-tag-not-working
+8 -1
View File
@@ -6,12 +6,14 @@ from typing import Optional
from twitch import Twitch from twitch import Twitch
from version import __version__ from version import __version__
from constants import FORMATTER, LOG_PATH
class ParsedArgs(argparse.Namespace): class ParsedArgs(argparse.Namespace):
_verbose: int _verbose: int
_debug_ws: bool _debug_ws: bool
_debug_gql: bool _debug_gql: bool
log: bool
game: Optional[str] game: Optional[str]
@property @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-ws", dest="_debug_ws", action="store_true")
parser.add_argument("--debug-gql", dest="_debug_gql", action="store_true") parser.add_argument("--debug-gql", dest="_debug_gql", action="store_true")
parser.add_argument("-g", "--game", default=None) parser.add_argument("-g", "--game", default=None)
parser.add_argument("-l", "--log", action="store_true")
options: ParsedArgs = parser.parse_args(namespace=ParsedArgs()) options: ParsedArgs = parser.parse_args(namespace=ParsedArgs())
# handle logging stuff # handle logging stuff
if options.logging_level > logging.DEBUG: if options.logging_level > logging.DEBUG:
# redirect the root logger into a NullHandler, effectively ignoring all logging calls # 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()) logging.getLogger().addHandler(logging.NullHandler())
logger = logging.getLogger("TwitchDrops") logger = logging.getLogger("TwitchDrops")
logger.setLevel(options.logging_level) 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.gql").setLevel(options.debug_gql)
logging.getLogger("TwitchDrops.websocket").setLevel(options.debug_ws) logging.getLogger("TwitchDrops.websocket").setLevel(options.debug_ws)
# client run # client run