Skip to content

Conversation

stayallive
Copy link
Collaborator

@stayallive stayallive commented Sep 23, 2025

This should allow us to simplify the logging setup in modern Laravel applications.

Instead of adding the channel manually to the logging.php as the first step we can configure it using only these .env settings:

LOG_CHANNEL=stack
LOG_STACK=single,sentry_logs

# And we also need
SENTRY_ENABLE_LOGS=true

# Optional:
LOG_LEVEL=info # defaults to debug
# or:
SENTRY_LOGS_LEVEL=info # defaults to LOG_LEVEL

We should be careful with SENTRY_LOGS_LEVEL in the docs since users who manually added the sentry_logs channel will not have the boilerplate to allow SENTRY_LOGS_LEVEL to work so LOG_LEVEL should be prefereed, however since that usually sets the log level for the complete application it's nice to have the option to just set the Sentry version. Maybe you want LOG_LEVEL=warning and SENTRY_LOGS_LEVEL=debug for example.

The troubleshooting steps still apply when the application doesn't have a stack channel which has been the default for many years but not everyone keeps up to date with their config changes.

Comment on lines +38 to +39
// The minimum log level that will be sent to Sentry as logs using the `sentry_logs` logging channel
'logs_channel_level' => env('SENTRY_LOGS_LEVEL', env('LOG_LEVEL', 'debug')),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need this otherwise we cannot get the env() call to work with config caching. This gives us some flexibility where we can limit the log level just for Sentry if needed. Since configurations are merged with existing user configuration this key will not need to be added on upgrade of existing users and works out of the box.

The debug default comes from: https://github.com/laravel/laravel/blob/12.x/config/logging.php

]);
}

if (!array_key_exists('sentry_logs', $logChannels)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They array_key_exists check is here to ensure we do not overwrite the custom configuration by the user if it's present. We only register if it's not already added by the user.

@stayallive stayallive marked this pull request as ready for review September 25, 2025 11:26
Copy link
Contributor

@Litarnus Litarnus left a comment

Choose a reason for hiding this comment

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

Nice!

@Litarnus Litarnus merged commit 6a51dbe into master Sep 25, 2025
34 checks passed
@Litarnus Litarnus deleted the auto-config-logs branch September 25, 2025 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants