Назад к каталогу
⭐ TOP
Logger with Context
Структурный логгер с контекстом — requestId, userId, traceId. JSON для прода, pretty для разработки. Как pino, но проще.
E
Elena Volkova
3.8
4 оценок
41
скачиваний
677
просмотров
$ Add to .github/copilot-skills/ in your repository
Скачать .mdloggingjsonstructuredcontextrequest-idtraceprettyproduction
# Logger with Context
`console.log('done')` — отлично работает пока один пользователь. Когда их 10 тысяч одновременно и запросы летят параллельно, нужен контекст: какой запрос, какой юзер, какая ошибка.
Этот логгер добавляет контекст к каждому сообщению. В проде — JSON (для ELK/Datadog). В разработке — pretty print с цветами.
## Что умеет
- Уровни: debug, info, warn, error, fatal
- Контекст: requestId, userId, traceId, что угодно
- Child loggers: `log.with({ requestId: 'abc' })`
- JSON output для прода, pretty для dev
- Авто-error stack traces
- Redact секретов (password, token, apiKey)
## Пример
```ts
import { createLogger } from './context-logger';
const log = createLogger({ service: 'api', env: process.env.NODE_ENV });
// middleware — добавляем requestId
app.use((req, res, next) => {
req.log = log.with({ requestId: crypto.randomUUID(), path: req.path });
req.log.info('incoming request');
next();
});
// в хендлере
req.log.info('user authenticated', { userId: user.id });
req.log.error('payment failed', { amount: 100, error: err.message });
```
Обновлено 13 мая 2026 г.v1.2.0