You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to add some debugging images (png format) to the logging for better visualization. Seems like it's hard to find a library that natively supports adding images to logging.
I found some code elsewhere (below) but it's not working well cosmetically. Do you have any suggestions using loguru to save images?
Thank you!
importloguruimportmatplotlib.pyplotaspltimportbase64fromioimportBytesIO# Create a logger objectlogger=loguru.logger# Create a plot and save to a bufferfig, ax=plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.set_title('Example plot')
ax.set_xlabel('X label')
ax.set_ylabel('Y label')
buf=BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
plot_data=base64.b64encode(buf.read()).decode('utf8')
plt.close()
# Add the plot to a log messagelogger.add('example.html', format='<b>{time}</b> {level}: {message}',
level='DEBUG', compression='zip',
serialize=True, rotation='10 MB',
enqueue=True)
logger.debug('This is a log message with an embedded plot',
plot='<img src="data:image/png;base64,{}">'.format(plot_data))
And the result HTML looks like this:
The text was updated successfully, but these errors were encountered:
It all depends on how you intend to view your logs and images.
Logs are just text. If you wish to embed images, you need to define an encoding format that will be recognized by the application used to read your logs. For example, if your logs are read in a terminal, it's unlikely that you'll be able to display any images at all.
Thank you for the explanation! I figured out a way to encode images into base64 strings and save them to a plain HTML file. But this thing won't work with the terminal for sure. The code is posted here for someone who has the same need and close this issue now.
Hi,
I would like to add some debugging images (png format) to the logging for better visualization. Seems like it's hard to find a library that natively supports adding images to logging.
I found some code elsewhere (below) but it's not working well cosmetically. Do you have any suggestions using loguru to save images?
Thank you!
And the result HTML looks like this:
The text was updated successfully, but these errors were encountered: