From 6254e1aad7adccdf2d214be80f86af5724cf3d9b Mon Sep 17 00:00:00 2001 From: Adrian Mroz Date: Mon, 13 Feb 2023 14:01:35 +0100 Subject: [PATCH] add logger to query router context --- src/common/logger/logger.ts | 10 +++++----- src/server/routes/query/query.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/common/logger/logger.ts b/src/common/logger/logger.ts index 08639c966..b1e6ef43b 100644 --- a/src/common/logger/logger.ts +++ b/src/common/logger/logger.ts @@ -19,7 +19,7 @@ import { noop, Unary } from "../utils/functional/functional"; import { isNil } from "../utils/general/general"; import { isoNow } from "../utils/time/time"; -type LogFn = (msg: string, extra?: Record) => void; +type LogFn = (msg: string, extra?: Record) => void; type LogLevel = "INFO" | "WARN" | "ERROR"; export function errorToMessage(error: Error): string { @@ -41,7 +41,7 @@ class JSONLogger implements Logger { constructor(private logger = "turnilo") { } - private logMessage(level: LogLevel, message: string, extra: Record = {}) { + private logMessage(level: LogLevel, message: string, extra: Record = {}) { console.log(JSON.stringify({ message, level, @@ -51,15 +51,15 @@ class JSONLogger implements Logger { })); } - log(message: string, extra: Record = {}) { + log(message: string, extra: Record = {}) { this.logMessage("INFO", message, extra); } - error(message: string, extra: Record = {}) { + error(message: string, extra: Record = {}) { this.logMessage("ERROR", message, extra); } - warn(message: string, extra: Record = {}) { + warn(message: string, extra: Record = {}) { this.logMessage("WARN", message, extra); } diff --git a/src/server/routes/query/query.ts b/src/server/routes/query/query.ts index e88f06453..9ef62a774 100644 --- a/src/server/routes/query/query.ts +++ b/src/server/routes/query/query.ts @@ -15,6 +15,7 @@ */ import { NextFunction, Request, Response, Router } from "express"; +import { Logger } from "../../../common/logger/logger"; import { QueryableDataCube } from "../../../common/models/data-cube/queryable-data-cube"; import { Essence } from "../../../common/models/essence/essence"; import { Timekeeper } from "../../../common/models/timekeeper/timekeeper"; @@ -40,6 +41,7 @@ interface QueryRouterContext { essence: Essence; decorator: AppliedQueryDecorator; timekeeper: Timekeeper; + logger: Logger; } export type QueryRouterRequest = Request & { @@ -47,6 +49,7 @@ export type QueryRouterRequest = Request & { }; export function queryRouter(settings: Pick) { + const logger = settings.logger; const router = Router(); @@ -57,6 +60,7 @@ export function queryRouter(settings: Pick { - handleRequestErrors(error, res, settings.logger); + router.use((error: Error, { context }: QueryRouterRequest, res: Response, next: NextFunction) => { + handleRequestErrors(error, res, context.logger); }); return router;