PurgeLogsDays

PurgeLogsDays

#Overview

name: PurgeLogsDays

The value of this variable can be defined or overridden in .ini config files. 1 .ini config file referencing this setting variable.

It is referenced in 3 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of PurgeLogsDays is to control the automatic deletion of old log files in the Unreal Engine logging system. It determines how many days worth of log files should be kept before they are considered eligible for deletion.

This setting variable is primarily used by the Core module of Unreal Engine, specifically in the log file maintenance system. Based on the callsites, it’s utilized in both the main engine and the NetcodeUnitTest plugin.

The value of PurgeLogsDays is set in the engine configuration file (GEngineIni). It is read from the “LogFiles” section with the key “PurgeLogsDays”.

PurgeLogsDays interacts with another variable, MaxLogFilesOnDisk, which limits the total number of log files to keep. Together, these two variables control the log file cleanup process.

Developers must be aware that setting PurgeLogsDays to a value less than 0 (typically -1) will disable the automatic deletion of old log files based on their age. A value of 0 or greater will cause log files older than that many days to be deleted during the cleanup process.

Best practices when using this variable include:

  1. Set an appropriate value based on your project’s needs for historical log data and available disk space.
  2. Consider the relationship between PurgeLogsDays and MaxLogFilesOnDisk to ensure you’re not inadvertently deleting important logs.
  3. Regularly review and adjust these settings as your project grows to maintain optimal performance and disk usage.
  4. Be cautious when setting this to a low value in production environments where you might need to retain logs for a longer period for debugging or compliance reasons.
  5. Document any changes to this setting and communicate them to the development team to ensure everyone is aware of the log retention policy.

#Setting Variables

#References In INI files

Location: <Workspace>/Engine/Config/BaseEngine.ini:2028, section: [LogFiles]

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Plugins/NetcodeUnitTest/NetcodeUnitTest/Source/NetcodeUnitTest/Private/UnitTestManager.cpp:161

Scope (from outer to inner):

file
function     void UUnitTestManager::InitializeLogs
class        class FUnitLogPurger : public IPlatformFile::FDirectoryVisitor

Source code excerpt:

		class FUnitLogPurger : public IPlatformFile::FDirectoryVisitor
		{
			int32 PurgeLogsDays;
			int32 MaxLogFilesOnDisk;
			IFileManager& FM;

			TMap<FString, FDateTime> DirList;

		public:
			FUnitLogPurger()
				: PurgeLogsDays(INDEX_NONE)
				, MaxLogFilesOnDisk(INDEX_NONE)
				, FM(IFileManager::Get())
			{
				GConfig->GetInt(TEXT("LogFiles"), TEXT("PurgeLogsDays"), PurgeLogsDays, GEngineIni);
				GConfig->GetInt(TEXT("LogFiles"), TEXT("MaxLogFilesOnDisk"), MaxLogFilesOnDisk, GEngineIni);
			}

			void ScanAndPurge()
			{
				if (PurgeLogsDays != INDEX_NONE || MaxLogFilesOnDisk != INDEX_NONE)
				{
					FM.IterateDirectory(*FPaths::ProjectLogDir(), *this);

					DirList.ValueSort(TLess<FDateTime>());

					// First purge directories older than a certain date
					if (PurgeLogsDays != INDEX_NONE)
					{
						for (TMap<FString, FDateTime>::TIterator It(DirList); It; ++It)
						{
							if ((FDateTime::Now() -  It.Value()).GetDays() > PurgeLogsDays)
							{
								UE_LOG(LogUnitTest, Log, TEXT("Deleting old unit test log directory: %s"), *It.Key());

								FM.DeleteDirectory(*It.Key(), true, true);

								It.RemoveCurrent();

#Loc: <Workspace>/Engine/Source/Runtime/Core/Private/Misc/FileHelper.cpp:1153

Scope (from outer to inner):

file
function     void FMaintenance::DeleteOldLogs

Source code excerpt:

{
	SCOPED_BOOT_TIMING("FMaintenance::DeleteOldLogs");
	int32 PurgeLogsDays = -1; // -1 means don't delete old files
	int32 MaxLogFilesOnDisk = -1; // -1 means keep all files

	GConfig->GetInt(TEXT("LogFiles"), TEXT("PurgeLogsDays"), PurgeLogsDays, GEngineIni);
	GConfig->GetInt(TEXT("LogFiles"), TEXT("MaxLogFilesOnDisk"), MaxLogFilesOnDisk, GEngineIni);

	if (PurgeLogsDays >= 0 || MaxLogFilesOnDisk >= 0)
	{
		// get list of files in the log directory (grouped by log name)
		TMap<FString, TArray<FString>> LogToPaths;
		{
			TArray<FString> Files;
			IFileManager::Get().FindFiles(Files, *FString::Printf(TEXT("%s*.*"), *FPaths::ProjectLogDir()), true, false);

#Loc: <Workspace>/Engine/Source/Runtime/Core/Private/Misc/FileHelper.cpp:1181

Scope (from outer to inner):

file
function     void FMaintenance::DeleteOldLogs

Source code excerpt:


		// delete old log files in each group
		double MaxFileAgeSeconds = 60.0 * 60.0 * 24.0 * double(PurgeLogsDays);

		struct FSortByDateNewestFirst
		{
			bool operator()(const FString& A, const FString& B) const
			{
				const FDateTime TimestampA = IFileManager::Get().GetTimeStamp(*A);