Add an option of logging into a file

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

View File

@@ -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):

6
gui.py
View File

@@ -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

View File

@@ -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