Skip to content

Commit

Permalink
chore: call env load before importing logger
Browse files Browse the repository at this point in the history
- set level as part of initialisation
  • Loading branch information
Sai Sankeerth committed Jun 12, 2024
1 parent efdb9c5 commit 365f738
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -32,7 +35,7 @@ app.use(
addRequestSizeMiddleware(app);
addSwaggerRoutes(app);

logger.info('Using new routes');
logger.error('Using new routes');
applicationRoutes(app);

function finalFunction() {
Expand Down
16 changes: 8 additions & 8 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
};

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 365f738

Please sign in to comment.