How to use logging library in Python to create HTML log files with embedded plots?

I want to create HTML log files with embedded plots using the logging library in Python. For this purpose, I found a code that helps in creating and configuring a logger object that will write the log messages to an HTML file. It is using Matplotlib to create the plot and then embed it in the HTML log file and log messages, as usual, using the logger object created. In addition to logging messages, it can also include the plot in the log message.
Here is the code:

import logging
from logging.handlers import RotatingFileHandler
import matplotlib.pyplot as plt
import base64
from io import BytesIO
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# define a rotating file handler
handler = RotatingFileHandler('example.log', maxBytes=100000, 
                        backupCount=1)
handler.setLevel(logging.INFO)

# define a formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the handler to the logger
logger.addHandler(handler)
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')

# Convert plot to base64 string
buf = BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
plot_data = base64.b64encode(buf.read()).decode('utf8')
plt.close()

# Embed plot in HTML log message
plot_html = '<img src="data:image/png;base64,{}">'.format(plot_data)
logger.info(plot_html)
logger.info('This is a log message with an embedded plot: {}'
                    .format(plot_html))

Apart from the above code, I’m curious are there many other methods available for creating HTML log files with embedded plots using the logging library in Python? Can someone share any alternative code with me that helps me in logging messages in HTML files with embedded plots?

2 Likes

Hi, @safa The code you provided is a great example of how to use the logging library in Python to create HTML log files with embedded plots. It creates a logger object that writes log messages to an HTML file and includes a plot in the log message.

There are other ways to create HTML log files with embedded plots using the logging library in Python. One alternative method is to use the loguru library, which provides an easy-to-use interface for logging messages and supports adding rich data like plots and tables to the logs.

Here is an example code snippet that demonstrates how to use loguru to create an HTML log file with an embedded plot:

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))

In this example, loguru is used to create a logger object that writes log messages to an HTML file. The logger.debug method is used to log a message with an embedded plot. The plot argument contains an HTML img tag that displays the plot in the log message. I hope this helps!

2 Likes

Hello @safa, there are many alternatives to achieve the task you accomplished with your code. I faced a similar issue some time ago and used the following code to solve it:

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

# 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()

# Create the HTML log file with the embedded plot
timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
filename = f'example_{timestamp}.html'
with open(filename, 'w') as f:
    f.write(f'<html><head><title>Example log file with plot</title></head><body>')
    f.write(f'<h1>Example log file with plot</h1>')
    f.write(f'<p>This is a log message with an embedded plot:</p>')
    f.write(f'<img src="data:image/png;base64,{plot_data}">')
    f.write('</body></html>')

Here is how this code works:

  • It creates a plot using the matplotlib library and saves it to a buffer using a BytesIO object.
  • It then encodes the plot data as base64 and stores it in a variable.
  • After encoding, the code generates a timestamp for the log file name using datetime.datetime.now() and formats it appropriately.
  • Lastly, the code opens a file with the generated filename and writes the HTML code to it, including the embedded plot. The HTML code is written manually instead of using a library or formatter.

I hope this helps you, too!

1 Like

To create HTML log files with embedded plots using the logging library in Python, you can follow these steps:

  1. Set Up Logging Configuration: First, configure the logging library to output logs to both the console and a file. You can configure a FileHandler to write logs to an HTML file.
  2. Generate Plots: Generate the plots you want to embed in the HTML log file using a plotting library like Matplotlib or Plotly.
  3. Embed Plots in HTML: Convert the plots to HTML format and embed them in the log messages.

Here’s an example implementation:

import logging
from logging.handlers import RotatingFileHandler
import matplotlib.pyplot as plt
import base64
from io import BytesIO

# Configure logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(message)s')

# Create file handler for HTML log
html_handler = RotatingFileHandler('log.html', mode='a', maxBytes=5*1024*1024,
                                   backupCount=2, encoding=None, delay=0)
html_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
html_handler.setFormatter(formatter)

# Add HTML handler to the root logger
logging.getLogger('').addHandler(html_handler)

# Generate plot
def generate_plot():
    x = [1, 2, 3, 4, 5]
    y = [10, 20, 25, 30, 35]
    plt.plot(x, y)
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Sample Plot')

    # Save plot to BytesIO object
    buffer = BytesIO()
    plt.savefig(buffer, format='png')
    buffer.seek(0)
    image_png = buffer.getvalue()
    buffer.close()

    # Encode plot in base64
    image_base64 = base64.b64encode(image_png).decode('utf-8')
    return image_base64

# Embed plot in HTML log
plot_html = f'<img src="data:image/png;base64,{generate_plot()}" alt="Sample Plot">'
logging.info(f'Embedded Plot: {plot_html}')

In this example:

  • We set up logging to output to both the console and an HTML file (log.html) using a RotatingFileHandler.
  • We define a function generate_plot() to generate a sample plot using Matplotlib and convert it to a base64-encoded image.
  • The plot is embedded in HTML format using the <img> tag and logged as a message using logging.info().

This will create an HTML log file (log.html) with embedded plots whenever you run your Python script.

Here are the best HTML online learning platforms:

  1. W3School 2. Iqra Technology