Skip to content

Commit 447705f

Browse files
committed
feat!: add log writer options to LogManager and use settings objects instead of parameters
This is a breaking change which I believe would be useful for the future to create better consistency between LogManager and LogWriter and scale better as we add more parameters to log writer settings.
1 parent 6f07729 commit 447705f

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,17 @@ describe('MongoLogManager', function () {
213213
writer.end();
214214
await once(writer, 'finish');
215215
});
216+
217+
it('can create a disabled writer', async function () {
218+
const manager = new MongoLogManager({
219+
directory,
220+
retentionDays,
221+
onwarn,
222+
onerror,
223+
gzip: true,
224+
});
225+
const logWriter = await manager.createLogWriter({ isDisabled: true });
226+
227+
expect(logWriter.isDisabled).is.true;
228+
});
216229
});

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { once } from 'events';
44
import { createWriteStream, promises as fs } from 'fs';
55
import { createGzip, constants as zlibConstants } from 'zlib';
66
import { Heap } from 'heap-js';
7+
import type { MongoLogWriterOptions } from './mongo-log-writer';
78
import { MongoLogWriter } from './mongo-log-writer';
89
import { Writable } from 'stream';
910

1011
/** Options used by MongoLogManager instances. */
11-
interface MongoLogOptions {
12+
export interface MongoLogManagerOptions {
1213
/** A base directory in which log files are stored. */
1314
directory: string;
1415
/** Whether to write files as .gz files or not. */
@@ -29,9 +30,9 @@ interface MongoLogOptions {
2930
* naming convention `${logId}_log`.
3031
*/
3132
export class MongoLogManager {
32-
_options: MongoLogOptions;
33+
_options: MongoLogManagerOptions;
3334

34-
constructor(options: MongoLogOptions) {
35+
constructor(options: MongoLogManagerOptions) {
3536
this._options = options;
3637
}
3738

@@ -97,7 +98,9 @@ export class MongoLogManager {
9798
}
9899

99100
/** Create a MongoLogWriter stream for a new log file. */
100-
async createLogWriter(): Promise<MongoLogWriter> {
101+
async createLogWriter(
102+
writerOptions: Pick<MongoLogWriterOptions, 'isDisabled'> = {}
103+
): Promise<MongoLogWriter> {
101104
const logId = new ObjectId().toString();
102105
const doGzip = !!this._options.gzip;
103106
const logFilePath = path.join(
@@ -132,10 +135,15 @@ export class MongoLogManager {
132135
},
133136
});
134137
originalTarget = stream;
135-
logWriter = new MongoLogWriter(logId, null, stream);
138+
logWriter = new MongoLogWriter({
139+
...writerOptions,
140+
logId,
141+
logFilePath: null,
142+
target: stream,
143+
});
136144
}
137145
if (!logWriter) {
138-
logWriter = new MongoLogWriter(logId, logFilePath, stream);
146+
logWriter = new MongoLogWriter({ logId, logFilePath, target: stream });
139147
}
140148

141149
// We use 'log-finish' to give consumers an event that they can

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ describe('MongoLogWriter', function () {
2828

2929
beforeEach(function () {
3030
target = new stream.PassThrough().setEncoding('utf8');
31-
writer = new MongoLogWriter('logid', null, target, () => now);
31+
writer = new MongoLogWriter({
32+
logId: 'logid',
33+
logFilePath: null,
34+
target,
35+
now: () => now,
36+
});
3237
writeSpy = sinon.spy(writer, 'write');
3338
});
3439

@@ -48,15 +53,12 @@ describe('MongoLogWriter', function () {
4853
});
4954

5055
it('can be disabled on initialization', async function () {
51-
const disabledWriter = new MongoLogWriter(
52-
'logid',
53-
null,
56+
const disabledWriter = new MongoLogWriter({
57+
logId: 'logid',
58+
logFilePath: null,
5459
target,
55-
() => now,
56-
{
57-
isDisabled: true,
58-
}
59-
);
60+
now: () => now,
61+
});
6062

6163
expect(disabledWriter.isDisabled).to.equal(true);
6264
logAllSeverities(disabledWriter);
@@ -107,7 +109,12 @@ describe('MongoLogWriter', function () {
107109

108110
it('allows writing log messages to a stream', async function () {
109111
const target = new stream.PassThrough().setEncoding('utf8');
110-
const writer = new MongoLogWriter('logid', null, target, () => now);
112+
const writer = new MongoLogWriter({
113+
logId: 'logid',
114+
logFilePath: null,
115+
target,
116+
now: () => now,
117+
});
111118
const logEvents: MongoLogEntry[] = [];
112119
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
113120
writer.on('log', (entry) => logEvents.push(entry));
@@ -207,7 +214,12 @@ describe('MongoLogWriter', function () {
207214
it('can log error object as data as-is', async function () {
208215
const now = new Date(1628591965386);
209216
const target = new stream.PassThrough().setEncoding('utf8');
210-
const writer = new MongoLogWriter('logid', null, target, () => now);
217+
const writer = new MongoLogWriter({
218+
logId: 'logid',
219+
logFilePath: null,
220+
target,
221+
now: () => now,
222+
});
211223
writer.error(
212224
'component',
213225
mongoLogId(12345),
@@ -233,7 +245,12 @@ describe('MongoLogWriter', function () {
233245
it('can log non-trivial data', async function () {
234246
const now = new Date(1628591965386);
235247
const target = new stream.PassThrough().setEncoding('utf8');
236-
const writer = new MongoLogWriter('logid', null, target, () => now);
248+
const writer = new MongoLogWriter({
249+
logId: 'logid',
250+
logFilePath: null,
251+
target,
252+
now: () => now,
253+
});
237254

238255
const cyclic: any = {};
239256
cyclic.cyclic = cyclic;
@@ -256,7 +273,11 @@ describe('MongoLogWriter', function () {
256273
const errors: Error[] = [];
257274
function tryWrite(input: any) {
258275
const target = new stream.PassThrough().setEncoding('utf8');
259-
const writer = new MongoLogWriter('logid', null, target);
276+
const writer = new MongoLogWriter({
277+
logId: 'logid',
278+
logFilePath: null,
279+
target,
280+
});
260281
writer.on('error', (err) => errors.push(err));
261282
writer.write(input);
262283
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ function validateLogEntry(info: MongoLogEntry): Error | null {
6565

6666
export type MongoLogWriterOptions = {
6767
isDisabled?: boolean;
68+
logId: string;
69+
logFilePath: string | null;
70+
target: PlainWritable;
71+
now?: () => Date;
6872
};
6973

7074
/**
@@ -88,13 +92,13 @@ export class MongoLogWriter extends Writable {
8892
* @param target The Writable stream to write data to.
8993
* @param now An optional function that overrides computation of the current time. This is used for testing.
9094
*/
91-
constructor(
92-
logId: string,
93-
logFilePath: string | null,
94-
target: PlainWritable,
95-
now?: () => Date,
96-
{ isDisabled = false }: MongoLogWriterOptions = {}
97-
) {
95+
constructor({
96+
logId,
97+
logFilePath,
98+
target,
99+
now,
100+
isDisabled = false,
101+
}: MongoLogWriterOptions) {
98102
super({ objectMode: true });
99103
this._logId = logId;
100104
this._logFilePath = logFilePath;

0 commit comments

Comments
 (0)