Set Up Logs
Structured logs allow you to send, view and query logs sent from your applications within Sentry.
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
Logs for Apple platforms are supported in Sentry Cocoa SDK version 8.55.0
and above. Logs are still experimental and the API may change in the future.
To enable logging, you need to initialize the SDK with the experimental enableLogs
option set to true
.
import Sentry
SentrySDK.start { options in
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
// Enable logs to be sent to Sentry
options.experimental.enableLogs = true
}
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the Sentry.logger
APIs.
The Sentry.logger
namespace exposes six methods that you can use to log messages at different log levels: trace
, debug
, info
, warn
, error
, and fatal
. Supported types for attributes are Strings, Int, Double, and Bool.
import Sentry
let logger = SentrySDK.logger
// Log messages without attributes
logger.trace("Starting database connection")
logger.debug("Cache miss for user")
logger.info("Updated profile")
// Log messages with attributes
logger.trace("Starting database connection", attributes: ["database": "users"])
logger.debug("Cache miss for user", attributes: ["userId": 123])
logger.info("Updated profile", attributes: ["profileId": 345])
logger.warn("Rate limit reached for endpoint", attributes: [
"endpoint": "/api/results/",
"isEnterprise": false
])
logger.error("Failed to process payment", attributes: [
"orderId": "order_123",
"amount": 99.99
])
logger.fatal("Database connection pool exhausted", attributes: [
"database": "users",
"activeConnections": 100
])
Swift supports automatic extraction of interpolated values as attributes. When you use string interpolation in your log messages, supported types (Strings, Int, Double, and Bool) are automatically extracted and added as attributes with the key format sentry.message.parameter.{index}
.
let userId = "user_123"
let orderCount = 5
let isPremium = true
let totalAmount = 99.99
// String interpolation automatically extracts values as attributes
logger.info("User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)")
// This is equivalent to manually specifying attributes:
let message = "User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)"
logger.info(
message,
attributes: [
"sentry.message.template": "User {0} placed {1} orders, premium: {2}, total: {3}",
"sentry.message.parameter.0": userId,
"sentry.message.parameter.1": orderCount,
"sentry.message.parameter.2": isPremium,
"sentry.message.parameter.3": totalAmount
]
)
To filter logs, or update them before they are sent to Sentry, you can use the beforeSendLog
option.
import Sentry
SentrySDK.start { options in
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
options.experimental.enableLogs = true
options.beforeSendLog = { log in
if log.level == .info {
// Filter out all info logs
return nil
}
return log
}
}
The beforeSendLog
function receives a log object, and should return the log object if you want it to be sent to Sentry, or nil
if you want to discard it.
The log object has the following properties:
level
: The log level (trace, debug, info, warn, error, fatal)message
: The message to be loggedtimestamp
: The timestamp of the logattributes
: The attributes of the log
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").