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

info log lines from discord.py are being directed to sys.stderr #9534

Closed
3 tasks done
modernNeo opened this issue Aug 23, 2023 · 3 comments
Closed
3 tasks done

info log lines from discord.py are being directed to sys.stderr #9534

modernNeo opened this issue Aug 23, 2023 · 3 comments
Labels
as designed This feature is working as intended

Comments

@modernNeo
Copy link

modernNeo commented Aug 23, 2023

Summary

info log lines from discord.py are being directed to sys.stderr

Reproduction Steps

just ran the bot

Minimal Reproducible Code

class WalleBot(commands.Bot):
            def __init__(self):
                super().__init__(command_prefix='.', intents=intents)
                self.bot_loop_manager = BotChannelManager(wall_e_config, self)

            async def setup_hook(self) -> None:

                # removing default help command to allow for custom help command
                print("[main.py] default help command being removed")
                self.remove_command("help")

                await self.add_custom_cog()
                await super().setup_hook()

            async def add_custom_cog(self, module_path_and_name: str = None):
                pass # not relevant to issue

bot = WalleBot()
bot.run(TOKEN)

Expected Results

I expected the below 2 log lines to be white like all the other lines which would indicate they are being sent to sys.stdout

Screenshot from 2023-08-23 09-30-58
Screenshot from 2023-08-23 09-31-10

Actual Results

the red highlighting that PyCharm places on the lines indicates they are actually being printed to sys.stderr

Intents

Intents.all()

System Information

$ python -m discord -v

  • Python v3.9.2-final
  • discord.py v2.3.2-final
  • aiohttp v3.8.5
  • system info: Linux 5.10.0-25-amd64 1 SMP Debian 5.10.191-1 (2023-08-16)

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

@modernNeo modernNeo added the unconfirmed bug A bug report that needs triaging label Aug 23, 2023
@Rapptz Rapptz added as designed This feature is working as intended and removed unconfirmed bug A bug report that needs triaging labels Aug 23, 2023
@Rapptz
Copy link
Owner

Rapptz commented Aug 23, 2023

That's how the default handler for Python works. It's documented. If you want a separate handler you can pass one explicitly.

@Rapptz Rapptz closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2023
@modernNeo
Copy link
Author

I updated the run command to bot.run(TOKEN, log_handler=logging.StreamHandler(sys.stdout)) as suggested so that the INFO log levels get directed to stdout instead but this also resulted in ERROR log lines also going to stdout instead of stderr.

Can I ask for a pointer on how to get this library to redirect only INFO log lines to sys.stdout?

Screenshot from 2023-09-01 09-45-41

@Rapptz
Copy link
Owner

Rapptz commented Sep 3, 2023

This isn't really discord.py related. This library just uses the standard logging library to do the calls and the handlers are provided by the standard library. If you want mixed streams in your handler you'd have to make your own that switches the stream depending on the logging level of the record object. This code's untested but it should probably work:

class MultiStreamHandler(logging.Handler):
    def __init__(self) -> None:
        self.stderr = logging.StreamHandler()
        self.stdout = logging.StreamHandler(sys.stdout)
    
    def emit(self, record: logging.LogRecord) -> None:
        if record.levelno <= logging.INFO:
            self.stdout.emit(record)
        else:
            self.stderr.emit(record)
    
    def flush(self) -> None:
        self.stdout.flush()
        self.stderr.flush()
    
    def close(self) -> None:
        self.stdout.close()
        self.stderr.close() 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed This feature is working as intended
Projects
None yet
Development

No branches or pull requests

2 participants