thanks alot for bringing up this question, and for UE5, it can be done in a very easy way:
FStringOutputDevice OutputDevice;
FOutputDeviceRedirector::Get()->AddOutputDevice(&OutputDevice);
// call the log functions
FOutputDeviceRedirector::Get()->RemoveOutputDevice(&OutputDevice);
UE_LOG(LogTemp, Display, TEXT("the captured log is: %s"), *OutputDevice);