How to generate .tlog file using pymavlink for connected drone?

I’m trying to create a program to generate .tlog file using pymavlink.

Here is the program which I’m currently running:

import time
from pymavlink import mavutil

connection_string = 'tcp:'  
master = mavutil.mavlink_connection(connection_string)

tlog_file_path = 'telemetry_log.tlog'
tlog = mavutil.mavlogfile(tlog_file_path, write=True)

    while True:

        msg = master.recv_match()

        if not msg:

        buf = msg.get_msgbuf()


except KeyboardInterrupt:
    print("\nTelemetry logging stopped. TLog file saved at:", tlog_file_path)

But I’m not able to read or run the generated .tlog file by this code properly by using pymavlink or mission planner.

Can anyone help me.

I’d consider asking in That’s where tlogs and pymavlink are usually used.