Skip to content

Commit 25de907

Browse files
committed
feat(logs): flexible log message for downstream services
1 parent ce586d5 commit 25de907

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/telemetry/requestLogger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
108108
}
109109
}
110110

111-
service.getLogFields?.(req as RequestWithApp<SLocals>, endLog);
112-
logger.info(endLog, url);
111+
const msg = service.getLogFields?.(req as RequestWithApp<SLocals>, endLog) || url;
112+
logger.info(endLog, msg);
113113
}
114114

115115
export function loggerMiddleware<
@@ -162,8 +162,8 @@ export function loggerMiddleware<
162162
sid: (req as WithIdentifiedSession).session?.id,
163163
c: req.headers.correlationid || undefined,
164164
};
165-
service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
166-
logger.info(preLog, url);
165+
const msg = service.getLogFields?.(req as RequestWithApp<SLocals>, preLog) || url;
166+
logger.info(preLog, msg);
167167
}
168168

169169
const logWriter = () => finishLog(app, undefined, req, res, histogram);

src/types.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ export interface Service<
9595
): boolean | Promise<boolean>;
9696

9797
// Add or redact any fields for logging. Note this will be called twice per request,
98-
// once at the start and once at the end. Modify the values directly.
98+
// once at the start and once at the end. Modify the values directly. Return a
99+
// string to control the "msg" field of the logs, or return undefined to leave it
100+
// as the default, which is the request URL.
99101
getLogFields?(
100102
req: RequestWithApp<SLocals>,
101103
values: Record<string, string | string[] | number | undefined>,
102-
): void;
104+
): string | undefined;
103105

104106
// The repl is a useful tool for diagnosing issues in non-dev environments.
105107
// The attachRepl method provides a way to add custom functionality
@@ -195,11 +197,8 @@ export interface ServiceTypes<
195197
ServiceLocals: SLocals;
196198
}
197199

198-
export type UnwrapServiceConfig<SLocals extends ServiceLocals> = SLocals extends ServiceLocals<
199-
infer C
200-
>
201-
? C
202-
: never;
200+
export type UnwrapServiceConfig<SLocals extends ServiceLocals> =
201+
SLocals extends ServiceLocals<infer C> ? C : never;
203202

204203
// TODO this allows us to clean up the generics by having a loose parameter
205204
// but using the UnwrapServiceConfig to get the specific type back

0 commit comments

Comments
 (0)