Unreal Engine 4 - Custom Log Categories

Unreal Engine 4 - Custom Log Categories

Don't use LogTemp for everything!

It is trivial to setup a new log category in C++.

First, in a header in your project add a line like so:

DECLARE_LOG_CATEGORY_EXTERN(LogMyAwesomeGame, Log, All);

If this is for a specific class, I tend to put this in the header for that class. If this is a more general "whole game" log type, I put this in my module's header.

Similarly, in the CPP file:

DEFINE_LOG_CATEGORY(LogMyAwesomeGame);

And boom! Done!

Now you can use this category instead of LogTemp:

UE_LOG(LogMyAwesomeGame, Log, TEXT("Test Log Message"));

Advanced Usage

You may notice some extra arguments for DECLARE_LOG_CATEGORY_EXTERN, "Log" and "All".

Second Macro Argument

DECLARE_LOG_CATEGORY_EXTERN(LogMyAwesomeGame, Log, All);

The second argument (above is set to "Log") determines the default verbosity of the category. There are various verbosity levels in Unreal:

  • Fatal
  • Error
  • Warning
  • Display
  • Log
  • Verbose
  • VeryVerbose

These are in order, where if you set the verbosity to one of these settings, that level and all the above levels will be printed.

So if you have a fairly spammy system where you only want to logs in some cases (perhaps you're debugging a bug that requires more in-depth information), you can choose the default verbosity of your category to be something like "Warning". This means only Warning, Error, and Fatal log lines will be output.

The standard verbosity level is "Log", which will print everything except for Verbose and VeryVerbose.

The verbosity level can be overridden in the DefaultEngine.ini file in your Config/ folder. This is useful if you want to increase verbosity for a system locally (because you're debugging/developing it), without effecting builds or any other team members.

[Core.Log]
LogMyAwesomeGame=VeryVerbose

If you don't already have a "Core.Log" section in your DefaultEngine.ini file, you can add one. Add your category name and the verbosity level you want to have and next time you boot the editor, this will be the verbosity level for you.

Third Macro Argument

DECLARE_LOG_CATEGORY_EXTERN(LogMyAwesomeGame, Log, All);

The last argument of the macro above is "All". This is actually a shortcut to "VeryVerbose" (as of time of writing).

This argument determines the default verbosity level that will ever be compiled into the game. If this is set to anything above All, it will not be possible to change the verbosity level to any level below the level set here.

Why would you want this? If you have an incredibly verbose system that is logging a lot of data, it may be slowing down your project, even if it's set to VeryVerbose and you're not logging anything above Log/Warning! Just the act of deciding whether to print a log may be taking a long time, so this is a quick way to compile out all verbose log messages in one code change. Set this to something like Log to stop all the Verbose log lines being compiled at all, which may save your program some time running.

Unless you have knowingly added a lot of Verbose/VeryVerbose log lines with complex string formatting, I think it is very unlikely you will ever need/want to do this, as while this is slow, there are usually much slower aspects of a video game!!!