From 21593002b1a6438b857b5928e10facb69cfa9138 Mon Sep 17 00:00:00 2001 From: Jonas Enge Date: Mon, 28 Nov 2022 11:44:22 +0100 Subject: [PATCH] r legger til loglevel for proxy --- src/http.ts | 5 +++-- src/logger.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/http.ts b/src/http.ts index 89258c4..81e6c26 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import config from './config'; import axios, { AxiosError } from 'axios'; -import log from './logger'; +import log, { getLogLevel } from './logger'; import { getTokenFromCookie } from './auth/tokenDings'; interface ProxyOpts { @@ -44,7 +44,8 @@ export function proxyHttpCall(url: string, opts?: ProxyOpts) { } catch (err) { const e = err as AxiosError; const status = e.response?.status || 500; - log.error(`${method} ${url}: ${status} ${e.response?.statusText}`); + const logLevel = getLogLevel(status); + log[logLevel](`${method} ${url}: ${status} ${e.response?.statusText}`); return res.status(status).send((err as Error).message); } }; diff --git a/src/logger.ts b/src/logger.ts index 1f1f6fa..a4f8b43 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -15,7 +15,7 @@ export function customRequestLogMessage(req: IncomingMessage, res: ServerRespons return `${req.method} ${req.url} completed ${res.statusCode} ${res.statusMessage}`; } -function getLogLevel(statusCode: number, err: Error): pino.Level { +export function getLogLevel(statusCode: number, err?: Error): pino.Level { if (statusCode === 401 || statusCode === 403) return 'warn'; if (statusCode >= 400 || err) return 'error'; return 'info';