Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for the future Red Logger class #95

Open
wants to merge 9 commits into
base: V3
Choose a base branch
from
39 changes: 24 additions & 15 deletions logger/logger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import contextlib
import logging
import tabulate

from redbot.core import commands, Config
from redbot.core.utils import chat_formatting
from redbot.core.utils.chat_formatting import humanize_list

try:
from red_commons.logging import VERBOSE, TRACE
except ImportError:
VERBOSE = logging.DEBUG - 3
TRACE = logging.DEBUG - 5

log = logging.getLogger("red.cogs.Squid-Plugins.Logger")


class Logger(commands.Cog):
Expand All @@ -25,24 +35,19 @@ def __init__(self, bot):
**logger_defaults
)

self.levels = [
"debug",
"warning",
"critical",
"info",
"error",
"notset"
]

self.level_map = {
logging.CRITICAL: "Critical",
logging.ERROR: "Error",
logging.WARNING: "Warning",
logging.INFO: "Info",
logging.DEBUG: "Debug",
VERBOSE: "Verbose",
TRACE: "Trace",
logging.NOTSET: "Not set"
}

self.name_to_int_map = {v.lower().replace(" ", ""): k for k, v in self.level_map.items()}

async def red_delete_data_for_user(self, **kwargs):
"""Nothing to delete."""
return
Expand All @@ -52,6 +57,7 @@ async def refresh_levels(self):
for name, data in all_data.items():
logger = logging.getLogger(name)
level = data['override']
log.debug("refresh_levels - Setting %s to %s", name, level)
logger.setLevel(level)

def _available_loggers(self):
Expand All @@ -62,11 +68,14 @@ def _int_to_name(self, level_int):
return self.level_map.get(level_int, "Unknown")

def _name_to_int(self, level_name: str):
if level_name.isdigit():
return level_name
with contextlib.suppress(ValueError):
if level_name.isdigit() and int(level_name) in self.level_map:
return int(level_name)

if level_name.lower() in self.levels:
return getattr(logging, level_name.upper())
if level_name.lower().replace(" ", "") in self.name_to_int_map:
value = self.name_to_int_map[level_name.lower().replace(" ", "")]
return value
raise AttributeError

def _loggers_with_levels(self):
loggers = self._available_loggers()
Expand All @@ -90,7 +99,7 @@ async def _set_level(self, logger, level):

if curr_default is None:
await group.original.set(curr_level)

log.debug("_set_level - Setting %s to %s", logger.name, level)
await group.override.set(level)

logger.setLevel(level)
Expand Down Expand Up @@ -127,7 +136,7 @@ async def logger_setlevel(self, ctx, name: str, level: str):
try:
to_level = self._name_to_int(level)
except AttributeError:
await ctx.send("Invalid level.")
await ctx.send(f"Invalid level, levels are {humanize_list(list(self.name_to_int_map.keys()))}")
return

await self._set_level(curr_log, to_level)
Expand Down