UE5 C++ Logging: Unreal Engine Custom Logger & UE_LOGFMT
Tags: cpp, gamedev, unrealengine
Published: 2024-04-01 12:26:54

---
title: "UE5 C++ Logging: Unreal Engine Custom Logger & UE_LOGFMT"
published: "true"
description: "UE5 C++ Logging: creating custom logs for easier filtering in Unreal Engine 5 and using UE_LOGFMT as a replacement for printf interpolation."
tags: "cplusplus, gamedev, unrealengine"
canonical_url: "https://rodneylab.com/ue5-c++-logging/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skao96b6e7tzwa963kq3.png"
---

## 📝 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&rsquo;s own code, speeding up your debugging process.

If that&rsquo;s what you were looking for, let&rsquo;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 <a href="https://en.cppreference.com/w/c/io/fprintf">C `printf` style formatting</a>.  Modern C++ style guides <a href="https://google.github.io/styleguide/cppguide.html#Streams">prefer C++ approaches over `printf`</a> though; `printf` can <a href="https://github.com/fmtlib/fmt#printf">introduce safety issues</a> 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 <a href="https://rodneylab.com/c++-game-utility-libraries/#fmtlib">third-party modern formatting library like fmtlib</a>.

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`

<a href="https://docs.unrealengine.com/5.3/en-US/logging-in-unreal-engine/?">Unreal Engine 5.2 and newer offer `UE_LOGFMT` is available</a>, 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 (<code>UE_LOG</code>)**

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

**After (<code>UE_LOGFMT</code>)**

```cplusplus
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.

```cplusplus
 #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:

```cplusplus
#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:

```cplusplus
// 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.

```cplusplus
// ...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 **<code>UE_LOGFMT</code> macro** in action;
- **alternatives to <code>UE_LOG</code> 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&rsquo;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 <a aria-label="Support Rodney Lab via Buy me a Coffee" href="/giving/">consider supporting me through Buy me a Coffee</a>.

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 <a href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</a> on X (previously Twitter) and also, join the <a href="https://matrix.to/#/%23rodney:matrix.org">#rodney</a> Element Matrix room. Also, see <a aria-label="Get in touch with Rodney Lab" href="/contact/">further ways to get in touch with Rodney Lab</a>. I post regularly on <a href="/tags/gaming/">Game Dev</a> as well as <a href="/tags/rust/">Rust</a> and <a href="/tags/c++/">C++</a> (among other topics). Also, <a aria-label="Subscribe to the   Rodney Lab newsletter" href="https://newsletter.rodneylab.com/issue/latest-issue">subscribe to the newsletter to keep up-to-date</a> with our latest projects.