Delgan / loguru

Python logging made (stupidly) simple
MIT License
18.69k stars 682 forks source link

Can `loguru` conditionally display the extra field if the data is non-empty only? #1164

Closed mainrs closed 2 days ago

mainrs commented 4 days ago

My template contains | {extra} at the end. Is it possible to only print that part of any extra arguments are present? Right now all my logging lines without extra data end in | {}.

Delgan commented 4 days ago

You need to use a dynamic format function:

def formatter(record):
    if record["extra"]:
        return "{time} {message} | {extra}\n{exception}"
    else:
        return "{time} {message}\n{exception}"    
mainrs commented 2 days ago

Thank you!