From 639718399d91933e69315fee143b709c64aff22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Mon, 30 Oct 2023 08:19:03 +0100 Subject: [PATCH] Use different logger to make sure heartbeat is enabled --- packages/pusher/src/heartbeat/heartbeat.test.ts | 7 ++++--- packages/pusher/src/heartbeat/heartbeat.ts | 9 +++++---- packages/pusher/src/heartbeat/logger.ts | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 packages/pusher/src/heartbeat/logger.ts diff --git a/packages/pusher/src/heartbeat/heartbeat.test.ts b/packages/pusher/src/heartbeat/heartbeat.test.ts index 91ed4fcb..b7764188 100644 --- a/packages/pusher/src/heartbeat/heartbeat.test.ts +++ b/packages/pusher/src/heartbeat/heartbeat.test.ts @@ -1,10 +1,11 @@ import * as promiseUtilsModule from '@api3/promise-utils'; import { config, parseHeartbeatLog } from '../../test/fixtures'; -import { logger } from '../logger'; import * as stateModule from '../state'; import { loadRawConfig } from '../validation/config'; +import { heartbeatLogger } from './logger'; + import { initiateHeartbeat, logHeartbeat, createHash } from '.'; // eslint-disable-next-line jest/no-hooks @@ -30,12 +31,12 @@ describe(logHeartbeat.name, () => { it('sends the correct heartbeat log', async () => { const state = stateModule.getInitialState(config); jest.spyOn(stateModule, 'getState').mockReturnValue(state); - jest.spyOn(logger, 'info'); + jest.spyOn(heartbeatLogger, 'info').mockImplementation(); jest.advanceTimersByTime(1000 * 3); // Advance time by 3 seconds to ensure the timestamp of the log is different from deployment timestamp. await logHeartbeat(); - expect(logger.info).toHaveBeenCalledWith(expectedLogMessage); + expect(heartbeatLogger.info).toHaveBeenCalledWith(expectedLogMessage); }); it('the heartbeat log can be parsed', () => { diff --git a/packages/pusher/src/heartbeat/heartbeat.ts b/packages/pusher/src/heartbeat/heartbeat.ts index e1712878..974e1894 100644 --- a/packages/pusher/src/heartbeat/heartbeat.ts +++ b/packages/pusher/src/heartbeat/heartbeat.ts @@ -5,6 +5,8 @@ import { logger } from '../logger'; import { getState } from '../state'; import { loadRawConfig } from '../validation/config'; +import { heartbeatLogger } from './logger'; + export const initiateHeartbeat = () => { logger.debug('Initiating heartbeat loop'); setInterval(async () => { @@ -47,8 +49,7 @@ export const logHeartbeat = async () => { const heartbeatSignature = await signHeartbeat(airnodeWallet, heartbeatPayload); const heartbeatLog = [...heartbeatPayload, heartbeatSignature].join(' - '); - // We ensure in config validation that INFO level logs are not disabled. The logs are sent to API3 for validation - // (that the data provider deployed deployed the correct configuration) and monitoring purposes (whether the instance - // is running). - logger.info(heartbeatLog); + // The logs are sent to API3 for validation (that the data provider deployed deployed the correct configuration) and + // monitoring purposes (whether the instance is running). + heartbeatLogger.info(heartbeatLog); }; diff --git a/packages/pusher/src/heartbeat/logger.ts b/packages/pusher/src/heartbeat/logger.ts new file mode 100644 index 00000000..0486cdda --- /dev/null +++ b/packages/pusher/src/heartbeat/logger.ts @@ -0,0 +1,15 @@ +import { createLogger } from '@api3/commons'; + +import { loadEnv } from '../validation/env'; + +// We need to load the environment variables before we can use the logger. Because we want the logger to always be +// available, we load the environment variables as a side effect during the module import. +const env = loadEnv(); + +export const heartbeatLogger = createLogger({ + colorize: env.LOG_COLORIZE, + format: env.LOG_FORMAT, + // We make sure the heartbeat logger is always enabled and logs all levels. + enabled: true, + minLevel: 'debug', +});