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:127.0.0.1:14550'  
master = mavutil.mavlink_connection(connection_string)

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

try:
    while True:

        msg = master.recv_match()

        if not msg:
            continue

        buf = msg.get_msgbuf()
        tlog.write(buf)

        print(msg)

except KeyboardInterrupt:
   
    tlog.close()
    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 discuss.ardupilot.org. That’s where tlogs and pymavlink are usually used.