[PR #7040] [CLOSED] feat(core): add BETTER_AUTH_LOG_LEVEL environment variable for logger #7039

Closed
opened 2026-03-13 13:22:04 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7040
Author: @dagimkassagebrie
Created: 12/28/2025
Status: Closed

Base: canaryHead: feat/add-log-level-env-var


📝 Commits (1)

  • f1a2358 feat(core): add BETTER_AUTH_LOG_LEVEL environment variable for logger

📊 Changes

5 files changed (+145 additions, -4 deletions)

View changed files

📝 docs/content/docs/installation.mdx (+10 -0)
📝 docs/content/docs/reference/options.mdx (+6 -1)
📝 packages/core/src/env/index.ts (+1 -0)
📝 packages/core/src/env/logger.test.ts (+108 -2)
📝 packages/core/src/env/logger.ts (+20 -1)

📄 Description

Problem Statement

Issue: https://github.com/better-auth/better-auth/issues/7037

By default, Better Auth only logs at the "error" level. During development, you want to see debug or info logs, but right now you have to change your code to add logger: { level: "debug" } to your auth config. This means you can't easily switch log levels between environments without code changes.

Description

This adds support for a BETTER_AUTH_LOG_LEVEL environment variable so you can control logging without touching your code.

What Changed

Before: You had to modify your code like this:

export const auth = betterAuth({
  logger: {
    level: "debug",  // ← Required code change
  },
});

After:

Just set an environment variable:

export BETTER_AUTH_LOG_LEVEL=debug      -> Your code stays clean - no logger config needed:script
export const auth = betterAuth({
  // No logger config needed!
});

The code option still works if you want it (it takes precedence over the env var).

Also,

  • Added getLogLevelFromEnv() function that reads the env var
  • Updated createLogger() to check the env var if no explicit level is provided
  • Priority: explicit code option > env var > default "error"
  • Case-insensitive (DEBUG, debug, Debug all work)
  • Excludes "success" level (internal only)
  • Added BETTER_AUTH_LOG_LEVEL to installation docs (environment variables section)
  • Updated logger options reference to mention env var support

Testing

Added 31 tests covering all valid levels, case insensitivity, invalid values, and integration with createLogger.

Backward Compatibility

Fully backward compatible - existing code keeps working. The env var is optional, and if you explicitly set the level in code, that still takes priority.

Usage Example

In your .env file or environment
BETTER_AUTH_LOG_LEVEL=debug # for development
BETTER_AUTH_LOG_LEVEL=info # for staging
BETTER_AUTH_LOG_LEVEL=error # for production## Checklist

Format is correct and all tests pass and build succeeds, and I have followed existing patterns and it is also backward compatible.


Summary by cubic

Add BETTER_AUTH_LOG_LEVEL to control the logger level via environment variable, so you can switch log verbosity per environment without code changes. Explicit logger.level still takes precedence; default remains error.

  • New Features
    • Read log level from BETTER_AUTH_LOG_LEVEL (debug, info, warn, error), case-insensitive
    • Precedence: code option > env var > default error
    • createLogger falls back to the env var when level isn’t set in code
    • Excludes “success” from env values
    • Docs updated (installation and options)

Written for commit f1a2358420. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7040 **Author:** [@dagimkassagebrie](https://github.com/dagimkassagebrie) **Created:** 12/28/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/add-log-level-env-var` --- ### 📝 Commits (1) - [`f1a2358`](https://github.com/better-auth/better-auth/commit/f1a2358420aff15754ebf9b162aed7c70fe7e2a6) feat(core): add BETTER_AUTH_LOG_LEVEL environment variable for logger ### 📊 Changes **5 files changed** (+145 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/installation.mdx` (+10 -0) 📝 `docs/content/docs/reference/options.mdx` (+6 -1) 📝 `packages/core/src/env/index.ts` (+1 -0) 📝 `packages/core/src/env/logger.test.ts` (+108 -2) 📝 `packages/core/src/env/logger.ts` (+20 -1) </details> ### 📄 Description ## Problem Statement Issue: https://github.com/better-auth/better-auth/issues/7037 By default, Better Auth only logs at the "error" level. During development, you want to see debug or info logs, but right now you have to change your code to add `logger: { level: "debug" }` to your auth config. This means you can't easily switch log levels between environments without code changes. ## Description This adds support for a `BETTER_AUTH_LOG_LEVEL` environment variable so you can control logging without touching your code. ## What Changed **Before:** You had to modify your code like this: ``` export const auth = betterAuth({ logger: { level: "debug", // ← Required code change }, }); ``` **After:** Just set an environment variable: ``` export BETTER_AUTH_LOG_LEVEL=debug -> Your code stays clean - no logger config needed:script export const auth = betterAuth({ // No logger config needed! }); ``` The code option still works if you want it (it takes precedence over the env var). Also, - Added `getLogLevelFromEnv()` function that reads the env var - Updated `createLogger()` to check the env var if no explicit level is provided - Priority: explicit code option > env var > default "error" - Case-insensitive (DEBUG, debug, Debug all work) - Excludes "success" level (internal only) - Added `BETTER_AUTH_LOG_LEVEL` to installation docs (environment variables section) - Updated logger options reference to mention env var support ## Testing Added 31 tests covering all valid levels, case insensitivity, invalid values, and integration with createLogger. ## Backward Compatibility Fully backward compatible - existing code keeps working. The env var is optional, and if you explicitly set the level in code, that still takes priority. ## Usage Example In your .env file or environment BETTER_AUTH_LOG_LEVEL=debug # for development BETTER_AUTH_LOG_LEVEL=info # for staging BETTER_AUTH_LOG_LEVEL=error # for production## Checklist Format is correct and all tests pass and build succeeds, and I have followed existing patterns and it is also backward compatible. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add BETTER_AUTH_LOG_LEVEL to control the logger level via environment variable, so you can switch log verbosity per environment without code changes. Explicit logger.level still takes precedence; default remains error. - New Features - Read log level from BETTER_AUTH_LOG_LEVEL (debug, info, warn, error), case-insensitive - Precedence: code option > env var > default error - createLogger falls back to the env var when level isn’t set in code - Excludes “success” from env values - Docs updated (installation and options) <sup>Written for commit f1a2358420aff15754ebf9b162aed7c70fe7e2a6. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:22:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#7039