You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- Use this checklist to make sure your PR is ready for merge. You may
delete any sections you don't need. -->
## DESCRIBE YOUR PR
Improves the flow and adds info about `before_send_log` option in the
Rails' `Logs` section. I tried to make it flow similarly to how we have
Laravel structured.
---
Fixesgetsentry/sentry-ruby#2738
## Preview
https://sentry-docs-git-rails-improve-logs-docs.sentry.dev/platforms/ruby/guides/rails/logs/
## IS YOUR CHANGE URGENT?
Help us prioritize incoming PRs by letting us know when the change needs
to go live.
- [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE -->
- [ ] Other deadline: <!-- ENTER DATE HERE -->
- [x] None: Not urgent, can wait up to 1 week+
## SLA
- Teamwork makes the dream work, so please add a reviewer to your PRs.
- Please give the docs team up to 1 week to review your PR unless you've
added an urgent due date to it.
Thanks in advance for your help!
## PRE-MERGE CHECKLIST
*Make sure you've checked the following before merging your changes:*
- [ ] Checked Vercel preview for correctness, including links
- [ ] PR was reviewed and approved by any necessary SMEs (subject matter
experts)
- [ ] PR was reviewed and approved by a member of the [Sentry docs
team](https://github.com/orgs/getsentry/teams/docs)
## LEGAL BOILERPLATE
<!-- Sentry employees and contractors can delete or ignore this section.
-->
Look, I get it. The entity doing business as "Sentry" was incorporated
in the State of Delaware in 2015 as Functional Software, Inc. and is
gonna need some rights from me in order to utilize my contributions in
this here PR. So here's the deal: I retain all rights, title and
interest in and to my contributions, and by keeping this boilerplate
intact I confirm that Sentry can use, modify, copy, and redistribute my
contributions, under Sentry's choice of terms.
## EXTRA RESOURCES
- [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
Provides a lambda or proc that's called with an SDK-specific log object, and can return a modified log object, or `nil` to skip reporting the log. This can be used to filter logs before they are sent to Sentry.
If you want to clean up the backtrace of an exception before it's sent to Sentry, you can specify a callback with `backtrace_cleanup_callback`, for example:
Log subscribers automatically respect Rails' parameter filtering configuration. Sensitive parameters defined in `config.filter_parameters` will be filtered from structured logs:
102
+
This will send all `Rails.logger` calls to Sentry:
This captures ALL Rails framework logs as well as ANY OTHER logs produced by instances of the `Logger` class and this can be very noisy. We recommend using `Sentry.logger` for better control over what gets logged.
<Alertlevel="info"title="Note on structured logging">
13
-
Structured logging with Log Subscribers provides rich context and metadata compared to plain logger messages. This enables better filtering, searching, and analysis of your application's behavior in Sentry.
14
-
</Alert>
15
-
16
-
Rails applications can benefit from structured logging using ActiveSupport's Log Subscribers. This feature captures Rails instrumentation events and sends them as structured logs to Sentry with relevant context and metadata.
17
-
18
-
#### Default Setup
19
-
20
-
To enable structured logging with default subscribers:
21
-
22
-
```ruby
23
-
Sentry.init do |config|
24
-
config.dsn ="___PUBLIC_DSN___"
25
-
config.enable_logs =true
26
-
config.rails.structured_logging.enabled =true
27
-
end
28
-
```
29
-
30
-
By default, this enables structured logging for:
31
-
32
-
-**ActiveRecord**: Database queries with SQL, duration, connection info, and caching status
33
-
-**ActionController**: HTTP requests with controller, action, parameters, response status, and timing
34
-
35
-
Additional subscribers are available but not enabled by default:
-**ActionMailer**: Email delivery (`deliver`) and processing (`process`) events
39
-
40
-
See the [Options](#options) section for information on how to enable additional subscribers.
41
-
42
-
### Basic Rails Logger Integration
43
-
44
-
If you enable `:logger` patch, this will affect Rails' built-in logger. This means that anything that Rails logs, and any custom usage of the Rails logger, will result in sending log entries to Sentry:
45
-
46
-
```ruby
47
-
Sentry.init do |config|
48
-
config.dsn ="___PUBLIC_DSN___"
49
-
config.enable_logs =true
50
-
config.enabled_patches = [:logger]
51
-
end
52
-
```
53
-
54
-
Then all logs from Rails logger will be sent to Sentry, for example:
55
-
56
-
```ruby
57
-
# All these calls will result in log entries in Sentry
58
-
# if :logger patch is enabled
59
-
Rails.logger.debug("Hello from Rails logger")
60
-
Rails.logger.info("Hello from Rails logger")
61
-
Rails.logger.error("Hello from Rails logger")
62
-
```
63
-
64
-
<Alertlevel="warning"title="Note on logger patch">
65
-
Enabling `:logger` patch will send all logs from Rails logger to Sentry. This includes logs from Rails framework itself, which might not be desired. In that case we recommend using [Sentry logger](#using-sentry-logger) or [structured logging with log subscribers](#structured-logging-with-log-subscribers).
66
-
</Alert>
14
+
See the [Usage](#usage) section for examples and [Options](#options) to customize which events are captured.
Once the feature is enabled on the SDK, you can send logs using the `Sentry.logger` API.
2
-
3
-
### Using Sentry Logger
4
-
5
-
The `logger` namespace exposes common logging methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warning`, `error`, and `fatal`.
6
-
7
-
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.
1
+
Use the `Sentry.logger` API to send logs to Sentry:
8
2
9
3
```ruby
10
-
Sentry.logger.info("Updated global cache")
11
-
12
-
Sentry.logger.debug("Cache miss for user %{user_id}", user_id:123)
13
-
14
-
Sentry.logger.trace(
15
-
"Starting database connection %{database}",
16
-
database:"users"
17
-
)
18
-
19
-
Sentry.logger.warn(
20
-
"Rate limit reached for endpoint %{endpoint}",
21
-
endpoint:"/api/results/"
22
-
)
23
-
24
-
Sentry.logger.error(
25
-
"Failed to process payment. Order: %{order_id}. Amount: %{amount}",
26
-
order_id:"or_2342", amount:99.99
27
-
)
28
-
29
-
Sentry.logger.fatal(
30
-
"Database %{database} connection pool exhausted",
31
-
database:"users"
32
-
)
4
+
Sentry.logger.info("User logged in successfully")
5
+
Sentry.logger.error("Payment processing failed")
33
6
```
34
7
35
-
### Message Templates
36
-
37
-
You can use message templates with positional or hash parameters:
8
+
You can include additional context data as attributes:
0 commit comments