Skip to content

Commit de37413

Browse files
author
Theodore Li
committed
feat(log): Add wrapper function for standardized logging
1 parent 5f33432 commit de37413

File tree

610 files changed

+21779
-20401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

610 files changed

+21779
-20401
lines changed

.claude/rules/global.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Global Standards
22

33
## Logging
4-
Import `createLogger` from `sim/logger`. Use `logger.info`, `logger.warn`, `logger.error` instead of `console.log`.
4+
Import `createLogger` from `@sim/logger`. Use `logger.info`, `logger.warn`, `logger.error` instead of `console.log`. Inside API routes wrapped with `withRouteHandler`, loggers automatically include the request ID.
5+
6+
## API Route Handlers
7+
All API route handlers must be wrapped with `withRouteHandler` from `@/lib/core/utils/with-route-handler`. Never export a bare `async function GET/POST/...` — always use `export const METHOD = withRouteHandler(...)`.
58

69
## Comments
710
Use TSDoc for documentation. No `====` separators. No non-TSDoc comments.

CLAUDE.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ You are a professional software engineer. All code must follow best practices: a
44

55
## Global Standards
66

7-
- **Logging**: Import `createLogger` from `@sim/logger`. Use `logger.info`, `logger.warn`, `logger.error` instead of `console.log`
7+
- **Logging**: Import `createLogger` from `@sim/logger`. Use `logger.info`, `logger.warn`, `logger.error` instead of `console.log`. Inside API routes wrapped with `withRouteHandler`, loggers automatically include the request ID — no manual `withMetadata({ requestId })` needed
8+
- **API Route Handlers**: All API route handlers (`GET`, `POST`, `PUT`, `DELETE`, `PATCH`) must be wrapped with `withRouteHandler` from `@/lib/core/utils/with-route-handler`. This provides request ID tracking, automatic error logging for 4xx/5xx responses, and unhandled error catching. See "API Route Pattern" section below
89
- **Comments**: Use TSDoc for documentation. No `====` separators. No non-TSDoc comments
910
- **Styling**: Never update global styles. Keep all styling local to components
1011
- **ID Generation**: Never use `crypto.randomUUID()`, `nanoid`, or `uuid` package. Use `generateId()` (UUID v4) or `generateShortId()` (compact) from `@/lib/core/utils/uuid`
@@ -92,6 +93,41 @@ export function Component({ requiredProp, optionalProp = false }: ComponentProps
9293

9394
Extract when: 50+ lines, used in 2+ files, or has own state/logic. Keep inline when: < 10 lines, single use, purely presentational.
9495

96+
## API Route Pattern
97+
98+
Every API route handler must be wrapped with `withRouteHandler`. This sets up `AsyncLocalStorage`-based request context so all loggers in the request lifecycle automatically include the request ID.
99+
100+
```typescript
101+
import { createLogger } from '@sim/logger'
102+
import type { NextRequest } from 'next/server'
103+
import { NextResponse } from 'next/server'
104+
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
105+
106+
const logger = createLogger('MyAPI')
107+
108+
// Simple route
109+
export const GET = withRouteHandler(async (request: NextRequest) => {
110+
logger.info('Handling request') // automatically includes {requestId=...}
111+
return NextResponse.json({ ok: true })
112+
})
113+
114+
// Route with params
115+
export const DELETE = withRouteHandler(async (
116+
request: NextRequest,
117+
{ params }: { params: Promise<{ id: string }> }
118+
) => {
119+
const { id } = await params
120+
return NextResponse.json({ deleted: id })
121+
})
122+
123+
// Composing with other middleware (withRouteHandler wraps the outermost layer)
124+
export const POST = withRouteHandler(withAdminAuth(async (request) => {
125+
return NextResponse.json({ ok: true })
126+
}))
127+
```
128+
129+
Never export a bare `async function GET/POST/...` — always use `export const METHOD = withRouteHandler(...)`.
130+
95131
## Hooks
96132

97133
```typescript

0 commit comments

Comments
 (0)