#🔒 Console logging with Windows Powershell

26 messages · Page 1 of 1 (latest)

opal dune
#

I have a discord.py bot I'm running in a Windows 10 PowerShell instance. I've been using the console as a good enough logging solution, but I think I need a more stable solution.

Having learned about Tee-Object, I was curious about using it to log the console contents to a file. I was successfully able to get this to work with a simple hello world script using the powershell script & "<python.exe path>" "<Python Script Path>" | Tee-Object -FilePath "<logfile path>" -Append, which printed everything to both the console and the log file.

However, when I try the same thing with the discord.py bot, which runs continuously until I use ctrl+c to terminate it, it fails to display anything in the console or send anything to the log file when using exactly the same PowerShell script as above.

I'd like to know what the "right" way to do this bifurcated logging to console and file is, and I'm curious why the discord.py script is causing Tee-Object to stop working.

safe ingotBOT
#

@opal dune

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

opal dune
#

Console logging with Windows Powershell

acoustic bay
#

!d discord.utils.setup_logging

safe ingotBOT
#

discord.utils.setup_logging(*, handler=..., formatter=..., level=..., root=True)```
A helper function to setup logging.

This is superficially similar to [`logging.basicConfig()`](https://docs.python.org/3/library/logging.html#logging.basicConfig) but uses different defaults and a colour formatter if the stream can display colour.

This is used by the [`Client`](https://discordpy.readthedocs.io/en/stable/api.html#discord.Client) to set up logging if `log_handler` is not `None`...
restive bough
#

Discord.py uses buffered logging and async I/O, which doesn’t flush output automatically when piped through Tee-Object PowerShell’s redirection also makes Python suppress non-terminal output leaving logs stuck in memory, usre python’s built-in logging (not Tee-Object) or force output with -u (unbuffered mode)

python -u bot.py | Tee-Object -File log.txt -Append
acoustic bay
#

!d logging.FileHandler

safe ingotBOT
#

class logging.FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)```
Returns a new instance of the [`FileHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler) class. The specified file is opened and used as the stream for logging. If *mode* is not specified, `'a'` is used. If *encoding* is not `None`, it is used to open the file with that encoding. If *delay* is true, then file opening is deferred until the first call to [`emit()`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler.emit). By default, the file grows indefinitely. If *errors* is specified, it’s used to determine how encoding errors are handled.

Changed in version 3.6: As well as string values, [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) objects are also accepted for the *filename* argument.

Changed in version 3.9: The *errors* parameter was added.
restive bough
#

use*

opal dune
opal dune
acoustic bay
opal dune
acoustic bay
#

!d logging.Handler

safe ingotBOT
acoustic bay
#

lemme take a look

acoustic bay
# safe ingot

yeah you'll probably do good to subclass this, store a file handler and stream handler, and then override methods like emit() and flush() to apply over both of them

opal dune
opal dune
acoustic bay
#

congrats on solving it

safe ingotBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.