#unreal

Related: Editor Notifications

Getting object names

You often need to get an object’s name when you’re writing logs to print more useful information.

  • AActor::GetDebugName(actor) returns a name for Actors. This works with nullptr values so you don’t need to check.
  • GetNameSafe(uobject) is similar as above, but works for any UObject type. This function also works with nullptr values.
  • UKismetSystemLibrary::GetDisplayName(object) works similar to GetNameSafe, but this is probably mostly intended to be used from blueprints.

Custom log categories

You can define custom logging categories which can be used both with regular logging macros, and visual logging macros.

  • DECLARE_LOG_CATEGORY_EXTERN with DEFINE_LOG_CATEGORY for logging categories shared across multiple files
  • DEFINE_LOG_CATEGORY_STATIC for log categories used in a single .cpp file
  • DECLARE_LOG_CATEGORY_CLASS with DEFINE_LOG_CATEGORY_CLASS for log categories used within a single class

Log categories used in multiple files

You can use DECLARE_LOG_CATEGORY_EXTERN to declare logging categories which can be also included within other files. For example, define a LogMyGame category in your game’s module file.

DECLARE_LOG_CATEGORY_EXTERN(LogMyGame, Log, All);

Then, in your module’s .cpp file, define it using

DEFINE_LOG_CATEGORY(LogMyGame);

Parameters

The first parameter is the name of the logging category. This should be prefixed with Log in the format of LogSomeCategoryName

The second parameter defines the most verbose logging level that will be displayed by default. This setting can be overridden using DefaultEngine.ini

The third parameter defines the most verbose logging level that gets compiled. This means if you set it to Error, everything that is more verbose will be compiled out of the game, and will not display, ever. You can set this to All and NoLogging to enable all and no verbosity levels respectively.

Setting compile time logging to a low verbosity is mainly useful if you have verbose logging which gathers a lot of data, and is causing performance issues. All logging below the compile time verbosity will be completely compiled out of the game, so it’s a code-level method to quickly turn off some logs.

Log categories in single files

If you only want the category used in one file, use

DEFINE_LOG_CATEGORY_STATIC(LogMyCategory, Log, Log);

The parameters are the same as for DECLARE_LOG_CATEGORY_EXTERN

Class log categories

This is mostly useful if your class spans multiple files. Otherwise you can just use DEFINE_LOG_CATEGORY_STATIC and achieve the same result.

Use DECLARE_LOG_CATEGORY_CLASS in header and DEFINE_LOG_CATEGORY_CLASS in cpp file. Parameters are similar to before.

Overriding logging levels

Place into DefaultEngine.ini

[Core.Log]
LogMyLogCategoryName=VeryVerbose