Skip to content

Commit

Permalink
fix test messing DB url + small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 25, 2024
1 parent bdc4e5d commit 6eb48c7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { TypesenseSearchParams } from '../../@types/index.js'
import {
LOG_LEVELS_STR,
configureCustomDBTransport,
GENERIC_EMOJIS
GENERIC_EMOJIS,
USE_DB_TRANSPORT
} from '../../utils/logging/Logger.js'
import { DATABASE_LOGGER } from '../../utils/logging/common.js'
import { validateObject } from '../core/utils/validateDdoHandler.js'
Expand Down Expand Up @@ -1012,7 +1013,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 (ENVIRONMENT_VARIABLES.LOG_DB.value === 'true') {
if (USE_DB_TRANSPORT()) {
configureCustomDBTransport(this, DATABASE_LOGGER)
} else {
DATABASE_LOGGER.warn(
Expand Down
2 changes: 1 addition & 1 deletion src/test/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ NODE1_PRIVATE_KEY=0xcb345bd2b11264d523ddaf383094e2675c420a17511c3102a53817f13474
NODE2_PRIVATE_KEY=0x3634cc4a3d2694a1186a7ce545f149e022eea103cc254d18d08675104bb4b5ac
INDEXER_INTERVAL=9000
ADDRESS_FILE=${HOME}/.ocean/ocean-contracts/artifacts/address.json

LOG_LEVEL=debug
9 changes: 4 additions & 5 deletions src/test/unit/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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'
Expand Down Expand Up @@ -40,22 +41,20 @@ describe('Logger instances and transports tests', async () => {
})

it(`should change LOG_DB to "true" and logger should have DB transport`, async () => {
expect(process.env.LOG_DB).to.be.equal('false')
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']
['true', 'http://172.15.0.6:8108?apiKey=xyz']
)
)
expect(process.env.LOG_DB).to.be.equal('true')
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
console.log('CONFIG IS', config.dbConfig)
const DB = await new Database(config.dbConfig)
console.log(DB)
// 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
4 changes: 2 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export const ENVIRONMENT_VARIABLES: Record<any, EnvVariable> = {
required: false
},
LOG_CONSOLE: {
// log to console output?
// log to console output? true if no other bellow is set
name: 'LOG_CONSOLE',
value: process.env.LOG_CONSOLE,
required: false
Expand All @@ -292,7 +292,7 @@ export const ENVIRONMENT_VARIABLES: Record<any, EnvVariable> = {
// log to DB?
name: 'LOG_DB',
value: process.env.LOG_DB,
required: false // default is true
required: false
}
}

Expand Down
30 changes: 16 additions & 14 deletions src/utils/logging/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -142,15 +141,21 @@ export const MAX_LOGGER_INSTANCES = 10
export const NUM_LOGGER_INSTANCES = INSTANCE_COUNT

// log locations
const USE_FILE_TRANSPORT: boolean =
process.env.LOG_FILES && process.env.LOG_FILES !== 'false'
function USE_FILE_TRANSPORT(): boolean {
return process.env.LOG_FILES && process.env.LOG_FILES !== 'false'
}

const USE_DB_TRANSPORT: boolean = process.env.LOG_DB && process.env.LOG_DB !== 'false'
export function USE_DB_TRANSPORT(): boolean {
return 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 =
(process.env.LOG_CONSOLE && process.env.LOG_CONSOLE !== 'false') ||
(!USE_FILE_TRANSPORT && !USE_DB_TRANSPORT)
function USE_CONSOLE_TRANSPORT(): boolean {
return (
(process.env.LOG_CONSOLE && process.env.LOG_CONSOLE !== 'false') ||
(!USE_FILE_TRANSPORT() && !USE_DB_TRANSPORT())
)
}

// if not set, then gets default 'development' level & colors
export function isDevelopmentEnvironment(): boolean {
Expand Down Expand Up @@ -274,12 +279,13 @@ export function getDefaultLoggerTransports(
moduleOrComponentName: string
): winston.transport[] {
const transports: winston.transport[] = []
if (USE_FILE_TRANSPORT) {
// account for runtime changes done by tests (force read again value)
if (USE_FILE_TRANSPORT()) {
// always log to file
transports.push(buildCustomFileTransport(moduleOrComponentName))
}

if (USE_CONSOLE_TRANSPORT) {
if (USE_CONSOLE_TRANSPORT()) {
transports.push(defaultConsoleTransport)
}
return transports
Expand Down Expand Up @@ -450,11 +456,7 @@ export class CustomNodeLogger {
includeModuleName: boolean = false
) {
// lazy check db custom transport, needed beacause of dependency cycles
if (
customDBTransport !== null &&
(USE_DB_TRANSPORT || ENVIRONMENT_VARIABLES.LOG_DB.value === 'true') &&
!this.hasDBTransport()
) {
if (customDBTransport !== null && USE_DB_TRANSPORT() && !this.hasDBTransport()) {
this.addTransport(customDBTransport)
}

Expand Down

0 comments on commit 6eb48c7

Please sign in to comment.