From 365f7386a5354cb65a28d6fbb3214d941f2c1ee6 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Wed, 12 Jun 2024 20:40:04 +0530 Subject: [PATCH] chore: call env load before importing logger - set level as part of initialisation --- src/index.ts | 7 +++++-- src/logger.js | 16 ++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3bfd68cf21..47f93068ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,9 +8,12 @@ import { metricsRouter } from './routes/metricsRouter'; import cluster from './util/cluster'; import { RedisDB } from './util/redis/redisConnector'; import { logProcessInfo } from './util/utils'; -import logger from './logger'; dotenv.config(); + +// eslint-disable-next-line import/first +import logger from './logger'; + const clusterEnabled = process.env.CLUSTER_ENABLED !== 'false'; const port = parseInt(process.env.PORT ?? '9090', 10); const metricsPort = parseInt(process.env.METRICS_PORT || '9091', 10); @@ -32,7 +35,7 @@ app.use( addRequestSizeMiddleware(app); addSwaggerRoutes(app); -logger.info('Using new routes'); +logger.error('Using new routes'); applicationRoutes(app); function finalFunction() { diff --git a/src/logger.js b/src/logger.js index 101689d0cc..483bef0491 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,9 +1,7 @@ const dotenv = require('dotenv'); /* istanbul ignore file */ -const { structuredLogger: logger /* LOGLEVELS */ } = require('@rudderstack/integrations-lib'); - -dotenv.config({ path: '../.env' }); +const { /* LOGLEVELS */ structuredLogger } = require('@rudderstack/integrations-lib'); const LOGLEVELS = { debug: 0, // Most verbose logging level @@ -17,10 +15,16 @@ const loggerImpl = process.env.LOGGER_IMPL ?? 'winston'; let logLevel = process.env.LOG_LEVEL ?? 'error'; +const logger = structuredLogger({ level: logLevel }); + +const getLogger = () => { + return loggerImpl === 'winston' ? logger : console; +}; + const setLogLevel = (level) => { const logger = getLogger(); logLevel = level || logLevel; - logger?.setLogLevel(`${loglevel}`); + logger?.setLogLevel(logLevel); }; /** @@ -75,10 +79,6 @@ const log = (logMethod, args) => { logMethod(message); }; -const getLogger = () => { - return loggerImpl === 'winston' ? logger : console; -}; - const debug = (...args) => { const logger = getLogger(); if (LOGLEVELS.debug >= logLevel) {