Skip to content

Commit

Permalink
add logger to query router context
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Feb 13, 2023
1 parent 53fb447 commit 6254e1a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/common/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) => void;
type LogFn = (msg: string, extra?: Record<string, unknown>) => void;
type LogLevel = "INFO" | "WARN" | "ERROR";

export function errorToMessage(error: Error): string {
Expand All @@ -41,7 +41,7 @@ class JSONLogger implements Logger {
constructor(private logger = "turnilo") {
}

private logMessage(level: LogLevel, message: string, extra: Record<string, string> = {}) {
private logMessage(level: LogLevel, message: string, extra: Record<string, unknown> = {}) {
console.log(JSON.stringify({
message,
level,
Expand All @@ -51,15 +51,15 @@ class JSONLogger implements Logger {
}));
}

log(message: string, extra: Record<string, string> = {}) {
log(message: string, extra: Record<string, unknown> = {}) {
this.logMessage("INFO", message, extra);
}

error(message: string, extra: Record<string, string> = {}) {
error(message: string, extra: Record<string, unknown> = {}) {
this.logMessage("ERROR", message, extra);
}

warn(message: string, extra: Record<string, string> = {}) {
warn(message: string, extra: Record<string, unknown> = {}) {
this.logMessage("WARN", message, extra);
}

Expand Down
8 changes: 6 additions & 2 deletions src/server/routes/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -40,13 +41,15 @@ interface QueryRouterContext {
essence: Essence;
decorator: AppliedQueryDecorator;
timekeeper: Timekeeper;
logger: Logger;
}

export type QueryRouterRequest = Request & {
context?: QueryRouterContext;
};

export function queryRouter(settings: Pick<SettingsManager, "logger" | "getSources" | "appSettings" | "anchorPath" | "getTimekeeper">) {
const logger = settings.logger;

const router = Router();

Expand All @@ -57,6 +60,7 @@ export function queryRouter(settings: Pick<SettingsManager, "logger" | "getSourc
const decorator = getQueryDecorator(req, dataCube, settings);

(req as QueryRouterRequest).context = {
logger,
dataCube,
essence,
decorator,
Expand All @@ -78,8 +82,8 @@ export function queryRouter(settings: Pick<SettingsManager, "logger" | "getSourc

router.post("/pinboard", asyncHandler(pinboardRoute));

router.use((error: Error, req: Request, res: Response, next: NextFunction) => {
handleRequestErrors(error, res, settings.logger);
router.use((error: Error, { context }: QueryRouterRequest, res: Response, next: NextFunction) => {
handleRequestErrors(error, res, context.logger);
});

return router;
Expand Down

0 comments on commit 6254e1a

Please sign in to comment.