Files
omnibot22/omnibot/__main__.py
Alek Ratzloff 40a859083a Add TRACE log level
This is useful for when we want important debug messages, but not
necessarily to be flooded with every message that comes through IRC

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2024-07-18 11:02:02 -07:00

70 lines
1.7 KiB
Python

import argparse
import asyncio
import logging
import os
from pathlib import Path
import sys
from .config import ServerConfig
from .bot import Bot
# ADD TRACE
# add TRACE logging level to logging module
TRACE = 5
logging.addLevelName(TRACE, "TRACE")
logging.TRACE = TRACE
def trace(self, message, *args, **kwargs):
if self.isEnabledFor(TRACE):
#args = list(map(str, args))
self._log(TRACE, message, args, **kwargs)
logging.Logger.trace = trace
# END ADD TRACE
parser = argparse.ArgumentParser(description="Run an IRC bot")
parser.add_argument("-c", "--config", type=str, default=None)
parser.add_argument(
"--loglevel",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
required=False,
)
args = parser.parse_args()
config_path = Path(args.config or os.environ.get("OMNIBOT_CONFIG", "config.toml"))
config = ServerConfig()
try:
with open(config_path) as fp:
config.load(fp)
except FileNotFoundError:
print(f"ERROR: config file not found: {config_path}", file=sys.stderr)
sys.exit(1)
# Set log level now
loglevel = logging.INFO
if args.loglevel:
loglevel = getattr(logging, args.loglevel)
elif config.loglevel:
loglevel = getattr(logging, config.loglevel)
logging.basicConfig(
level=loglevel,
format="%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s",
)
log = logging.getLogger(__name__)
log.info("Loaded config file %s", args.config)
log.debug("Using configuration: %s", config)
bot = Bot(config)
try:
asyncio.run(bot.run())
except KeyboardInterrupt:
log.info("Got ctrl-c")
finally:
log.info("Quitting, press ctrl-c to quit immediately")
bot.quit()
asyncio.run(bot.keepalive())