-
-
Notifications
You must be signed in to change notification settings - Fork 27
fix: replace console.log with structured pino logger in API startup #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,17 @@ | ||||||||||||||||
| import { ValidationPipe } from "@nestjs/common"; | ||||||||||||||||
| import { NestFactory } from "@nestjs/core"; | ||||||||||||||||
| import { Logger } from "nestjs-pino"; | ||||||||||||||||
| import pino from "pino"; | ||||||||||||||||
| import { AppModule } from "./app.module"; | ||||||||||||||||
| import { runMigrations } from "./database/migrate"; | ||||||||||||||||
|
|
||||||||||||||||
| const bootstrapLogger = pino({ name: "bootstrap" }); | ||||||||||||||||
|
|
||||||||||||||||
| async function bootstrap() { | ||||||||||||||||
| // Run database migrations before starting the application | ||||||||||||||||
| // Note: migrations run before NestJS app is created, so we use console here | ||||||||||||||||
| console.log("Running database migrations..."); | ||||||||||||||||
| bootstrapLogger.info("Running database migrations..."); | ||||||||||||||||
| await runMigrations(); | ||||||||||||||||
| console.log("Database migrations completed"); | ||||||||||||||||
| bootstrapLogger.info("Database migrations completed"); | ||||||||||||||||
|
|
||||||||||||||||
| const app = await NestFactory.create(AppModule, { bufferLogs: true }); | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -39,6 +41,6 @@ async function bootstrap() { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| bootstrap().catch((error) => { | ||||||||||||||||
| console.error("Failed to start the0 API:", error); | ||||||||||||||||
| bootstrapLogger.error({ err: error }, "Failed to start the0 API"); | ||||||||||||||||
| process.exit(1); | ||||||||||||||||
|
Comment on lines
+44
to
45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, check if the file exists and examine lines around 44-45
if [ -f "api/src/main.ts" ]; then
echo "=== File: api/src/main.ts ==="
wc -l api/src/main.ts
echo "=== Lines 40-50 context ==="
sed -n '40,50p' api/src/main.ts | cat -n
else
echo "File api/src/main.ts not found"
git ls-files | grep "main.ts"
fiRepository: alexanderwanyoike/the0 Length of output: 367 🏁 Script executed: # Search for bootstrap function and error handling patterns
rg "bootstrap.*catch|process\.exit" api/src/ -A 3 -B 3Repository: alexanderwanyoike/the0 Length of output: 2067 🏁 Script executed: # Check how the logger is initialized to understand pino setup
rg "bootstrapLogger|pino|winston" api/src/main.ts -B 2 -A 2Repository: alexanderwanyoike/the0 Length of output: 801 🏁 Script executed: # Check pino configuration and understand default behavior
rg "pino|transport|stream" api/src/main.ts api/src/app.module.ts -B 2 -A 2Repository: alexanderwanyoike/the0 Length of output: 559 🏁 Script executed: # Check if there are any pino transport configurations or logging setup
fd -e ts -e js api/src | xargs grep -l "pino\|Logger" | head -10Repository: alexanderwanyoike/the0 Length of output: 399 🌐 Web query:
💡 Result: In Node.js, when using Pino logger and calling process.exit, logs may not flush to stdout synchronously by default, potentially losing recent entries due to buffering in sonic-boom (Pino's underlying writer). This is a known issue discussed extensively in Pino GitHub issues since v5+. Best practices for synchronous stdout flushing before process.exit: 1. Use pino.destination explicitly (sonic-boom defaults to sync: true for stdout/files): import pino from 'pino'; const dest = pino.destination; // or pino.destination(1) for explicit stdout fd const logger = pino(dest); logger.info('Will flush'); process.exit(1); // Logs guaranteed due to synchronous writes This ensures writes are synchronous (fs.writeSync), preventing loss on abrupt exit. Performance trade-off: slower than async but safe for exit scenarios. 2. For async destinations (sync: false), call logger.flushSync or dest.flushSync before exit: const dest = pino.destination({ sync: false }); const logger = pino(dest); logger.info('Flush me'); dest.flushSync; // Synchronous buffer flush process.exit(1); Note: logger.flush is async (fire-and-forget); use flushSync for blocking guarantee before exit. 3. Use logger.fatal: Automatically sync-flushes as it's designed for pre-exit logging. logger.fatal('Exiting with error'); process.exit(1); // Flushed automatically 4. Avoid transports like pino-pretty in production/exit-critical paths: They run in worker threads, causing flush races (Issue Citations:
Ensure error logs flush before exiting during bootstrap failures. Line 46 calls Proposed changebootstrap().catch((error) => {
bootstrapLogger.error({ err: error }, "Failed to start the0 API");
- process.exit(1);
+ process.exitCode = 1;
+ return;
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| }); | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 1017
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 543
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 130
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 164
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 643
🏁 Script executed:
# Check the api.yml workflow file for frozen-lockfile instructions cat -n .github/workflows/api.ymlRepository: alexanderwanyoike/the0
Length of output: 2694
🏁 Script executed:
# Also check what's actually in the api directory to understand the structure ls -la api/Repository: alexanderwanyoike/the0
Length of output: 1017
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 1478
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 740
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 372
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 79
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 497
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 336
🏁 Script executed:
Repository: alexanderwanyoike/the0
Length of output: 48
Update the pino version to satisfy existing dependency constraints.
Line 57 adds
"pino": "^9.6.0", butpino-http@^11.0.0(already in dependencies) requirespino@^10.0.0. This version conflict will causeyarn install --frozen-lockfileto fail in CI. Either update pino to^10.0.0or downgrade pino-http to a version compatible with pino ^9.6.0, then runyarn installinapi/and commit the updated lockfile.🤖 Prompt for AI Agents