|
| 1 | +Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `Sentry.logger` APIs. |
| 2 | + |
| 3 | +The `logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warning`, `error`, and `fatal`. |
| 4 | + |
| 5 | +You can pass additional attributes directly to the logging functions. 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. |
| 6 | + |
| 7 | +```ruby |
| 8 | +Sentry.logger.info("Updated global cache") |
| 9 | + |
| 10 | +Sentry.logger.debug("Cache miss for user %{user_id}", user_id: 123) |
| 11 | + |
| 12 | +Sentry.logger.trace( |
| 13 | + "Starting database connection %{database}", |
| 14 | + database: "users" |
| 15 | +) |
| 16 | + |
| 17 | +Sentry.logger.warn( |
| 18 | + "Rate limit reached for endpoint %{endpoint}", |
| 19 | + endpoint: "/api/results/" |
| 20 | +) |
| 21 | + |
| 22 | +Sentry.logger.error( |
| 23 | + "Failed to process payment. Order: %{order_id}. Amount: %{amount}", |
| 24 | + order_id: "or_2342", amount: 99.99 |
| 25 | +) |
| 26 | + |
| 27 | +Sentry.logger.fatal( |
| 28 | + "Database %{database} connection pool exhausted", |
| 29 | + database: "users" |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +You can also use message templates with positional or hash parameters: |
| 34 | + |
| 35 | +```ruby |
| 36 | +# Using named parameters |
| 37 | +Sentry.logger.info("User %{name} logged in", name: "Jane Doe") |
| 38 | + |
| 39 | +# Using positional parameters |
| 40 | +Sentry.logger.info("User %s logged in", ["Jane Doe"]) |
| 41 | +``` |
| 42 | + |
| 43 | +Any other arbitrary attributes will be sent as part of the log event payload: |
| 44 | + |
| 45 | +```ruby |
| 46 | +# Here `user_id` and `action` will be sent as extra attributes that |
| 47 | +# Sentry Logs UI displays |
| 48 | +Sentry.logger.info( |
| 49 | + "User %{user} logged in", |
| 50 | + user: "Jane", user_id: 123, action: "create" |
| 51 | +) |
| 52 | +``` |
0 commit comments