Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibilities to add visual images to python log #1135

Closed
tongshencmu opened this issue May 2, 2024 · 2 comments
Closed

Possibilities to add visual images to python log #1135

tongshencmu opened this issue May 2, 2024 · 2 comments
Labels
question Further information is requested

Comments

@tongshencmu
Copy link

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!

import loguru
import matplotlib.pyplot as plt
import base64
from io import BytesIO

# Create a logger object
logger = loguru.logger

# Create a plot and save to a buffer
fig, 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 message
logger.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: Screenshot from 2024-05-02 11-43-40

@Delgan
Copy link
Owner

Delgan commented May 9, 2024

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.

@Delgan Delgan added the question Further information is requested label May 9, 2024
@tongshencmu
Copy link
Author

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.

logging_path = 'test.html'
with open(logging_path, "a") as f:
    f.write(f"<h1>Title </h1>")
    f.write("<h3>Image Input</h3>")
    f.write(f'<img src="data:image/png;base64,{base64_image}">')
    f.write("<h3>JSON format output: </h3>")
    f.write(f"<pre>{captioning}</pre>")
    f.write(
        '<hr style="height:2px;border-width:0;color:gray;background-color:gray">'
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants