UE5 C++ Logging

UE5 C++ Logging

Unreal Engine Custom Logger & UE_LOGFMT

📝 Logging in UE5

In this UE5 C++ logging post, we see a few ways for logging in Unreal Engine 5, including UE_LOGFMT. UE_LOGFMT provides modern string interpolation, offered in std::format, std::print and libraries like fmtlib and spdlog.

As well as that, we see a way for customizing your Unreal log messages, included in your own C++ source files. This can help to filter log messages generated by your code from the engine’s own code, speeding up your debugging process.

If that’s what you were looking for, let’s crack on. Please get in touch with any feedback on the post, especially if it is possible to make any aspect clearer.

🖥️ Main Logging Methods in UE5 C++ Code

Before Unreal Engine 5.2, you were limited to:

  • GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("Hello Everybody!"));; and

  • UE_LOG(LogTemp, Warning, TEXT("Hello Everybody!"));.

The TEXT macro, above, provides C printf style formatting. Modern C++ style guides prefer C++ approaches over printf though; printf can introduce safety issues and does not support user-defined types. A good solution (working in older versions of Unreal Engine) is to use std::format (added in C++20) or alternatively, std::print (added in C++23) or a third-party modern formatting library like fmtlib.

If you are using Unreal Engine 5.2 or newer, you can use UE_LOGFMT, with string interpolation, which we see next.

🛸 Modern UE5 Logging with UE_LOGFMT

Unreal Engine 5.2 and newer offer UE_LOGFMT is available, which has an arguably cleaner API for logging and provides with string interpolation.

Here is a before and after comparison, to see how you can update code to use the new API:

Before (UE_LOG)

UE_LOG(LogTemp, Warning, TEXT("Hello Everybody!"));

After (UE_LOGFMT)

UE_LOGFMT(LogCore, Warning, "Loading `{Name}` failed with error {Error}", Package->GetName(),  ErrorCode);

This will look familiar if you have used any of the string formatting alternatives mentioned above. To sue this new logging macro, just add the following header to source files you use UE_LOGFMT in.

 #include "Logging/StructuredLog.h"

As an alternative, add this header to the project CPP file, like we do below with custom logging.

🪚 Custom UE5 C++ Logging

Moving on, custom logging, is relatively easy to set up. The advantage of using it, is that you can quickly filter Unreal Engine log messages from your own modules.

To start, create a new CustomLogging.h header file in your project code under YourProject/Source/YourProject/Private/. Create the Private directory if the project does not already have one. Add this code to the new file:

#pragma once

#include "CoreMinimal.h"
#include "Logging/StructuredLog.h"

/* Custom log category */
DECLARE_LOG_CATEGORY_EXTERN(YourCustomLogName, Log, All);

Replace YourCustomLogName with whatever you want to appear in the logs against the custom messages. The second and third parameters here are the default severity and which levels of severity to include.

Including Logging/StructuredLog.h (in line 4) will give you access to UE_LOGFMT when you include this header file in other source files.

Making Custom Logs Available to the Engine

Next, open the project file (YourProject/Source/YourProject/YourProject.cpp), and include your new header and the extra line at the bottom here:

// Copyright Epic Games, Inc. All Rights Reserved.

#include "YourProject.h"
#include "CustomLogging.h"
#include "Modules/ModuleManager.h"

IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, YourProject, "YourProject" );

DEFINE_LOG_CATEGORY(YourCustomLogName);

You might need to refresh the project in the Unreal Engine Editor, for it to pick up the new header. You can now use custom logs, with UE_LOG or UE_LOGFMT in your C++ source files.

// ...TRUNCATED

#include "CustomLogging.h"

// TRUNCATED...

Foo::Foo() {
  UE_LOGFMT(YourCustomLogName, Warning, "Made it here!");
}

🙌🏽 UE5 C++ Logging: Wrapping Up

In this UE5 C++ logging post, we had a look at the options for logging from your C++ code. More specifically, we saw:

  • the new UE_LOGFMT macro in action;

  • **alternatives to UE_LOG string interpolation **for working with older versions of Unreal Engine; and

  • how you can create custom logs in Unreal Engine, making filtering your own code’s message simpler.

I hope you found this useful. Do let me know if you would like to see more similar content. Also reach out if there is anything I could improve to provide a better experience for you.

🙏🏽 UE5 C++ Logging: Feedback

If you have found this post useful, see links below for further related content on this site. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.

Did you find this article valuable?

Support Rodney Lab - Content Site WebDev & Serverless by becoming a sponsor. Any amount is appreciated!