feat: api logging by pino (#1865)

experiemental
This commit is contained in:
David Nguyen
2025-06-27 21:44:51 +10:00
committed by GitHub
parent 21dc4eee62
commit e07a497b69
10 changed files with 389 additions and 116 deletions

View File

@ -65,7 +65,13 @@ const t = initTRPC
/**
* Middlewares
*/
export const authenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
export const authenticatedMiddleware = t.middleware(async ({ ctx, next, path }) => {
const logger = ctx.logger.child({
path,
auth: ctx.metadata.auth,
source: ctx.metadata.source,
});
const authorizationHeader = ctx.req.headers.get('authorization');
// Taken from `authenticatedMiddleware` in `@documenso/api/v1/middleware/authenticated.ts`.
@ -79,6 +85,11 @@ export const authenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
const apiToken = await getApiTokenByToken({ token });
logger.info({
userId: apiToken.user.id,
apiTokenId: apiToken.id,
});
return await next({
ctx: {
...ctx,
@ -111,6 +122,11 @@ export const authenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
});
}
logger.info({
userId: ctx.user.id,
apiTokenId: null,
});
return await next({
ctx: {
...ctx,