Skip to content

Commit

Permalink
more log changes, check log level for decide to log or not
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 17, 2024
1 parent 78a26d3 commit 7edee9b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/test/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 51 additions & 9 deletions src/test/integration/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@ 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'
}
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'
}
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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', () => {
Expand All @@ -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'
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -429,4 +467,8 @@ describe('LogDatabase retrieveMultipleLogs with pagination', () => {
)
assert.isEmpty(logs, 'Expected logs to be empty')
})

after(async () => {
await tearDownEnvironment(previousConfiguration)
})
})
29 changes: 29 additions & 0 deletions src/utils/logging/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7edee9b

Please sign in to comment.