Send log messages with async function
See original GitHub issueI am writing a bot for Discord and using the discord.py library which is async. I want to send some log messages to Discord using the bot.
Essentially, all of the bot activity happens inside a class. I am having a lot of trouble figuring out where to put the logger.add and whether the sink function should be inside the class or outside the class. If it’s outside, then the bot can’t be accessed from the function (critical to send the message). If it’s inside, then I don’t know where to put logger.add.
Here is where I am at the moment, but I get errors that Push.send_log
was never awaited.
from discord.ext import commands, tasks
from config import settings
from loguru import logger
class Push(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.test_loop.start()
logger.add(self.send_log, level="WARNING")
def cog_unload(self):
self.test_loop.cancel()
@tasks.loop(seconds=15)
async def test_loop(self):
print("success")
self.logger.warning("Yes!")
async def send_log(self, message):
await self.bot.get_channel(settings['pushChannels']['pushLog']).send(message)
def setup(bot):
bot.add_cog(Push(bot))
Any help would be appreciated.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
logging errors in async functions - node.js - Stack Overflow
I have this code: async function getURL() { try { await fetch("http://www.blah.com"); return 0; } catch (err) { return err; } } getURL()....
Read more >How to use async/await with postMessage
It works by sending a message to the other side, then the receiving end can listen to message events. For example, the page...
Read more >async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration declares an async function where the await keyword is permitted within the function body. The async and await ...
Read more >Log4j 2 Lock-free Asynchronous Loggers for Low-Latency ...
Async logging can help prevent or dampen latency spikes by shortening the wait time until the next message can be logged. If the...
Read more >DispatchQueue vs. async/await especially for logging
The use of DispatchQueue in those cases prevents having to work in an async context. On the contrary, if you define a logger...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Using Loguru
v0.4.0
, using thesend_log()
asynchronous method as a sink should work out-of-the-box. 👍Make sure to await
complete()
before leaving the event loop.–Moved to separate issue–