refactor: structured logging across server

Create server/src/lib/logger.ts with info/warn/error methods that add
[botfights:tag] timestamps. Replace bare console.log/warn/error calls
in index.ts, seed.ts, routes/fights.ts, engine/queue.ts, engine/mock.ts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-08 20:41:23 +00:00
co-authored by Claude Opus 4.6
parent 7bd110684d
commit f62323c59f
6 changed files with 42 additions and 18 deletions
+7 -6
View File
@@ -1,4 +1,5 @@
import { serve } from '@hono/node-server'
import { logger } from './lib/logger.js'
import { app } from './app.js'
import { runMigrations } from './db/startup.js'
import { seedMockBots, seedClassicBots } from './engine/mock.js'
@@ -11,10 +12,10 @@ if (process.env.NODE_ENV === 'production') {
const walletVars = ['BOTFIGHTS_NWC_URL', 'BOTFIGHTS_WALLET_ENCRYPTION_KEY']
const missing = walletVars.filter(k => !process.env[k])
if (missing.length > 0) {
console.warn(`[botfights] WARNING: Missing env vars: ${missing.join(', ')} — wallet/payment features disabled`)
logger.warn('env', `Missing env vars: ${missing.join(', ')} — wallet/payment features disabled`)
}
if (!process.env.CORS_ORIGIN || process.env.CORS_ORIGIN === '*') {
console.warn('[botfights] WARNING: CORS_ORIGIN not set — allowing all origins')
logger.warn('env', 'CORS_ORIGIN not set — allowing all origins')
}
}
@@ -26,7 +27,7 @@ await seedClassicBots()
const port = Number(process.env.PORT) || 9100
serve({ fetch: app.fetch, port }, () => {
console.log(`[botfights] server listening on http://localhost:${port}`)
logger.info('server', `listening on http://localhost:${port}`)
// Start background fight loop so the site always has fresh activity
startBackgroundFights()
@@ -34,14 +35,14 @@ serve({ fetch: app.fetch, port }, () => {
// Graceful shutdown — drain in-flight fights before exiting
const shutdown = async (signal: string) => {
console.log(`[botfights] ${signal} received, shutting down...`)
logger.info('server', `${signal} received, shutting down...`)
const active = getActiveFighterCount()
if (active > 0) {
console.log(`[botfights] ${active} fighters still active, waiting 10s...`)
logger.info('server', `${active} fighters still active, waiting 10s...`)
}
await new Promise(r => setTimeout(r, active > 0 ? 10_000 : 500))
clearAllPending()
console.log('[botfights] shutdown complete')
logger.info('server', 'shutdown complete')
process.exit(0)
}
process.on('SIGTERM', () => shutdown('SIGTERM'))