-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): add request logging to board page (#1666)
* feat(logging): add pino logger * feat(logging): add middleware and logging to board page
- Loading branch information
1 parent
d21cba7
commit a166f01
Showing
6 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextResponse, userAgent } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
import { logger } from 'utils/logger' | ||
|
||
const log = logger.child({ module: 'middleware' }) | ||
export function middleware(request: NextRequest) { | ||
const response = NextResponse.next() | ||
log.info({ | ||
request: { | ||
browser: userAgent(request).browser, | ||
device: userAgent(request).device, | ||
url: request.url, | ||
path: request.nextUrl.pathname, | ||
method: request.method, | ||
}, | ||
response: { | ||
status: response.status, | ||
body: response.body, | ||
}, | ||
}) | ||
return response | ||
} | ||
|
||
export const config = { | ||
matcher: ['/:id(\\w{20})'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pino, { Logger } from 'pino' | ||
|
||
const PinoLevelToSeverityLookup: { [key: string]: string } = { | ||
trace: 'DEBUG', | ||
debug: 'DEBUG', | ||
info: 'INFO', | ||
warn: 'WARNING', | ||
error: 'ERROR', | ||
fatal: 'CRITICAL', | ||
} | ||
|
||
const defaultPinoConf = { | ||
messageKey: 'message', | ||
formatters: { | ||
level(label: string, number: number) { | ||
return { | ||
severity: | ||
PinoLevelToSeverityLookup[label] || | ||
PinoLevelToSeverityLookup['info'], | ||
level: number, | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
export const logger: Logger = | ||
process.env.COMMON_ENV === 'prd' | ||
? pino({ level: 'warn', ...defaultPinoConf }) | ||
: pino({ | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { | ||
colorize: true, | ||
}, | ||
}, | ||
level: 'debug', | ||
...defaultPinoConf, | ||
}) |
Oops, something went wrong.