From 873d9fcee3bf0c17c471ab2b762496b79d0a4ee7 Mon Sep 17 00:00:00 2001
From: Steve Reis <stevereis93@gmail.com>
Date: Mon, 20 Jun 2022 12:14:24 +0000
Subject: [PATCH] feat: Added configurable log levels

---
 api/src/common/utils/shared.utils.ts | 15 ++++++++++++++-
 api/src/main.ts                      |  8 +++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/api/src/common/utils/shared.utils.ts b/api/src/common/utils/shared.utils.ts
index 8f1ed08..8d9b507 100644
--- a/api/src/common/utils/shared.utils.ts
+++ b/api/src/common/utils/shared.utils.ts
@@ -2,15 +2,28 @@
 /* eslint-disable @typescript-eslint/no-use-before-define */
 
 import {
-  BadRequestException,
   HttpException,
   InternalServerErrorException,
+  LogLevel,
   NotFoundException,
   RequestTimeoutException,
   UnauthorizedException,
 } from '@nestjs/common';
 import axios from 'axios';
 
+export const LOG_LEVELS = [
+  ['warn', 'error'],
+  ['warn', 'error', 'log'],
+  ['warn', 'error', 'log', 'verbose'],
+  ['warn', 'error', 'log', 'verbose', 'debug'],
+];
+
+export const getLogLevels = (level: number): LogLevel[] => {
+  let internLevel = level - 1;
+  if (internLevel > LOG_LEVELS.length || internLevel < 0) internLevel = 0;
+  return LOG_LEVELS[internLevel] as LogLevel[];
+};
+
 export const errorAxiosHandler = (e: any) => {
   if (!axios.isAxiosError(e)) throw new InternalServerErrorException(e);
 
diff --git a/api/src/main.ts b/api/src/main.ts
index 6e6fe7b..7caa98d 100644
--- a/api/src/main.ts
+++ b/api/src/main.ts
@@ -1,9 +1,14 @@
 import { NestFactory } from '@nestjs/core';
 import { NestExpressApplication } from '@nestjs/platform-express';
-import { AppModule } from './main/app.module';
 import * as cookieParser from 'cookie-parser';
+import { getLogLevels } from './common/utils/shared.utils';
+import { AppModule } from './main/app.module';
 
 const CORS_URL = process.env.CORS_URL ?? process.env.ENGINE_BASE_URL;
+const DEFAULT_LEVEL = process.env.NODE_ENV === 'production' ? 1 : 4;
+const LOG_LEVEL = process.env.LOG_LEVEL
+  ? parseInt(process.env.LOG_LEVEL)
+  : DEFAULT_LEVEL;
 
 async function bootstrap() {
   const app = await NestFactory.create<NestExpressApplication>(AppModule, {
@@ -15,6 +20,7 @@ async function bootstrap() {
         CORS_URL,
       ],
     },
+    logger: getLogLevels(LOG_LEVEL),
   });
 
   app.use(cookieParser());
-- 
GitLab