diff --git a/env.md b/env.md index f21064171..b401f80ce 100644 --- a/env.md +++ b/env.md @@ -25,6 +25,9 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/ - `MAX_REQ_PER_SECOND`: Number of requests per second allowed by the same client. Example: `3` - `MAX_CHECKSUM_LENGTH`: Define the maximum length for a file if checksum is required (Mb). Example: `10` - `LOG_LEVEL`: Define the default log level. Example: `debug` +- `LOG_CONSOLE`: Write logs to the console. Default is `false` +- `LOG_FILES`: Write logs to files. Default is `false` +- `LOG_DB`: Write logs to noSQL database. Default is `true` ## HTTP diff --git a/src/components/database/index.ts b/src/components/database/index.ts index 508751f4a..c9fae137b 100644 --- a/src/components/database/index.ts +++ b/src/components/database/index.ts @@ -1014,11 +1014,10 @@ export class Database { // 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) { - console.log('DB USE_DB_TRANSPORT ', USE_DB_TRANSPORT) configureCustomDBTransport(this, DATABASE_LOGGER) } else { DATABASE_LOGGER.warn( - '"NODE_ENV" is set to "development". This means logs will be saved to console and file(s) only.' + 'Property "LOG_DB" is set to "false". This means logs will NOT be saved to database!' ) } return (async (): Promise => { diff --git a/src/test/integration/logs.test.ts b/src/test/integration/logs.test.ts index fc83c2d93..840b04f1b 100644 --- a/src/test/integration/logs.test.ts +++ b/src/test/integration/logs.test.ts @@ -42,7 +42,6 @@ describe('LogDatabase CRUD', () => { 'meta' ) logId = result?.id // Save the auto-generated id for further operations - console.log('log id: ' + logId) }) it('retrieve log', async () => { @@ -72,14 +71,12 @@ describe('LogDatabase CRUD', () => { // Retrieve the latest log entry const logs = await database.logs.retrieveMultipleLogs(startTime, endTime, 1) - console.log('logs are: ', logs) expect(logs?.length).to.equal(1) - expect(logs?.[0].id).to.equal(String(Number(logId) + 1)) + expect(logs?.[0].id).to.greaterThan(Number(logId)) expect(logs?.[0].level).to.equal(newLogEntry.level) expect(logs?.[0].message).to.equal(newLogEntry.message) expect(logs?.[0].moduleName).to.equal('HTTP') - console.log('end debug') }) it('should save a log in the database when a log.logMessage is called', async () => { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 491bf0d47..3b34f6988 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -275,6 +275,24 @@ export const ENVIRONMENT_VARIABLES: Record = { name: 'LOG_LEVEL', value: process.env.LOG_LEVEL, required: false + }, + LOG_CONSOLE: { + // log to console output? + name: 'LOG_CONSOLE', + value: process.env.LOG_CONSOLE, + required: false + }, + LOG_FILES: { + // log to files? + name: 'LOG_FILES', + value: process.env.LOG_FILES, + required: false + }, + LOG_DB: { + // log to DB? + name: 'LOG_DB', + value: process.env.LOG_DB, + required: false // default is true } } diff --git a/src/utils/logging/Logger.ts b/src/utils/logging/Logger.ts index ef3d34e18..35729dd0f 100644 --- a/src/utils/logging/Logger.ts +++ b/src/utils/logging/Logger.ts @@ -141,9 +141,10 @@ const USE_CONSOLE_TRANSPORT: boolean = process.env.LOG_CONSOLE && process.env.LOG_CONSOLE !== 'false' const USE_FILE_TRANSPORT: boolean = process.env.LOG_FILES && process.env.LOG_FILES !== 'false' -// can be affected by configuration -export const USE_DB_TRANSPORT: boolean = - true || (process.env.LOG_DB && process.env.LOG_DB !== 'false') +// default to true, if not explicitly set otherwise +export const USE_DB_TRANSPORT: boolean = !( + process.env.LOG_DB && process.env.LOG_DB === 'false' +) // if not set, then gets default 'development' level & colors export function isDevelopmentEnvironment(): boolean {