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.

Two line logged for each statement?

See original GitHub issue
from loguru import logger
logger.add(sys.stdout, colorize=True, backtrace=True, diagnose=True, format="<green>{time}</green> <level>{message}</level>")

if __name__ == "__main__":
    txt = "This is test text"
    logger.debug("{}", txt)
    logger.debug("End")

My terminal output looks like image

Not sure why it wants to output two rows per log

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Jul 26, 2019

@jamesstidard The standard logging is not always straightforward to work with, I would blame that instead. 😄

It seems you need first to remove the Sanic’s handler:

logging.getLogger("sanic.root").handlers.clear()

For some libraries, this extra-step is not required. I’m not sure of why, I guess it depends on how these libraries configure logging internally. Sanic seems to attach their own stderr handler to logging.getLogger("sanic.root"). There is a bit of documentation there: Sanic Logging

2reactions
jamesstidardcommented, Jul 26, 2019

@Delgan ah, that makes a lot of sense. Sorry, I didn’t think to check out the doc string on the function apparently… probably should have figured that out.

Not to take advantage of your good will, but adding the remove call does indeed get rid of the duplicate lines, but not the ones I was hoping 😃

import logging

from loguru import logger

from sanic import Sanic
from sanic.response import json

# Redirect all logging to loguru
class InterceptHandler(logging.Handler):
    def emit(self, record):
        # Retrieve context where the logging call occurred, this happens to be in the 6th frame upward
        logger_opt = logger.opt(depth=6, exception=record.exc_info)
        logger_opt.log(record.levelno, record.getMessage())

# logger.remove()
logging.basicConfig(handlers=[InterceptHandler()], level=0)

app = Sanic()

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

Without the remove line this outputs:

[2019-07-26 10:44:35 +0100] [17811] [INFO] Goin' Fast @ http://0.0.0.0:8000
2019-07-26 10:44:35.242 | Level 20 | sanic.app:_helper:1383 - Goin' Fast @ http://0.0.0.0:8000
[2019-07-26 10:44:35 +0100] [17811] [INFO] Starting worker [17811]
2019-07-26 10:44:35.248 | Level 20 | sanic.server:serve:802 - Starting worker [17811]

With the remove line, it drops the pretty loguru ones in favor of the default. It’s still the morning here so I’ll continue to blame it on that 😃

Thanks for the help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Collect and Manage All of Your Multi-Line Logs
We will go over two primary methods for collecting and processing multi-line logs in a way that aggregates them as single events:.
Read more >
Styling multi-line conditions in 'if' statements? [closed]
I suggest moving the and keyword to the second line and indenting all lines containing conditions with two spaces instead of four:
Read more >
Q17 Complete each statement with the... [FREE SOLUTION]
Two skew lines are parallel. Complete each statement with the word always, sometimes, or never. Lines in two parallel planes are parallel to...
Read more >
Solved Determine whether each statement is true or false. 1.
2. Two lines perpendicular to a third line are parallel. 3. Two planes parallel to a third plane are parallel. 4. Two planes...
Read more >
Determine whether each statement is true or false in R3 (a ...
j) Two lines either intersect or are parallel . (k) A plane and a line either intersect or are parallel. 2) Find a...
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