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>
This commit is contained in:
@@ -1,20 +1,40 @@
|
|||||||
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .config import ServerConfig
|
from .config import ServerConfig
|
||||||
from .bot import Bot
|
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(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=loglevel,
|
||||||
format="%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s",
|
format="%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s",
|
||||||
)
|
)
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
log.debug("Loading config")
|
log.info("Loaded config file %s", args.config)
|
||||||
config = ServerConfig()
|
|
||||||
config.load("config.toml")
|
|
||||||
log.debug("Using configuration: %s", config)
|
log.debug("Using configuration: %s", config)
|
||||||
|
|
||||||
bot = Bot(config)
|
bot = Bot(config)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Mapping, Sequence, Set
|
from typing import IO, Any, Mapping, Sequence, Set
|
||||||
|
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
@@ -31,11 +31,9 @@ class ServerConfig:
|
|||||||
plugins: Sequence[PluginConfig] = dataclasses.field(default_factory=list)
|
plugins: Sequence[PluginConfig] = dataclasses.field(default_factory=list)
|
||||||
channels: Sequence[str] = dataclasses.field(default_factory=list)
|
channels: Sequence[str] = dataclasses.field(default_factory=list)
|
||||||
nick: str = "omnibot"
|
nick: str = "omnibot"
|
||||||
|
loglevel: str | None = None
|
||||||
|
|
||||||
def load(self, path: Path | str):
|
def load(self, fp: IO[str]):
|
||||||
if isinstance(path, str):
|
|
||||||
path = Path(path)
|
|
||||||
with open(path) as fp:
|
|
||||||
obj = toml.load(fp)
|
obj = toml.load(fp)
|
||||||
|
|
||||||
if "server" not in obj:
|
if "server" not in obj:
|
||||||
@@ -81,6 +79,14 @@ class ServerConfig:
|
|||||||
raise ConfigError("nick", "must be a string")
|
raise ConfigError("nick", "must be a string")
|
||||||
self.nick = obj["nick"]
|
self.nick = obj["nick"]
|
||||||
|
|
||||||
|
if "loglevel" in obj:
|
||||||
|
if not isinstance(obj["loglevel"], str):
|
||||||
|
raise ConfigError("loglevel", "must be a string")
|
||||||
|
loglevels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
||||||
|
if obj["loglevel"] not in loglevels:
|
||||||
|
raise ConfigError("loglevel", "must be one of: " + " ".join(loglevels))
|
||||||
|
self.loglevel = obj["loglevel"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def all_channels(self) -> Set[str]:
|
def all_channels(self) -> Set[str]:
|
||||||
channels = set(self.channels)
|
channels = set(self.channels)
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ async def main():
|
|||||||
channel = sys.argv[1]
|
channel = sys.argv[1]
|
||||||
files = sys.argv[2:]
|
files = sys.argv[2:]
|
||||||
server_config = ServerConfig()
|
server_config = ServerConfig()
|
||||||
server_config.load("config.toml")
|
with open("config.toml") as fp:
|
||||||
|
server_config.load(fp)
|
||||||
# This only works on one plugin per config
|
# This only works on one plugin per config
|
||||||
bot = Bot(server_config)
|
bot = Bot(server_config)
|
||||||
plugin = [plugin for plugin in bot.plugins if isinstance(plugin, Markov)][0]
|
plugin = [plugin for plugin in bot.plugins if isinstance(plugin, Markov)][0]
|
||||||
|
|||||||
Reference in New Issue
Block a user