From 5ed690c5e43d18359114d4162e1883f9ba0359fc Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Thu, 25 Jul 2024 14:59:14 +0100 Subject: [PATCH] restore test, add functionality to remove db transport if not needed anymore --- src/test/unit/logging.test.ts | 42 ++++++++++++++++++++++++++++++++++- src/utils/logging/Logger.ts | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/test/unit/logging.test.ts b/src/test/unit/logging.test.ts index 0f920cb83..b49b812e6 100644 --- a/src/test/unit/logging.test.ts +++ b/src/test/unit/logging.test.ts @@ -1,17 +1,23 @@ -import { ENVIRONMENT_VARIABLES } from '../../utils/index.js' +import { Database } from '../../components/database/index.js' +import { ENVIRONMENT_VARIABLES, getConfiguration } from '../../utils/index.js' import { expect } from 'chai' import { + DEFAULT_TEST_TIMEOUT, OverrideEnvConfig, buildEnvOverrideConfig, setupEnvironment, tearDownEnvironment } from '../utils/utils.js' import { + CustomOceanNodesTransport, MAX_LOGGER_INSTANCES, NUM_LOGGER_INSTANCES, + USE_DB_TRANSPORT, isDevelopmentEnvironment } from '../../utils/logging/Logger.js' +import { OCEAN_NODE_LOGGER } from '../../utils/logging/common.js' +import winston from 'winston' let envOverrides: OverrideEnvConfig[] @@ -40,6 +46,40 @@ describe('Logger instances and transports tests', async () => { expect(numExistingInstances).to.be.lessThanOrEqual(MAX_LOGGER_INSTANCES) }) + it(`should change LOG_DB to "true" and logger should have DB transport`, async function () { + // when we are logging to DB, things can slow down a bit + this.timeout(DEFAULT_TEST_TIMEOUT * 3) + expect(USE_DB_TRANSPORT()).to.be.equal(false) + expect(OCEAN_NODE_LOGGER.hasDBTransport()).to.be.equal(false) + const envAfter = await setupEnvironment( + null, + buildEnvOverrideConfig( + [ENVIRONMENT_VARIABLES.LOG_DB, ENVIRONMENT_VARIABLES.DB_URL], + ['true', 'http://localhost:8108/?apiKey=xyz'] + ) + ) + expect(USE_DB_TRANSPORT()).to.be.equal(true) + // will build the DB transport layer + const config = await getConfiguration(true) + // eslint-disable-next-line no-unused-vars + const DB = await new Database(config.dbConfig) + // Could generate Typesene error if DB is not running, but does not matter for this test + OCEAN_NODE_LOGGER.logMessage('Should build DB transport layer') + + expect(OCEAN_NODE_LOGGER.hasDBTransport()).to.be.equal(true) + + const transports: winston.transport[] = OCEAN_NODE_LOGGER.getTransports().filter( + (transport: winston.transport) => { + return transport instanceof CustomOceanNodesTransport + } + ) + expect(transports.length).to.be.equal(1) + OCEAN_NODE_LOGGER.removeTransport(transports[0]) + expect(OCEAN_NODE_LOGGER.hasDBTransport()).to.be.equal(false) + await tearDownEnvironment(envAfter) + expect(USE_DB_TRANSPORT()).to.be.equal(false) + }) + after(() => { // Restore original local setup / env variables after test tearDownEnvironment(envOverrides) diff --git a/src/utils/logging/Logger.ts b/src/utils/logging/Logger.ts index d7dd72637..df5b7df0b 100644 --- a/src/utils/logging/Logger.ts +++ b/src/utils/logging/Logger.ts @@ -458,6 +458,9 @@ export class CustomNodeLogger { // lazy check db custom transport, needed beacause of dependency cycles if (customDBTransport !== null && USE_DB_TRANSPORT() && !this.hasDBTransport()) { this.addTransport(customDBTransport) + } else if (this.hasDBTransport() && !USE_DB_TRANSPORT()) { + this.logMessage('Removing DB transport from Logger: ' + this.getModuleName()) + this.removeTransport(this.getDBTransport()) } this.getLogger().log(