Retrieve log of flight using mavros

Hi,
I am working with python and ros and I am trying to retrieve the log of the flight from the code. I want to find a way to be reliable both for SITL and real drones.

I am using the mavros topics.
/mavros/log_transfer/raw/log_data
/mavros/log_transfer/raw/log_entry
and the services
/mavros/log_transfer/raw/log_request_data
/mavros/log_transfer/raw/log_request_end
/mavros/log_transfer/raw/log_request_list

However, I am not able to retrieve the log correctly.
As it is now, I ask the system the list of logs using /mavros/log_transfer/raw/log_request_list
then when the topic /mavros/log_transfer/raw/log_entry is updated I try to retrieve the data using a loop on the last log.

The code looks something like this:

def get_last_log(self):
    
    if self.log_req_list_srv(0,0): #0xffff for the last available'
        rospy.wait_for_message('mavros/log_transfer/raw/log_entry',
                                          LogEntry,
                                          10)
        rospy.logerr(f"Log Available: {self.log_entry}, {self.log_entry.num_logs}")
        log_name ="my_log.ulg"
        f = open(log_name, "wb")
        if self.log_entry.num_logs>0:
            rospy.loginfo("Retriving last log")
            step = 4096
            for j in range(0,self.log_entry.size,step):
                    request_log_data_successfully = self.log_req_data_srv(self.log_entry.last_log_num,j,step)
                    rospy.wait_for_message('mavros/log_transfer/raw/log_data',
                                            LogData,
                                            10)
                    f.write(self.log_data.data)
                    rospy.logerr(f"Log Data: {j} {request_log_data_successfully}, {self.log_data.data}")
        else:
            rospy.logerr(f"No logs available.add()")
    else:
        rospy.logerr(f"Unable to list logs")

    self.log_req_end_srv()
    f.close()
    rospy.logerr(f"Finished to retrieve log, saved in {log_name}")

However, the log is much smaller than the original one, and when I try to read the log or use it with some other log analyzer the file is corrupted.

So my question is, how can I retrieve the correct last log using mavros and python?

Any help is very appreciated,
Thank you in advance.

Sorry for the late reply. Did you consider using MAVSDK for this? Their log transfer example works out of the box: MAVSDK-Python/logfile_download.py at master · BeagleSystems/MAVSDK-Python · GitHub