question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send log messages with async function

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Dec 1, 2019

Using Loguru v0.4.0, using the send_log() asynchronous method as a sink should work out-of-the-box. 👍

Make sure to await complete() before leaving the event loop.

0reactions
t-martcommented, Nov 21, 2019

–Moved to separate issue

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found