From 7edee9b8476fb9c48ec1938565db41b55cdac5f6 Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Wed, 17 Jul 2024 17:27:01 +0100 Subject: [PATCH] more log changes, check log level for decide to log or not --- src/test/.env.test | 1 + src/test/integration/logs.test.ts | 60 ++++++++++++++++++++++++++----- src/utils/logging/Logger.ts | 29 +++++++++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/src/test/.env.test b/src/test/.env.test index 48115ee1f..ee100d2a9 100644 --- a/src/test/.env.test +++ b/src/test/.env.test @@ -9,3 +9,4 @@ NODE1_PRIVATE_KEY=0xcb345bd2b11264d523ddaf383094e2675c420a17511c3102a53817f13474 NODE2_PRIVATE_KEY=0x3634cc4a3d2694a1186a7ce545f149e022eea103cc254d18d08675104bb4b5ac INDEXER_INTERVAL=9000 ADDRESS_FILE=${HOME}/.ocean/ocean-contracts/artifacts/address.json +LOG_DB=false diff --git a/src/test/integration/logs.test.ts b/src/test/integration/logs.test.ts index 1e2cedc7a..4195d792e 100644 --- a/src/test/integration/logs.test.ts +++ b/src/test/integration/logs.test.ts @@ -7,13 +7,22 @@ import { configureCustomDBTransport, getCustomLoggerForModule } from '../../utils/logging/Logger.js' +import { ENVIRONMENT_VARIABLES } from '../../utils/constants.js' +import { + buildEnvOverrideConfig, + OverrideEnvConfig, + setupEnvironment, + tearDownEnvironment +} from '../utils/utils.js' + +let previousConfiguration: OverrideEnvConfig[] describe('LogDatabase CRUD', () => { let database: Database let logger: CustomNodeLogger const logEntry = { timestamp: Date.now(), - level: 'info', + level: 'debug', message: `Test log message ${Date.now()}`, moduleName: 'testModule-1', meta: 'Test meta information' @@ -21,6 +30,10 @@ describe('LogDatabase CRUD', () => { let logId: string // Variable to store the ID of the created log entry before(async () => { + previousConfiguration = await setupEnvironment( + null, + buildEnvOverrideConfig([ENVIRONMENT_VARIABLES.LOG_DB], [true]) + ) const dbConfig = { url: 'http://localhost:8108/?apiKey=xyz' } @@ -56,7 +69,7 @@ describe('LogDatabase CRUD', () => { it('should save a log in the database when a log event is triggered', async () => { const newLogEntry = { timestamp: Date.now(), - level: 'info', + level: 'debug', message: `NEW Test log message ${Date.now()}` } // Trigger a log event which should be saved in the database @@ -82,7 +95,7 @@ describe('LogDatabase CRUD', () => { it('should save a log in the database when a log.logMessage is called', async () => { const newLogEntry = { timestamp: Date.now(), - level: 'info', + level: 'debug', message: `logMessage: Test log message ${Date.now()}`, moduleName: 'testModule-3', meta: 'Test meta information' @@ -111,7 +124,7 @@ describe('LogDatabase CRUD', () => { it('should save a log in the database when a log.logMessageWithEmoji is called', async () => { const newLogEntry = { timestamp: Date.now(), - level: 'info', + level: 'debug', message: `logMessageWithEmoji: Test log message ${Date.now()}`, moduleName: 'testModule-4', meta: 'Test meta information' @@ -136,6 +149,10 @@ describe('LogDatabase CRUD', () => { assert(logs?.[0].message) expect(logs?.[0].moduleName).to.equal('HTTP') }) + + after(async () => { + await tearDownEnvironment(previousConfiguration) + }) }) describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { @@ -145,6 +162,11 @@ describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { const endTime = new Date() // now before(async () => { + previousConfiguration = await setupEnvironment( + null, + buildEnvOverrideConfig([ENVIRONMENT_VARIABLES.LOG_DB], [true]) + ) + const dbConfig = { url: 'http://localhost:8108/?apiKey=xyz' } @@ -165,7 +187,7 @@ describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { }) it('should retrieve logs with a specific level', async () => { - const level = 'info' + const level = 'debug' const logs = await database.logs.retrieveMultipleLogs( startTime, endTime, @@ -178,7 +200,7 @@ describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { it('should retrieve logs with both a specific moduleName and level', async () => { const moduleName = 'testModule-1' - const level = 'info' + const level = 'debug' const logs = await database.logs.retrieveMultipleLogs( startTime, endTime, @@ -205,7 +227,7 @@ describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { let database: Database const logEntry = { timestamp: Date.now(), - level: 'info', + level: 'debug', message: 'Test log message for single deletion', moduleName: 'testModule-2', meta: 'Test meta information for single deletion' @@ -290,26 +312,34 @@ describe('LogDatabase retrieveMultipleLogs with specific parameters', () => { const elapsedTimeInMs = endPerfTime[0] * 1000 + endPerfTime[1] / 1e6 expect(elapsedTimeInMs).to.be.below(1000) // threshold }) + + after(async () => { + await tearDownEnvironment(previousConfiguration) + }) }) describe('LogDatabase deleteOldLogs', () => { let database: Database const oldLogEntry = { timestamp: new Date().getTime() - 31 * 24 * 60 * 60 * 1000, // 31 days ago - level: 'info', + level: 'debug', message: 'Old log message for deletion test', moduleName: 'testModule-1', meta: 'Test meta information' } const recentLogEntry = { timestamp: new Date().getTime(), // current time - level: 'info', + level: 'debug', message: 'Recent log message not for deletion', moduleName: 'testModule-1', meta: 'Test meta information' } before(async () => { + previousConfiguration = await setupEnvironment( + null, + buildEnvOverrideConfig([ENVIRONMENT_VARIABLES.LOG_DB], [true]) + ) const dbConfig = { url: 'http://localhost:8108/?apiKey=xyz' } @@ -358,12 +388,20 @@ describe('LogDatabase deleteOldLogs', () => { const recentLogPresent = logs?.some((log) => log.message === recentLogEntry.message) assert(recentLogPresent === true, 'Recent logs are not present') }) + + after(async () => { + await tearDownEnvironment(previousConfiguration) + }) }) describe('LogDatabase retrieveMultipleLogs with pagination', () => { let database: Database const logCount = 10 // Total number of logs to insert and also the limit for logs per page before(async () => { + previousConfiguration = await setupEnvironment( + null, + buildEnvOverrideConfig([ENVIRONMENT_VARIABLES.LOG_DB], [true]) + ) const dbConfig = { url: 'http://localhost:8108/?apiKey=xyz' } @@ -429,4 +467,8 @@ describe('LogDatabase retrieveMultipleLogs with pagination', () => { ) assert.isEmpty(logs, 'Expected logs to be empty') }) + + after(async () => { + await tearDownEnvironment(previousConfiguration) + }) }) diff --git a/src/utils/logging/Logger.ts b/src/utils/logging/Logger.ts index 6fecfdd8c..27e8c854c 100644 --- a/src/utils/logging/Logger.ts +++ b/src/utils/logging/Logger.ts @@ -114,6 +114,10 @@ export class CustomOceanNodesTransport extends Transport { meta: JSON.stringify(info.meta) // Ensure meta is a string } + if (!isLogLevelLogable(document.level)) { + return + } + try { // Use the insertLog method of the LogDatabase instance if ( @@ -195,6 +199,27 @@ export const getDefaultLevel = (): string => { ) } +// only log if >= configured level +function isLogLevelLogable(level: string): boolean { + const configured: string = getDefaultLevel() + let configuredLevelNum = -1 + let currentLevelNum = -1 + for (const [key, value] of Object.entries(LOG_LEVELS_NUM)) { + if ((key as string) === configured) { + configuredLevelNum = value + } + if ((key as string) === level) { + currentLevelNum = value + } + + if (currentLevelNum > -1 && configuredLevelNum > -1) { + break + } + } + + return currentLevelNum >= configuredLevelNum +} + if (isDevelopmentEnvironment()) { winston.addColors(LOG_COLORS) } @@ -476,6 +501,10 @@ export class CustomNodeLogger { this.addTransport(customDBTransport) } + if (!isLogLevelLogable(level)) { + return + } + // ignore tons of typesense garbage if (!isTypesenseIgnoreLogMessage(this.getModuleName(), message)) { this.getLogger().log(