Cog-Creators / Red-DiscordBot

A multi-function Discord bot
https://docs.discord.red
GNU General Public License v3.0
4.81k stars 2.31k forks source link

Util to generate Logger with consistent naming schema #5574

Open i-am-zaidali opened 2 years ago

i-am-zaidali commented 2 years ago

Type of feature request

Other

Description of the feature you're suggesting

After a slightly detailed discussion in #coding on the main server, it came to the conclusion that unified namespaces for loggers of 3rd party cogs would be cool. It was proposed that a function could be added to the utils folder that takes the cog name and the repo name as its arguments, with an optional sub_module argument and returns the Logger object with a namespace as follows: red.RepoName.CogName[.SubModule]. This new addition is useful in ways like having consistency in all logger names for all 3rd party cogs (that use this), core devs could force a certain namespace format, devs won't have to worry about their logs being silenced if they use the wrong namespace and this would strip the namespace to a specified char limit so that it doesn't look weird on consoles with extra large names.

Anything else?

This would also merely be an addition and would not change or break any existing functionality in red.

I would like other's opinions on this and would be happy to PR this addition if agreed to.

Drapersniper commented 2 years ago

If this will be done you should take this opportunity to ensure a clean logger namespace. something akin to the following:

def get_logger(cog: str, repo:str, type: Literal["cog", "lib"] = "cog", sub_module: Optional[str] = None):
  base_namespace = "red.3pt"
  parts = [base_namespace, type, repo, cog]
  if sub_module:
    parts.append(sub_module.replace(" ", "."))
  final_namespace = ".".join(parts).lower()
  ... # check if duplicate and handle it
  return logging.getLogger(final_namespace)

To expand on

and this would strip the namespace to a specified char limit so that it doesn't look weird on consoles with extra-large names.

In addition to the code block above, due to to potential for having very large logger names (Audio cog for example), a character limiter should be added to https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/logging.py#L189

          if logger_name:
            output.append(f"[{logger_name[:32]}] ", style="bright_black")

This would ensure a consistent length of logger names printed on the console (while keeping the logger names in the logging file untouched)

if this is approved, https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/docs/guide_cog_creators.rst#recommendations-for-cog-creators should be updated to direct cog creators to use this util method to ensure consistency in the logging namespace

Drapersniper commented 2 years ago

This would likely make more sense to go into https://github.com/Cog-Creators/Red-Common, at least the get_logger section - but the second part should still be done in Red as Red is the lib that controls the log formatting.