fix: debug log level from .env not applied on startup#624
Conversation
|
你可以通过以下命令安装该版本: |
审阅者指南(在小型 PR 上折叠)审阅者指南确保在启动期间 dotenv 配置运行之后,记录器(logger)的运行时日志级别与 .env 文件中的 LOG_LEVEL 保持同步,这样从进程启动开始就能生效 debug 级别日志,而不是只能在 env 文件变更后才生效。 启动时初始化日志级别的时序图sequenceDiagram
actor Developer
participant NodeProcess
participant CoreIndex as core_start
participant Dotenv
participant Logger
Developer->>NodeProcess: run dev command
NodeProcess->>CoreIndex: call start()
CoreIndex->>Dotenv: dotenv.config(path = EBV_FILE, override = true)
Dotenv-->>CoreIndex: process.env populated
CoreIndex->>Logger: set level = (process.env.LOG_LEVEL || 'info') as LoggerLevel
Logger-->>CoreIndex: level updated before other initialization
Note over CoreIndex,Logger: Logger now respects LOG_LEVEL from .env at startup
Logger 与日志级别类型的类图classDiagram
class Logger {
+LoggerLevel level
}
class LoggerLevel
Logger --> LoggerLevel : uses
文件级变更
与关联 issue 的对照评估
可能关联的 issue
提示与命令与 Sourcery 交互
自定义你的使用体验访问你的 控制面板 来:
获取帮助Original review guide in EnglishReviewer's guide (collapsed on small PRs)Reviewer's GuideEnsures the logger's runtime log level is synchronized with LOG_LEVEL from the .env file immediately after dotenv config runs during startup, so debug-level logging is effective from process start instead of only after an env file change. Sequence diagram for logger level initialization on startupsequenceDiagram
actor Developer
participant NodeProcess
participant CoreIndex as core_start
participant Dotenv
participant Logger
Developer->>NodeProcess: run dev command
NodeProcess->>CoreIndex: call start()
CoreIndex->>Dotenv: dotenv.config(path = EBV_FILE, override = true)
Dotenv-->>CoreIndex: process.env populated
CoreIndex->>Logger: set level = (process.env.LOG_LEVEL || 'info') as LoggerLevel
Logger-->>CoreIndex: level updated before other initialization
Note over CoreIndex,Logger: Logger now respects LOG_LEVEL from .env at startup
Class diagram for logger and log level typeclassDiagram
class Logger {
+LoggerLevel level
}
class LoggerLevel
Logger --> LoggerLevel : uses
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - 我在这里给出了一些高层次的反馈:
- 与其使用
(process.env.LOG_LEVEL || 'info') as LoggerLevel这样的类型断言,不如考虑通过项目中其他地方用于日志级别的同一套校验/规范化逻辑来处理,这样可以避免意外值在没有任何提示的情况下绕过类型安全。 - 新增的注释提到
dotenv.config()已经执行完成,但当前代码片段附近并没有展示这行调用;可能更清晰的做法是要么把这个赋值语句移动到 config 调用之后紧接着的位置,要么重新措辞注释,使其与实际的控制流保持一致。 - 现在你至少在两个地方从
process.env.LOG_LEVEL同步logger.level(启动时以及 env 文件监听器中);可以考虑抽取一个小的辅助方法(例如syncLoggerLevelFromEnv()),把这部分逻辑集中到一个地方,保证行为一致。
供 AI Agents 使用的提示词
Please address the comments from this code review:
## Overall Comments
- 与其使用 `(process.env.LOG_LEVEL || 'info') as LoggerLevel` 这样的类型断言,不如考虑通过项目中其他地方用于日志级别的同一套校验/规范化逻辑来处理,这样可以避免意外值在没有任何提示的情况下绕过类型安全。
- 新增的注释提到 `dotenv.config()` 已经执行完成,但当前代码片段附近并没有展示这行调用;可能更清晰的做法是要么把这个赋值语句移动到 config 调用之后紧接着的位置,要么重新措辞注释,使其与实际的控制流保持一致。
- 现在你至少在两个地方从 `process.env.LOG_LEVEL` 同步 `logger.level`(启动时以及 env 文件监听器中);可以考虑抽取一个小的辅助方法(例如 `syncLoggerLevelFromEnv()`),把这部分逻辑集中到一个地方,保证行为一致。帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English
Hey - I've left some high level feedback:
- Instead of casting
(process.env.LOG_LEVEL || 'info') as LoggerLevel, consider routing this through the same validation/normalization logic used elsewhere for log levels so an unexpected value doesn’t silently bypass type safety. - The new comment refers to
dotenv.config()completing, but the code snippet here doesn’t show that call nearby; it might be clearer to either move this assignment directly after the config call or reword the comment to match the actual control flow. - You now have at least two places that sync
logger.levelfromprocess.env.LOG_LEVEL(startup and the env-file watcher); consider extracting a small helper (e.g.syncLoggerLevelFromEnv()) to keep the behavior consistent in one place.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of casting `(process.env.LOG_LEVEL || 'info') as LoggerLevel`, consider routing this through the same validation/normalization logic used elsewhere for log levels so an unexpected value doesn’t silently bypass type safety.
- The new comment refers to `dotenv.config()` completing, but the code snippet here doesn’t show that call nearby; it might be clearer to either move this assignment directly after the config call or reword the comment to match the actual control flow.
- You now have at least two places that sync `logger.level` from `process.env.LOG_LEVEL` (startup and the env-file watcher); consider extracting a small helper (e.g. `syncLoggerLevelFromEnv()`) to keep the behavior consistent in one place.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR fixes an initialization-order issue where logger is created before .env variables are loaded, causing LOG_LEVEL=debug (and other non-default levels) to be ignored until the env file watcher triggers.
Changes:
- After
dotenv.config()runs instart(), explicitly synchronizelogger.levelfromprocess.env.LOG_LEVEL(defaulting toinfo). - Add inline documentation explaining why the explicit sync is required at startup.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
loggeris instantiated at module evaluation time — beforedotenv.config()runs instart()— soprocess.env.LOG_LEVELis unset at that point and the logger defaults to'info'. The only code path that syncedlogger.levelpost-load was theinitEnvfile-watcher callback, meaning the configured level only took effect after a manual file save triggered the watcher.Changes
packages/core/src/index.ts: Afterdotenv.config()instart(), explicitly synclogger.levelfrom the now-populatedprocess.env.LOG_LEVEL:This mirrors the identical assignment already present in the
initEnvwatch callback (env.ts:90) — just applied at startup rather than only on file change.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Summary by Sourcery
错误修复:
.env中获取的LOG_LEVEL应用于日志记录器,这样调试日志在无需修改 env 文件的情况下就能生效。Original summary in English
Summary by Sourcery
Bug Fixes: