Skip to content

Add attributes support for structured logs#1190

Merged
tustanivsky merged 41 commits intomainfrom
feat/log-attributes
Jan 16, 2026
Merged

Add attributes support for structured logs#1190
tustanivsky merged 41 commits intomainfrom
feat/log-attributes

Conversation

@tustanivsky
Copy link
Collaborator

@tustanivsky tustanivsky commented Jan 12, 2026

Overview

This PR adds support for structured log attributes to Sentry's logging system enabling developers to attach metadata to individual log entries. Attributes are key-value pairs that can be used to add contextual information to logs beyond just the message body, log category and severity level.

It is now possible to capture logs with a given set of attributes (provided as a TMap<FString, FSentryVariant>) and to get or set individual log item attributes in the BeforeLog handler. Also, attributes can be set globally, meaning they will be applied to all captured logs.

Attributes support all FSentryVariant types:

  • Primitives: String, Integer (int32), Float, Boolean
  • Complex types: Array, Map are converted to JSON strings

Separate LogXXXWithAttributes methods were added to USentrySubsystem instead of extending the existing LogXXX methods, because UFUNCTION's parameters of type TMap cannot have default values. Adding them would therefore introduce a breaking API change which this approach avoids.

Closes #1163

Usage examples

Add a log with attributes:

TMap<FString, FSentryVariant> Attributes;
Attributes.Add(TEXT("user_id"), FSentryVariant(TEXT("12345")));
Attributes.Add(TEXT("request_id"), FSentryVariant(TEXT("abc-def-ghi")));
Attributes.Add(TEXT("retry_count"), FSentryVariant(3));
Attributes.Add(TEXT("success"), FSentryVariant(false));

USentrySubsystem* Sentry = GEngine->GetEngineSubsystem<USentrySubsystem>();
Sentry->LogErrorWithAttributes(TEXT("Payment processing failed"), Attributes, TEXT("Payment"));

Manipulate attributes in BeforeLog Handler

USentryLog* MyBeforeLogHandler::HandleBeforeLog(USentryLog* LogData)
{
    // Read existing attributes
    FSentryVariant UserId = LogData->GetAttribute(TEXT("user_id"));

    // Add or modify attributes
    LogData->SetAttribute(TEXT("environment"), FSentryVariant(TEXT("production")));
    LogData->SetAttribute(TEXT("server_name"), FSentryVariant(TEXT("game-server-01")));

    // Remove sensitive attributes
    LogData->RemoveAttribute(TEXT("password"));

    return LogData;
}

Set global attributes:

// Get the Sentry subsystem                                                                                                                                                                                      
USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>();                                                                                                                             
                                                                                                                                                                                                                 
// Set string attribute                                                                                                                                                                                          
SentrySubsystem->SetAttribute(TEXT("user.tier"), FSentryVariant(TEXT("premium")));                                                                                                                               
                                                                                                                                                                                                                 
// Set numeric attributes                                                                                                                                                                                        
SentrySubsystem->SetAttribute(TEXT("session.duration"), FSentryVariant(1234));                                                                                                                                   
SentrySubsystem->SetAttribute(TEXT("player.health"), FSentryVariant(85.5f));                                                                                                                                     
                                                                                                                                                                                                                 
// Set boolean attribute                                                                                                                                                                                         
SentrySubsystem->SetAttribute(TEXT("feature.enabled"), FSentryVariant(true));                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                 
// Remove an attribute when no longer needed                                                                                                                                                                     
SentrySubsystem->RemoveAttribute(TEXT("session.duration")); 

Other

Logging inside FSentryVariant was removed to prevent infinite recursion that could crash the SDK. This could previously happen if variants were misused in a custom BeforeLog handler (e.g. when attempting to get value of the non-existent attribute retrieved using USentryLog::GetAttribute).

To align SDK behavior across platforms, the Flush function is invoked internally on Android and Apple platforms to ensure all logs are flushed during app shutdown.

Testing

Newly added attributes API is covered with unit and integration tests.

Depends on:

Documentation

@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2026

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 5069650

@tustanivsky tustanivsky marked this pull request as ready for review January 12, 2026 16:33
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@tustanivsky tustanivsky merged commit 00b68a6 into main Jan 16, 2026
108 of 109 checks passed
@tustanivsky tustanivsky deleted the feat/log-attributes branch January 16, 2026 13:19
tustanivsky added a commit to getsentry/sentry-docs that referenced this pull request Jan 16, 2026
This PR documents the new structured logging attributes API for Unreal
SDK:
- `LogXxxWithAttributes()` methods for attaching custom attributes to
individual logs
- Global attributes via `SetAttribute()`/`RemoveAttribute()` (with
Android limitation note)
- Attribute manipulation methods on `USentryLog` for use in
`BeforeLogHandler`

Also fixes incorrect `AddLog()` references to use the actual public API
(`LogDebug()`, `LogInfo()`, etc.).

Related items:
- getsentry/sentry-unreal#1190
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add custom log attributes API

3 participants