From ea459d05e4ea1400005ca2d7d1ca470ab96342a3 Mon Sep 17 00:00:00 2001 From: mrfry Date: Wed, 23 Mar 2022 18:03:56 +0100 Subject: [PATCH] Added no log to logger --- src/tests/shouldLog.test.ts | 15 +++++++++++++++ src/utils/logger.ts | 11 +++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/tests/shouldLog.test.ts diff --git a/src/tests/shouldLog.test.ts b/src/tests/shouldLog.test.ts new file mode 100644 index 0000000..33a97da --- /dev/null +++ b/src/tests/shouldLog.test.ts @@ -0,0 +1,15 @@ +import { shouldLog } from '../utils/logger' + +const noLogIds = ['1', '2', '10', '40'] + +const truthy = [1, 2, '10', '40'] +const falsey = [5, '55', 47832, 'fhs'] + +test('ShouldLog works', () => { + truthy.forEach((x) => { + expect(shouldLog(x, noLogIds)).toBeTruthy() + }) + falsey.forEach((x) => { + expect(shouldLog(x, noLogIds)).toBeFalsy() + }) +}) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 0aaa387..6fb854e 100755 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -113,6 +113,9 @@ function LogReq( if (req.url.includes('lred')) { dl += C('red') } + if (!shouldLog(req.session.user.id, noLogIds)) { + return + } let hostname if (req.hostname) { @@ -221,10 +224,14 @@ function Load(): void { setNoLogReadInterval() } -function LogStat(url: string, hostname: string, userId: number | string): void { - const nolog = noLogIds.some((noLogId) => { +export function shouldLog(userId: string | number, nolog: string[]): boolean { + return nolog.some((noLogId) => { return noLogId === userId.toString() }) +} + +function LogStat(url: string, hostname: string, userId: number | string): void { + const nolog = shouldLog(userId, noLogIds) if (nolog) { return }