Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 22, 2024
1 parent 5f191bd commit 1c64966
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 36 deletions.
5 changes: 2 additions & 3 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { TypesenseSearchParams } from '../../@types/index.js'
import {
LOG_LEVELS_STR,
configureCustomDBTransport,
GENERIC_EMOJIS,
USE_DB_TRANSPORT
GENERIC_EMOJIS
} from '../../utils/logging/Logger.js'
import { DATABASE_LOGGER } from '../../utils/logging/common.js'
import { validateObject } from '../core/utils/validateDdoHandler.js'
Expand Down Expand Up @@ -1019,7 +1018,7 @@ export class Database {
// add this DB transport too
// once we create a DB instance, the logger will be using this transport as well
// we cannot have this the other way around because of the dependencies cycle
if (USE_DB_TRANSPORT) {
if (ENVIRONMENT_VARIABLES.LOG_DB.value === 'true') {
configureCustomDBTransport(this, DATABASE_LOGGER)
} else {
DATABASE_LOGGER.warn(
Expand Down
9 changes: 6 additions & 3 deletions src/test/unit/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ describe('Logger instances and transports tests', async () => {
// need to do it first
envOverrides = await setupEnvironment(
null,
buildEnvOverrideConfig([ENVIRONMENT_VARIABLES.NODE_ENV], ['development'])
buildEnvOverrideConfig(
[ENVIRONMENT_VARIABLES.NODE_ENV, ENVIRONMENT_VARIABLES.LOG_DB],
['development', 'false']
)
)
// because of this
it('should be development environment', () => {
Expand All @@ -43,14 +46,14 @@ describe('Logger instances and transports tests', async () => {
null,
buildEnvOverrideConfig(
[ENVIRONMENT_VARIABLES.LOG_DB, ENVIRONMENT_VARIABLES.DB_URL],
['true', 'http://172.15.0.6:8108?apiKey=xyz']
['true', 'http://localhost:8108?apiKey=xyz']
)
)
expect(process.env.LOG_DB).to.be.equal('true')
// will build the DB transport layer
const config = await getConfiguration(true)
// eslint-disable-next-line no-unused-vars
const DB = new Database(config.dbConfig)
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')

Expand Down
34 changes: 4 additions & 30 deletions src/utils/logging/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Transport from 'winston-transport'
import DailyRotateFile from 'winston-daily-rotate-file'
import fs from 'fs'
import { Database } from '../../components/database/index.js'
import { ENVIRONMENT_VARIABLES } from '../constants.js'

// all the types of modules/components
export const LOGGER_MODULE_NAMES = {
Expand Down Expand Up @@ -134,26 +135,6 @@ export class CustomOceanNodesTransport extends Transport {
}
}

/**
* Avoid these annoyng typesense log message (too much garbage and no utility)
* @param loggerModuleName module name
* @param logMessage the actual message
* @returns boolean
*/
// function isTypesenseIgnoreLogMessage(loggerModuleName: string, logMessage: string) {
// const msg1: string = 'Response Code was 200.'
// const msg2: string = 'Response Code was 201.'
// const msg3 = 'request Try #1 to Node'
// const msg4 = 'Request /collections/'
// return (
// loggerModuleName.toLowerCase() === LOGGER_MODULE_NAMES.DATABASE &&
// (logMessage.includes(msg2) ||
// logMessage.includes(msg3) ||
// logMessage.includes(msg1) ||
// logMessage.includes(msg4))
// )
// }

let INSTANCE_COUNT = 0
let customDBTransport: CustomOceanNodesTransport = null

Expand All @@ -164,8 +145,7 @@ export const NUM_LOGGER_INSTANCES = INSTANCE_COUNT
const USE_FILE_TRANSPORT: boolean =
process.env.LOG_FILES && process.env.LOG_FILES !== 'false'

export const USE_DB_TRANSPORT: boolean =
process.env.LOG_DB && process.env.LOG_DB !== 'false'
const USE_DB_TRANSPORT: boolean = process.env.LOG_DB && process.env.LOG_DB !== 'false'

// default to true, if not explicitly set otherwise AND no other locations defined
const USE_CONSOLE_TRANSPORT: boolean =
Expand Down Expand Up @@ -471,19 +451,13 @@ export class CustomNodeLogger {
) {
// lazy check db custom transport, needed beacause of dependency cycles
if (
customDBTransport !== null && // if null then what?
USE_DB_TRANSPORT &&
customDBTransport !== null &&
(USE_DB_TRANSPORT || ENVIRONMENT_VARIABLES.LOG_DB.value === 'true') &&
!this.hasDBTransport()
) {
this.addTransport(customDBTransport)
}

// if (!isLogLevelLogable(level)) {
// return
// }

// ignore tons of typesense garbage
// if (!isTypesenseIgnoreLogMessage(this.getModuleName(), message)) {
this.getLogger().log(
level,
includeModuleName ? this.buildMessage(message) : message,
Expand Down

0 comments on commit 1c64966

Please sign in to comment.