|
| 1 | +To enable logging in your Unreal Engine project, you need to configure the Sentry SDK with structured logging enabled. |
| 2 | + |
| 3 | +### Project Settings Configuration |
| 4 | + |
| 5 | +1. Open your project settings: **Project Settings > Plugins > Sentry** |
| 6 | +2. Check the **Enable Structured Logging** option |
| 7 | + |
| 8 | +### Programmatic Configuration |
| 9 | + |
| 10 | +Alternatively, you can enable logging programmatically when initializing the SDK: |
| 11 | + |
| 12 | +```cpp |
| 13 | +#include "SentrySubsystem.h" |
| 14 | + |
| 15 | +void ConfigureSentryWithLogs() |
| 16 | +{ |
| 17 | + USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>(); |
| 18 | + |
| 19 | + // Create settings with logging enabled |
| 20 | + SentrySubsystem->InitializeWithSettings(FConfigureSettingsNativeDelegate::CreateLambda([=](USentrySettings* Settings) |
| 21 | + { |
| 22 | + Settings->EnableStructuredLogging = true; |
| 23 | + })); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +### Advanced Configuration Options |
| 28 | + |
| 29 | +#### Automatic Unreal Engine Log Forwarding |
| 30 | + |
| 31 | +You can configure the SDK to automatically forward Unreal Engine's native `UE_LOG` calls to Sentry based on the enabled severity levels: |
| 32 | + |
| 33 | +```cpp |
| 34 | +USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>(); |
| 35 | + |
| 36 | +// Configure automatic log forwarding programmatically |
| 37 | +SentrySubsystem->InitializeWithSettings(FConfigureSettingsNativeDelegate::CreateLambda([=](USentrySettings* Settings) |
| 38 | +{ |
| 39 | + Settings->EnableStructuredLogging = true; |
| 40 | + |
| 41 | + // Enable specific severity levels for UE_LOG forwarding |
| 42 | + Settings->EnableDebugLogs = false; |
| 43 | + Settings->EnableInfoLogs = true; |
| 44 | + Settings->EnableWarningLogs = true; |
| 45 | + Settings->EnableErrorLogs = true; |
| 46 | + Settings->EnableFatalLogs = true; |
| 47 | + |
| 48 | + Settings->bSendBreadcrumbsWithStructuredLogging = false; // Send as structured logs instead of breadcrumbs |
| 49 | +})); |
| 50 | +``` |
| 51 | +
|
| 52 | +#### Log Category Filtering |
| 53 | +
|
| 54 | +You can filter which log categories are sent to Sentry: |
| 55 | +
|
| 56 | +```cpp |
| 57 | +// Configure category filtering |
| 58 | +USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>(); |
| 59 | + |
| 60 | +// Create settings with logging enabled |
| 61 | +SentrySubsystem->InitializeWithSettings(FConfigureSettingsNativeDelegate::CreateLambda([=](USentrySettings* Settings) |
| 62 | +{ |
| 63 | + Settings->EnableStructuredLogging = true; |
| 64 | + |
| 65 | + // Only forward logs from specific categories |
| 66 | + TArray<FString> AllowedCategories; |
| 67 | + AllowedCategories.Add(TEXT("LogGameFlow")); |
| 68 | + AllowedCategories.Add(TEXT("LogPlayerSystem")); |
| 69 | + AllowedCategories.Add(TEXT("LogSentrySdk")); |
| 70 | + Settings->LogCategoryFilter = AllowedCategories; |
| 71 | + |
| 72 | +})); |
| 73 | +``` |
| 74 | +
|
| 75 | +#### Before-Log Handler |
| 76 | +
|
| 77 | +To filter logs, or update them before they are sent to Sentry, you can create a custom before-log handler class. |
| 78 | +
|
| 79 | +<Alert level="info"> |
| 80 | + Logging additional messages in the BeforeLog handler can cause recursive call of the handler, resulting in a stack overflow! |
| 81 | +</Alert> |
| 82 | +
|
| 83 | +```cpp |
| 84 | +UCLASS() |
| 85 | +class UCustomLogFilter : public USentryBeforeLogHandler |
| 86 | +{ |
| 87 | + GENERATED_BODY() |
| 88 | +public: |
| 89 | + virtual USentryLogEvent* HandleBeforeLog_Implementation(USentryLogEvent* LogEvent) override |
| 90 | + { |
| 91 | + // Filter out all debug logs |
| 92 | + if (LogEvent->GetLevel() == ESentryLevel::Debug) |
| 93 | + { |
| 94 | + return nullptr; // Return null to prevent sending |
| 95 | + } |
| 96 | + |
| 97 | + // Filter out logs based on message content |
| 98 | + if (LogEvent->GetBody().Contains(TEXT("Sensitive"))) |
| 99 | + { |
| 100 | + return nullptr; // Filter out sensitive logs |
| 101 | + } |
| 102 | + |
| 103 | + // Filter based on specific categories |
| 104 | + if (LogEvent->GetBody().Contains(TEXT("Password")) || |
| 105 | + LogEvent->GetBody().Contains(TEXT("Token"))) |
| 106 | + { |
| 107 | + return nullptr; // Filter out authentication-related logs |
| 108 | + } |
| 109 | + |
| 110 | + return LogEvent; // Return modified event |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +// Configure settings using delegate |
| 115 | +FConfigureSettingsDelegate SettingsDelegate; |
| 116 | +SettingsDelegate.BindDynamic(this, &USomeClass::HandleSettingsDelegate); |
| 117 | + |
| 118 | +void USomeClass::HandleSettingsDelegate(USentrySettings* Settings) |
| 119 | +{ |
| 120 | + // Enable structured logging |
| 121 | + Settings->EnableStructuredLogging = true; |
| 122 | + |
| 123 | + // Configure individual severity levels |
| 124 | + Settings->EnableDebugLogs = false; |
| 125 | + Settings->EnableInfoLogs = true; |
| 126 | + Settings->EnableWarningLogs = true; |
| 127 | + Settings->EnableErrorLogs = true; |
| 128 | + Settings->EnableFatalLogs = true; |
| 129 | + |
| 130 | + // Set custom before-log handler |
| 131 | + Settings->BeforeLogCallback = UCustomLogFilter::StaticClass(); |
| 132 | +} |
| 133 | + |
| 134 | +// Initialize with settings delegate |
| 135 | +USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>(); |
| 136 | +SentrySubsystem->InitializeWithSettings(SettingsDelegate); |
| 137 | +``` |
| 138 | +
|
| 139 | +The `HandleBeforeLog_Implementation` method receives a `USentryLog` object, and should return the log event if you want it to be sent to Sentry, or `nullptr` if you want to discard it. |
| 140 | +
|
| 141 | +The `USentryLog` object has the following methods: |
| 142 | +- `GetLevel()`: Returns the severity level of the log (`ESentryLevel`) |
| 143 | +- `GetBody()`: Returns the formatted log message (`FString`) |
| 144 | +- `SetLevel(ESentryLevel Level)`: Sets the Level of the Log Event |
| 145 | +- `SetBody(FString& Body)`: Sets the Body of the Log Event |
0 commit comments