How to subscribe to/redirect log messages?

How to (in correct way) “hook” into UE4 logging system, to redirect log messages output to somewhere else? E.g. I have a custom module, which by I would to capture every UE_LOG call message, for further purpose.

Which part of engine (source code) will be good place to change for such log message capture? What is the proper way (stable and side-effect resistant) to do this?

I was looking for the same thing: FOutputDeviceRedirector::Get()->AddOutputDevice(…); should do the trick. You would need to implement your own OutputDevice

I’m having trouble with this. I can’t seem to get it to redirect anywhere. Not to a different file or an FString. Any suggestions on how to make the FOutputDevice?

You need to add FOutputDevice as base class, like you would do that with interface, Look at UConsole as a example

If you wanted to ‘redirect’ the output to a string you could do it as follows:

class StringOutputDevice : public FOutputDevice
{
public:
	StringOutputDevice()
	{
		check(GLog);
		GLog->AddOutputDevice(this);
	}

	~StringOutputDevice()
	{
		if (GLog != nullptr)
		{
			GLog->RemoveOutputDevice(this);
		}
	}

protected:
	virtual void Serialize(const TCHAR* Message, ELogVerbosity::Type Verbosity, const class FName& Category) override
	{
		MyString += Message;
	}

private:
	FString MyString;
};

Please note that the logs will still end up in the normal places as well. You are just adding an output device to which it will also be sent.

I got it to work. Thanks for your help!

Can someone explain the full solution? I pretty much did what you posted here but it seems like my call to GLog->AddOutputDevice(this); is not working. I made a post about it here

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