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:
co-authored by
Claude Opus 4.6
parent
7bd110684d
commit
f62323c59f
@@ -1,4 +1,5 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import { logger } from '../lib/logger.js'
|
||||
import { createHash, randomBytes } from 'crypto'
|
||||
import { db, schema, sqlite } from '../db/index.js'
|
||||
import { randomArena } from './arenas.js'
|
||||
@@ -335,7 +336,7 @@ export async function seedClassicBots(): Promise<void> {
|
||||
})
|
||||
}
|
||||
|
||||
console.log(`[botfights] seeded ${CLASSIC_BOTS.length} classic bots`)
|
||||
logger.info('mock', `seeded ${CLASSIC_BOTS.length} classic bots`)
|
||||
}
|
||||
|
||||
export function isClassicBot(webhookUrl: string): boolean {
|
||||
@@ -400,7 +401,7 @@ export async function seedMockBots(): Promise<void> {
|
||||
inserted++
|
||||
}
|
||||
|
||||
console.log(`[botfights] mock bots: ${inserted} inserted, ${updated} updated`)
|
||||
logger.info('mock', `mock bots: ${inserted} inserted, ${updated} updated`)
|
||||
}
|
||||
|
||||
export async function runMockFight(botAId: string, botBId: string): Promise<string> {
|
||||
@@ -558,7 +559,7 @@ export function generateMockBotResponse(
|
||||
export async function seedMockFights(count: number = 12): Promise<void> {
|
||||
const allBots = await db.select({ id: schema.bots.id, eloRating: schema.bots.eloRating }).from(schema.bots)
|
||||
if (allBots.length < 2) {
|
||||
console.log('[botfights] need at least 2 bots to seed fights')
|
||||
logger.info('mock', 'need at least 2 bots to seed fights')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -584,5 +585,5 @@ export async function seedMockFights(count: number = 12): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[botfights] seeded ${count} mock fights`)
|
||||
logger.info('mock', `seeded ${count} mock fights`)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { db, schema } from '../db/index.js'
|
||||
import { logger } from '../lib/logger.js'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { runFightAsync, isInFight } from './orchestrator.js'
|
||||
import { seedMockBots } from './mock.js'
|
||||
@@ -69,7 +70,7 @@ export async function joinQueue(botId: string): Promise<string> {
|
||||
throw new Error('Bot is deactivated due to webhook errors. Re-test your webhook to reactivate.')
|
||||
}
|
||||
|
||||
console.log(`[queue] joinQueue botId=${botId} name=${bot.name} webhook=${bot.webhookUrl}`)
|
||||
logger.info('queue', `joinQueue botId=${botId} name=${bot.name} webhook=${bot.webhookUrl}`)
|
||||
|
||||
// Don't allow same bot twice in queue
|
||||
const existing = waitingQueue.findIndex(e => e.botId === botId)
|
||||
@@ -145,7 +146,7 @@ async function startFight(botAId: string, botBId: string): Promise<string> {
|
||||
}
|
||||
|
||||
async function matchAgainstMock(botId: string, webhookUrl: string): Promise<string> {
|
||||
console.log(`[queue] matchAgainstMock botId=${botId} webhook=${webhookUrl}`)
|
||||
logger.info('queue', `matchAgainstMock botId=${botId} webhook=${webhookUrl}`)
|
||||
const allBots = await db.select({
|
||||
id: schema.bots.id,
|
||||
webhookUrl: schema.bots.webhookUrl,
|
||||
@@ -155,7 +156,7 @@ async function matchAgainstMock(botId: string, webhookUrl: string): Promise<stri
|
||||
const mockBots = allBots.filter(b => b.id !== botId && b.webhookUrl.startsWith('http://mock.local'))
|
||||
|
||||
if (mockBots.length === 0) {
|
||||
console.log('[queue] No mock bots found, seeding...')
|
||||
logger.info('queue', 'No mock bots found, seeding...')
|
||||
await seedMockBots()
|
||||
return matchAgainstMock(botId, webhookUrl)
|
||||
}
|
||||
@@ -166,6 +167,6 @@ async function matchAgainstMock(botId: string, webhookUrl: string): Promise<stri
|
||||
mockBots.sort((a, b) => Math.abs(a.eloRating - botElo) - Math.abs(b.eloRating - botElo))
|
||||
const opponent = mockBots[0]
|
||||
|
||||
console.log(`[queue] starting fight: ${botId} vs mock ${opponent.id}`)
|
||||
logger.info('queue', `starting fight: ${botId} vs mock ${opponent.id}`)
|
||||
return runFightAsync(botId, opponent.id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user