Skip to content

Commit 8444b82

Browse files
authored
feat(Unity): Structured Logging (#15786)
1 parent 2975d17 commit 8444b82

File tree

8 files changed

+208
-10
lines changed

8 files changed

+208
-10
lines changed

docs/platforms/unity/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Our Unity SDK builds on top of the [.NET SDK](/platforms/dotnet/) and extends it
2929
- [ViewHierarchy attachments](/platforms/unity/enriching-events/view-hierarchy/) for errors
3030
- [Offline Caching](/platforms/unity/configuration/options/#InitCacheFlushTimeout) stores event data to disk in case the device is not online
3131
- [Release Health](/platforms/unity/configuration/releases/) to keep track of crash-free users and sessions
32+
- [Structured Logging](/platforms/unity/logs/) to capture and send log messages with additional context
3233
- [Automatically adding breadcrumbs](/platforms/unity/enriching-events/breadcrumbs/#automatic-breadcrumbs) for
3334
- Unity's `Debug.Log` and `Debug.LogWarning`
3435
- Scene load, unload, active change
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Set Up Logs
3+
sidebar_title: Logs
4+
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
5+
sidebar_order: 5600
6+
---
7+
8+
With Sentry Structured Logs, you can send text-based log information from your Unity game to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
9+
10+
## Requirements
11+
12+
<PlatformContent includePath="logs/requirements" />
13+
14+
## Setup
15+
16+
<PlatformContent includePath="logs/setup" />
17+
18+
## Usage
19+
20+
<PlatformContent includePath="logs/usage" />
21+
22+
## Options
23+
24+
<PlatformContent includePath="logs/options" />
25+
26+
## Default Attributes
27+
28+
<PlatformContent includePath="logs/default-attributes" />
29+
30+
## Performance Considerations
31+
32+
- Logs are sent asynchronously to avoid impacting game performance
33+
- Consider disabling Debug level logs in production to avoid excessive log volume
34+
- Each severity level can be individually controlled to fine-tune what gets sent to Sentry
35+
- Before-log handlers are executed synchronously, so keep processing lightweight

docs/product/explore/logs/getting-started/index.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,21 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
297297

298298
### Gaming
299299

300-
- <LinkWithPlatformIcon
301-
platform="unreal"
302-
label="Unreal Engine"
303-
url="/platforms/unreal/logs/"
304-
/>
305300
- <LinkWithPlatformIcon
306301
platform="godot"
307302
label="Godot Engine"
308303
url="/platforms/godot/logs/"
309304
/>
305+
- <LinkWithPlatformIcon
306+
platform="unity"
307+
label="Unity"
308+
url="/platforms/unity/logs/"
309+
/>
310+
- <LinkWithPlatformIcon
311+
platform="unreal"
312+
label="Unreal Engine"
313+
url="/platforms/unreal/logs/"
314+
/>
310315

311316
## Upcoming SDKs
312317

@@ -317,11 +322,6 @@ We're actively working on adding Log functionality to additional SDKs. Check out
317322
label="Elixir"
318323
url="https://github.com/getsentry/sentry-elixir/issues/886"
319324
/>
320-
- <LinkWithPlatformIcon
321-
platform="unity"
322-
label="Unity"
323-
url="https://github.com/getsentry/sentry-unity/issues/2172"
324-
/>
325325

326326
If you don't see your platform listed above, please reach out to us on [GitHub](https://github.com/getsentry/sentry/discussions/86804) or [Discord](https://discord.gg/sentry), we'll get it prioritized!
327327

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The Sentry SDK for Unity automatically sets several default attributes on all log entries to provide context and improve debugging:
2+
3+
<Include name="logs/default-attributes/core" />
4+
5+
<Include name="logs/default-attributes/message-template" />
6+
7+
<Include name="logs/default-attributes/server" />
8+
9+
<Include name="logs/default-attributes/user" />
10+
11+
<Include name="logs/default-attributes/integration" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The following configuration options are available for Sentry Logs in Unity:
2+
3+
| Option | Description | Default |
4+
|--------|-------------|------------|
5+
| **Enable Structured Logging** | Master toggle for the structured logging feature | `false` |
6+
| **CaptureStructuredLogsForLogType[`LogType.Log`]** | Forward `Debug.Log` calls to Sentry | `false` |
7+
| **CaptureStructuredLogsForLogType[`LogType.Warning`]** | Forward `Debug.Warning` calls to Sentry | `true` |
8+
| **CaptureStructuredLogsForLogType[`LogType.Assert`]** | Forward `Debug.Assert` calls to Sentry | `true` |
9+
| **CaptureStructuredLogsForLogType[`LogType.Error`]** | Forward `Debug.Error` calls to Sentry | `true` |
10+
| **CaptureStructuredLogsForLogType[`LogType.Exception`]** | Forward `Debug.Exception` calls to Sentry | `true` |
11+
| **AddBreadcrumbsWithStructuredLogs** | Send `Debug` calls BOTH as Logs and as Breadcrumbs, instead of just Logs | `false` |
12+
| **Before Log Callback** | Handler to modify or filter log events before sending | None |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Logs for Unity are supported in Sentry SDK version `4.0.0` and above.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
To enable logging in your Unity game, you need to configure the Sentry SDK with structured logging enabled.
2+
3+
### Project Settings Configuration
4+
5+
1. Inside the editor open: **Tools > Sentry > Logging**
6+
2. Check the **Enable Structured Logging** option
7+
8+
### Programmatic Configuration
9+
10+
Alternatively, you can enable logging programmatically through the [configure callback](/platforms/unity/configuration/options/programmatic-configuration):
11+
12+
```csharp
13+
public override void Configure(SentryUnityOptions options)
14+
{
15+
options.EnableLogs = true;
16+
}
17+
```
18+
19+
or if you're manually initializing the SDK:
20+
21+
```csharp
22+
SentrySdk.Init(options =>
23+
{
24+
options.Dsn = "___PUBLIC_DSN___";
25+
26+
// Enable logs to be sent to Sentry
27+
options.EnableLogs = true;
28+
});
29+
```
30+
31+
### Advanced Configuration Options
32+
33+
In addition to enabling Structured Logging you can control the log capture behaviour through some additional options.
34+
35+
#### Automatic Unity Log Forwarding
36+
37+
You can configure the SDK to automatically forward Unity's Debug Logs to Sentry based on the enabled log level in the configuration window, or programmatically:
38+
39+
```csharp
40+
// Configure automatic log forwarding programmatically
41+
options.EnableLogs = true;
42+
43+
options.CaptureStructuredLogsForLogType[LogType.Log] = false;
44+
options.CaptureStructuredLogsForLogType[LogType.Warning] = true;
45+
options.CaptureStructuredLogsForLogType[LogType.Assert] = true;
46+
options.CaptureStructuredLogsForLogType[LogType.Error] = true;
47+
options.CaptureStructuredLogsForLogType[LogType.Exception] = true;
48+
49+
options.AddBreadcrumbsWithStructuredLogs = false; // Send as structured logs instead of breadcrumbs
50+
```
51+
52+
#### Before-Log Handler
53+
54+
To filter logs, or update them before they are sent to Sentry, you can provide a custom `BeforeSendLog` callback:
55+
56+
```csharp
57+
options.SetBeforeSendLog(log =>
58+
{
59+
if (log.Message.StartsWith("Sensitive:"))
60+
{
61+
return null;
62+
}
63+
64+
// Set a custom attribute for all other logs sent to Sentry
65+
log.SetAttribute("my.attribute", "value");
66+
67+
return log;
68+
});
69+
```
70+
71+
The callback function set via `SetBeforeSendLog(Func<SentryLog, SentryLog?>)` receives a log object, and should return the log object if you want it to be sent to Sentry, or `null` if you want to discard it.
72+
73+
The log object of type `SentryLog` has the following members:
74+
- `Timestamp` Property: (`DateTimeOffset`) The timestamp of the log.
75+
- `TraceId` Property: (`SentryId`) The trace id of the log.
76+
- `Level` Property: (`SentryLogLevel`) The severity level of the log. Either `Trace`, `Debug`, `Info`, `Warning`, `Error`, or `Fatal`.
77+
- `Message` Property: (`string`) The formatted log message.
78+
- `Template` Property: (`string?`) The parameterized template string.
79+
- `Parameters` Property: (`ImmutableArray<KeyValuePair<string, object>>`) The parameters to the template string.
80+
- `ParentSpanId` Property: (`SpanId?`) The span id of the span that was active when the log was collected.
81+
- `TryGetAttribute(string key, out object value)` Method: Gets the attribute value associated with the specified key. Returns `true` if the log contains an attribute with the specified key and it's value is not `null`, otherwise `false`.
82+
- `SetAttribute(string key, object value)` Method: Sets a key-value pair of data attached to the log. Supported types are `string`, `bool`, integers up to a size of 64-bit signed, and floating-point numbers up to a size of 64-bit.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Once the feature is enabled and the SDK is initialized, you can manually send logs using the `SentrySdk.Logger` API.
2+
3+
### Manual Logging
4+
5+
The `SentrySdk.Logger` instance exposes six methods that you can use to log messages at different log levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, and `Fatal`.
6+
7+
These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
8+
9+
```csharp
10+
SentrySdk.Logger.LogInfo("A simple debug log message");
11+
SentrySdk.Logger.LogError("A {0} log message", "formatted");
12+
```
13+
14+
### Automatic Logging Integration
15+
16+
Because Unity's `LogType` does not match `SentryLogLevel`, the SDKs logging integration maps them as follows:
17+
18+
| Unity LogLevel | Sentry Logs UI Severity |
19+
| --- | --- |
20+
| Debug | INFO |
21+
| Warning | WARN |
22+
| Assert | ERROR |
23+
| Error | ERROR |
24+
| Fatal | FATAL |
25+
26+
```csharp
27+
// Standard Unity logging - automatically captured when severity levels are enabled
28+
Debug.Log("Player position updated.");
29+
Debug.LogWarning("Low memory warning.");
30+
Debug.LogError("Failed to save game data.");
31+
```
32+
33+
You can configure whether these logs are sent as:
34+
- **Structured Logs**: Full log entries with searchable attributes
35+
- **Breadcrumbs**: Contextual information attached to errors (useful for debugging)
36+
37+
<Alert level="info">
38+
With **Structured Logs** enabled additional breadcrumb attachment to events needs to be explicitly enabled with `AddBreadcrumbsWithStructuredLogs = true`.
39+
</Alert>
40+
41+
## Capture with Attributes
42+
43+
```csharp
44+
SentrySdk.Logger.LogWarning(static log =>
45+
{
46+
log.SetAttribute("my.attribute", "value");
47+
}, "A log message with additional attributes.");
48+
```
49+
50+
Supported attribute types are:
51+
- Textual: `string` and `char`
52+
- Logical: `bool`
53+
- Integral: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `nint`
54+
- Floating-point: `float` and `double`
55+
56+
Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString()`.

0 commit comments

Comments
 (0)