Files
omnibot22/omnibot/__main__.py
Alek Ratzloff a823871039 Add log level config option
* Log levels can now be set via the command line and the configuration
  file.
* ServerConfig.load() function takes a file-like object now, rather than
  a string

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2022-06-03 17:21:20 -07:00

48 lines
1.1 KiB
Python

import argparse
import asyncio
import logging
from .config import ServerConfig
from .bot import Bot
parser = argparse.ArgumentParser(description="Run an IRC bot")
parser.add_argument(
"-c", "--config", type=argparse.FileType("r"), default="config.toml"
)
parser.add_argument(
"--loglevel",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
required=False,
)
args = parser.parse_args()
config = ServerConfig()
config.load(args.config)
# 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())