Skip to content

Commit 6f07729

Browse files
committed
feat(mongodb-log-writer): add ability to disable and enable logging
Adds a new isDisabled field to disable the writing of logs. This can be modified using the new enable() and disable() methods.
1 parent 01e5c1c commit 6f07729

File tree

2 files changed

+136
-11
lines changed

2 files changed

+136
-11
lines changed

packages/mongodb-log-writer/src/mongo-log-writer.spec.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,107 @@ import stream from 'stream';
55
import { inspect } from 'util';
66
import chai, { expect } from 'chai';
77
import sinonChai from 'sinon-chai';
8+
import sinon from 'sinon';
89
chai.use(sinonChai);
910

1011
describe('MongoLogWriter', function () {
12+
const now = new Date(1628591965386);
13+
14+
describe('enabling and disabling', function () {
15+
let writer: MongoLogWriter;
16+
let target: stream.PassThrough;
17+
let writeSpy: sinon.SinonSpy;
18+
19+
const SEVERITIES_COUNT = 5;
20+
21+
function logAllSeverities(writer: MongoLogWriter) {
22+
writer.info('component', mongoLogId(12345), 'context', 'message', {});
23+
writer.warn('component', mongoLogId(12345), 'context', 'message', {});
24+
writer.error('component', mongoLogId(12345), 'context', 'message', {});
25+
writer.debug('component', mongoLogId(12345), 'context', 'message', {});
26+
writer.fatal('component', mongoLogId(12345), 'context', 'message', {});
27+
}
28+
29+
beforeEach(function () {
30+
target = new stream.PassThrough().setEncoding('utf8');
31+
writer = new MongoLogWriter('logid', null, target, () => now);
32+
writeSpy = sinon.spy(writer, 'write');
33+
});
34+
35+
afterEach(function () {
36+
sinon.restore();
37+
});
38+
39+
it('is enabled by default', async function () {
40+
expect(writer.isDisabled).to.equal(false);
41+
42+
writer.info('component', mongoLogId(12345), 'context', 'message', {});
43+
44+
await writer.flush();
45+
46+
expect(target.read()).is.not.null;
47+
expect(writeSpy).callCount(1);
48+
});
49+
50+
it('can be disabled on initialization', async function () {
51+
const disabledWriter = new MongoLogWriter(
52+
'logid',
53+
null,
54+
target,
55+
() => now,
56+
{
57+
isDisabled: true,
58+
}
59+
);
60+
61+
expect(disabledWriter.isDisabled).to.equal(true);
62+
logAllSeverities(disabledWriter);
63+
64+
await disabledWriter.flush();
65+
66+
expect(target.read()).is.null;
67+
expect(writeSpy).not.called;
68+
});
69+
70+
it('can run disable() to disable logging across all severities', function () {
71+
expect(writer.isDisabled).to.equal(false);
72+
73+
logAllSeverities(writer);
74+
75+
expect(writeSpy).callCount(SEVERITIES_COUNT);
76+
77+
writer.disable();
78+
79+
expect(writer.isDisabled).to.equal(true);
80+
81+
logAllSeverities(writer);
82+
83+
expect(writeSpy).callCount(SEVERITIES_COUNT);
84+
});
85+
86+
it('can run enable() after being disabled', async function () {
87+
expect(writer.isDisabled).to.equal(false);
88+
89+
writer.disable();
90+
expect(writer.isDisabled).to.equal(true);
91+
92+
logAllSeverities(writer);
93+
94+
expect(writeSpy).not.called;
95+
96+
writer.enable();
97+
expect(writer.isDisabled).to.equal(false);
98+
99+
logAllSeverities(writer);
100+
101+
await writer.flush();
102+
103+
expect(target.read()).not.null;
104+
expect(writeSpy).callCount(SEVERITIES_COUNT);
105+
});
106+
});
107+
11108
it('allows writing log messages to a stream', async function () {
12-
const now = new Date(1628591965386);
13109
const target = new stream.PassThrough().setEncoding('utf8');
14110
const writer = new MongoLogWriter('logid', null, target, () => now);
15111
const logEvents: MongoLogEntry[] = [];

packages/mongodb-log-writer/src/mongo-log-writer.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ function validateLogEntry(info: MongoLogEntry): Error | null {
6363
return null;
6464
}
6565

66+
export type MongoLogWriterOptions = {
67+
isDisabled?: boolean;
68+
};
69+
6670
/**
6771
* A helper class for writing formatted log information to an output stream.
6872
* This class itself is an object-mode Writable stream to which
@@ -72,10 +76,11 @@ function validateLogEntry(info: MongoLogEntry): Error | null {
7276
* the target stream.
7377
*/
7478
export class MongoLogWriter extends Writable {
75-
_logId: string;
76-
_logFilePath: string | null;
77-
_target: PlainWritable;
78-
_now: () => Date;
79+
private _logId: string;
80+
private _logFilePath: string | null;
81+
private _target: PlainWritable;
82+
private _now: () => Date;
83+
private _isDisabled: boolean;
7984

8085
/**
8186
* @param logId A unique identifier for this log file. This is not used outside the `logId` getter.
@@ -87,13 +92,15 @@ export class MongoLogWriter extends Writable {
8792
logId: string,
8893
logFilePath: string | null,
8994
target: PlainWritable,
90-
now?: () => Date
95+
now?: () => Date,
96+
{ isDisabled = false }: MongoLogWriterOptions = {}
9197
) {
9298
super({ objectMode: true });
9399
this._logId = logId;
94100
this._logFilePath = logFilePath;
95101
this._target = target;
96102
this._now = now ?? (() => new Date());
103+
this._isDisabled = isDisabled;
97104
}
98105

99106
/** Return the logId passed to the constructor. */
@@ -111,6 +118,21 @@ export class MongoLogWriter extends Writable {
111118
return this._target;
112119
}
113120

121+
/**
122+
* Return whether the logger is currently disabled.
123+
*/
124+
get isDisabled() {
125+
return this._isDisabled;
126+
}
127+
128+
disable(): void {
129+
this._isDisabled = true;
130+
}
131+
132+
enable(): void {
133+
this._isDisabled = false;
134+
}
135+
114136
_write(
115137
info: MongoLogEntry,
116138
encoding: unknown,
@@ -186,6 +208,13 @@ export class MongoLogWriter extends Writable {
186208
await new Promise((resolve) => this._target.write('', resolve));
187209
}
188210

211+
writeIfEnabled(chunk: Parameters<typeof this.write>[0]): void {
212+
if (this._isDisabled) {
213+
return;
214+
}
215+
this.write(chunk);
216+
}
217+
189218
/**
190219
* Write a log entry with severity 'I'.
191220
*/
@@ -204,7 +233,7 @@ export class MongoLogWriter extends Writable {
204233
msg: message,
205234
attr: attr,
206235
};
207-
this.write(logEntry);
236+
this.writeIfEnabled(logEntry);
208237
}
209238

210239
/**
@@ -225,7 +254,7 @@ export class MongoLogWriter extends Writable {
225254
msg: message,
226255
attr: attr,
227256
};
228-
this.write(logEntry);
257+
this.writeIfEnabled(logEntry);
229258
}
230259

231260
/**
@@ -246,7 +275,7 @@ export class MongoLogWriter extends Writable {
246275
msg: message,
247276
attr: attr,
248277
};
249-
this.write(logEntry);
278+
this.writeIfEnabled(logEntry);
250279
}
251280

252281
/**
@@ -267,7 +296,7 @@ export class MongoLogWriter extends Writable {
267296
msg: message,
268297
attr: attr,
269298
};
270-
this.write(logEntry);
299+
this.writeIfEnabled(logEntry);
271300
}
272301

273302
/**
@@ -289,7 +318,7 @@ export class MongoLogWriter extends Writable {
289318
msg: message,
290319
attr: attr,
291320
};
292-
this.write(logEntry);
321+
this.writeIfEnabled(logEntry);
293322
}
294323

295324
/**

0 commit comments

Comments
 (0)