Skip to content

Commit

Permalink
r legger til loglevel for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
maccyber committed Nov 28, 2022
1 parent 64785f5 commit 2159300
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 2159300

Please sign in to comment.