Open DeflateAwning opened 10 months ago
The only way to display level icons is to re-define an appropriate format
. The default one is defined here: https://github.com/Delgan/loguru/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_defaults.py#L32-L38
Your code would then look like this:
import sys
from loguru import logger
custom_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level: <8}</level> | {level.icon} | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)
logger.remove()
logger.add(sys.stderr, format=custom_format)
The default format can also be changed by setting the LOGURU_FORMAT
environment variable. This avoids having to re-configure it every time you use Loguru.
Thanks for the explanation. That's a long bit of code to include something that feels like it could be an easy addition, considering that the level.icon
attribute is standard.
Is there an easy place in the API/code where a simple logger.enable_icons_in_default_logs()
could be added or something?
Hum, sorry but adding such enable_icons_in_default_logs()
is not something I'm considering. Users may have diverse preferences when it comes to choosing a format, and I want to avoid committing to new methods to meet individual expectations. I highly recommend configuring LOGURU_FORMAT
environment variable once and for all. This is a versatile solution that can cater to everyone's preferences without the necessity to extend Loguru's API.
Also note that the custom_format
variable in the above snippet is intentionally verbose and explicit, but you can use a shortened version if you prefer:
custom_format = (
"<g>{time:YYYY-MM-DD HH:mm:ss.SSS}</> |"
"<lvl>{level: <8}</> | {level.icon} | "
"<c>{name}</>:<c>{function}</>:<c>{line}</> - <lvl>{message}</>"
)
I wish the "Code snippets and recipes for loguru" section of the docs had an easy explanation of how to add the icons for each log level to the stderr logs, keeping everything else the same.
It seems like the icons are grossly underutilized throughout this project (including not being used at all in the default setup), despite being very helpful when the logs are being viewed in a colorless log viewer (e.g., AWS CloudWatch).