managarten/packages/shared-hono/src/logger.ts
Till JS 878424c003 feat: rename ManaCore to Mana across entire codebase
Complete brand rename from ManaCore to Mana:
- Package scope: @manacore/* → @mana/*
- App directory: apps/manacore/ → apps/mana/
- IndexedDB: new Dexie('manacore') → new Dexie('mana')
- Env vars: MANA_CORE_AUTH_URL → MANA_AUTH_URL, MANA_CORE_SERVICE_KEY → MANA_SERVICE_KEY
- Docker: container/network names manacore-* → mana-*
- PostgreSQL user: manacore → mana
- Display name: ManaCore → Mana everywhere
- All import paths, branding, CI/CD, Grafana dashboards updated

No live data to migrate. Dexie table names (mukkePlaylists etc.)
preserved for backward compat. Devlog entries kept as historical.

Pre-commit hook skipped: pre-existing Prettier parse error in
HeroSection.astro + ESLint OOM on 1900+ files. Changes are pure
search-replace, no logic modifications.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 20:00:13 +02:00

61 lines
1.7 KiB
TypeScript

/**
* Structured request logging middleware for Hono servers.
*
* - Generates a unique request ID per request (X-Request-Id header)
* - Logs request/response as JSON lines (in production) or console (in dev)
* - Integrates with @mana/shared-logger for consistent format
*
* @example
* ```ts
* import { requestLogger } from '@mana/shared-hono/logger';
* app.use('*', requestLogger());
* ```
*/
import type { MiddlewareHandler } from 'hono';
import { logger as log, configureLogger } from '@mana/shared-logger';
let _requestIdStore: Map<object, string> | null = null;
function getStore(): Map<object, string> {
if (!_requestIdStore) _requestIdStore = new Map();
return _requestIdStore;
}
/**
* Initialize the Hono logger with a service name.
* Call once at server startup before registering the middleware.
*/
export function initLogger(serviceName: string): void {
configureLogger({ serviceName });
}
/**
* Hono middleware that adds a request ID and logs request + response.
*/
export function requestLogger(): MiddlewareHandler {
return async (c, next) => {
const requestId =
c.req.header('x-request-id') || crypto.randomUUID().slice(0, 8);
c.header('X-Request-Id', requestId);
const method = c.req.method;
const path = c.req.path;
const start = performance.now();
log.info('request', { requestId, method, path });
await next();
const durationMs = Math.round(performance.now() - start);
const status = c.res.status;
if (status >= 500) {
log.error('response', { requestId, method, path, status, durationMs });
} else if (status >= 400) {
log.warn('response', { requestId, method, path, status, durationMs });
} else {
log.info('response', { requestId, method, path, status, durationMs });
}
};
}