Skip to content

Commit

Permalink
refactor: use global process instead of importing
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina committed Nov 22, 2022
1 parent 395da6c commit f35a732
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
21 changes: 10 additions & 11 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs';
import { env } from 'process';
import type { Writable } from 'stream';

import { getUint } from './connection_string';
Expand Down Expand Up @@ -109,22 +108,22 @@ export class Logger {
trace(component: any, message: any): void {}

static resolveOptions(clientOptions?: LoggerMongoClientOptions): LoggerOptions {
const defaultSeverity = toValidSeverity(env.MONGODB_LOG_ALL) ?? SeverityLevel.OFF;
const defaultSeverity = toValidSeverity(process.env.MONGODB_LOG_ALL) ?? SeverityLevel.OFF;

return {
command: toValidSeverity(env.MONGODB_LOG_COMMAND) ?? defaultSeverity,
topology: toValidSeverity(env.MONGODB_LOG_TOPOLOGY) ?? defaultSeverity,
serverSelection: toValidSeverity(env.MONGODB_LOG_SERVER_SELECTION) ?? defaultSeverity,
connection: toValidSeverity(env.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
command: toValidSeverity(process.env.MONGODB_LOG_COMMAND) ?? defaultSeverity,
topology: toValidSeverity(process.env.MONGODB_LOG_TOPOLOGY) ?? defaultSeverity,
serverSelection: toValidSeverity(process.env.MONGODB_LOG_SERVER_SELECTION) ?? defaultSeverity,
connection: toValidSeverity(process.env.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
defaultSeverity,
maxDocumentLength:
typeof env.MONGODB_LOG_MAX_DOCUMENT_LENGTH === 'string' &&
env.MONGODB_LOG_MAX_DOCUMENT_LENGTH !== ''
? getUint('MONGODB_LOG_MAX_DOCUMENT_LENGTH', env.MONGODB_LOG_MAX_DOCUMENT_LENGTH)
typeof process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH === 'string' &&
process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH !== ''
? getUint('MONGODB_LOG_MAX_DOCUMENT_LENGTH', process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH)
: 1000,
logPath:
typeof env.MONGODB_LOG_PATH === 'string' && env.MONGODB_LOG_PATH !== ''
? env.MONGODB_LOG_PATH
typeof process.env.MONGODB_LOG_PATH === 'string' && process.env.MONGODB_LOG_PATH !== ''
? process.env.MONGODB_LOG_PATH
: clientOptions?.mongodbLogPath ?? 'stderr'
};
}
Expand Down
9 changes: 4 additions & 5 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const os = require('os');
const fs = require('fs');
const { env } = require('process');
const { expect } = require('chai');
const { getSymbolFrom } = require('../tools/utils');
const { parseOptions, resolveSRVRecord } = require('../../src/connection_string');
Expand Down Expand Up @@ -861,19 +860,19 @@ describe('MongoOptions', function () {

for (const name of severityVars) {
it(`should enable logging if at least ${name} is set to a valid value`, function () {
env[name] = SeverityLevel.CRITICAL;
process.env[name] = SeverityLevel.CRITICAL;
const client = new MongoClient('mongodb://localhost:27017');
expect(client.mongoLogger).to.be.instanceOf(Logger);
env[name] = undefined;
process.env[name] = undefined;
});
}

for (const name of severityVars) {
it(`should not enable logging if ${name} is set to an invalid value`, function () {
env[name] = 'invalid';
process.env[name] = 'invalid';
const client = new MongoClient('mongodb://localhost:27017');
expect(client).property('mongoLogger', null);
env[name] = undefined;
process.env[name] = undefined;
});
}
});
Expand Down

0 comments on commit f35a732

Please sign in to comment.