Skip to content

Commit 076089f

Browse files
committed
Add flag for debug logging
1 parent df633e3 commit 076089f

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The project uses semantic versioning (see [semver](https://semver.org)).
99

1010
- `/zeus-upload` command now accepts an optional modlist (JSON snippet exported
1111
from Mod Presets menu in Reforger).
12+
- Command line flag `--debug` for enabling more verbose debug logging.
1213

1314
### Fixed
1415

src/zeusops_bot/cli.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from zeusops_bot import reforger_config_gen as cmd
1010
from zeusops_bot.discord import ZeusopsBot
1111
from zeusops_bot.errors import ZeusopsBotConfigException
12+
from zeusops_bot.logging import setup_logging
1213
from zeusops_bot.models import ModDetail
1314
from zeusops_bot.settings import ZeusopsBotConfig, load
1415

@@ -35,6 +36,11 @@ def parse_arguments(args: list[str]) -> argparse.Namespace:
3536
"zeusops-bot",
3637
description="Multipurpose discord bot for the Zeusops community",
3738
)
39+
parser.add_argument(
40+
"--debug",
41+
help="Enable debug logging",
42+
action="store_true",
43+
)
3844
return parser.parse_args(args)
3945

4046

@@ -43,10 +49,10 @@ def cli(arguments: list[str] | None = None):
4349
if arguments is None:
4450
arguments = sys.argv[1:]
4551
_args = parse_arguments(arguments)
46-
return main()
52+
return main(_args.debug)
4753

4854

49-
def main():
55+
def main(debug: bool = False):
5056
"""Run the main bot"""
5157
try:
5258
config = load(ZeusopsBotConfig)
@@ -60,11 +66,9 @@ def main():
6066
print("Error while loading the bot's config from envvars", file=sys.stderr)
6167
raise
6268

63-
logging.basicConfig(level=logging.DEBUG)
64-
logging.getLogger("discord").setLevel(logging.INFO)
65-
logging.getLogger("discord.gateway").setLevel(logging.WARNING)
69+
setup_logging(debug)
6670

67-
bot = ZeusopsBot(config, logging.getLogger("discord"))
71+
bot = ZeusopsBot(config, logging.getLogger("zeusops.discord"))
6872
bot.run() # Token is already in config
6973

7074

src/zeusops_bot/discord.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Manage the Discord-side horrors"""
22

3+
from logging import Logger
4+
35
from discord import Bot
46

57
from zeusops_bot.cogs import ZeusUpload
@@ -9,7 +11,7 @@
911
class ZeusopsBot(Bot):
1012
"""A Discord Client for general purposes"""
1113

12-
def __init__(self, config: ZeusopsBotConfig, logger, *args, **kwargs):
14+
def __init__(self, config: ZeusopsBotConfig, logger: Logger, *args, **kwargs):
1315
"""Initialize the Client"""
1416
super().__init__(*args, **kwargs)
1517
self.config = config

src/zeusops_bot/logging.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Logging-related utilities"""
2+
3+
import logging
4+
5+
6+
def setup_logging(debug):
7+
"""Set up basic logger"""
8+
if debug:
9+
logging.basicConfig(level=logging.DEBUG)
10+
else:
11+
logging.basicConfig(level=logging.INFO)
12+
13+
# By default the Discord library is very verbose, so we're limiting it
14+
# a bit
15+
logging.getLogger("discord").setLevel(logging.INFO)
16+
logging.getLogger("discord.gateway").setLevel(logging.WARNING)

0 commit comments

Comments
 (0)