Skip to content

Commit c6384d8

Browse files
committed
logger: skip input validation when no subscribers
1 parent 003402a commit c6384d8

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

lib/logger.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ class Logger {
472472
return;
473473
}
474474

475+
const channel = channels[level];
476+
if (!channel.hasSubscribers) {
477+
return;
478+
}
479+
475480
if (typeof msgOrObj === 'string') {
476481
if (fields !== undefined) {
477482
validateObject(fields, 'fields');
@@ -481,11 +486,6 @@ class Logger {
481486
validateString(msgOrObj.msg, 'obj.msg');
482487
}
483488

484-
const channel = channels[level];
485-
if (!channel.hasSubscribers) {
486-
return;
487-
}
488-
489489
let msg;
490490
let logFields;
491491

test/parallel/test-log-basic.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,35 @@ describe('Logger', () => {
7171

7272
describe('msg field validation', () => {
7373
it('should throw when object is missing msg field', () => {
74+
const stream = new TestStream();
75+
const consumer = new JSONConsumer({ stream, level: 'info' });
76+
consumer.attach();
7477
const logger = new Logger();
75-
assert.throws(() => {
76-
logger.info({ userId: 123 }); // Missing msg
77-
}, {
78-
code: 'ERR_INVALID_ARG_TYPE',
79-
});
78+
try {
79+
assert.throws(() => {
80+
logger.info({ userId: 123 }); // Missing msg
81+
}, {
82+
code: 'ERR_INVALID_ARG_TYPE',
83+
});
84+
} finally {
85+
consumer.detach();
86+
}
8087
});
8188

8289
it('should throw when msg is not a string', () => {
90+
const stream = new TestStream();
91+
const consumer = new JSONConsumer({ stream, level: 'info' });
92+
consumer.attach();
8393
const logger = new Logger();
84-
assert.throws(() => {
85-
logger.info({ msg: 123 }); // msg is not a string
86-
}, {
87-
code: 'ERR_INVALID_ARG_TYPE',
88-
});
94+
try {
95+
assert.throws(() => {
96+
logger.info({ msg: 123 }); // msg is not a string
97+
}, {
98+
code: 'ERR_INVALID_ARG_TYPE',
99+
});
100+
} finally {
101+
consumer.detach();
102+
}
89103
});
90104

91105
it('should accept string message without second argument', () => {
@@ -107,12 +121,19 @@ describe('Logger', () => {
107121

108122
describe('invalid fields argument', () => {
109123
it('should throw when fields is not an object', () => {
124+
const stream = new TestStream();
125+
const consumer = new JSONConsumer({ stream, level: 'info' });
126+
consumer.attach();
110127
const logger = new Logger();
111-
assert.throws(() => {
112-
logger.info('message', 'not an object');
113-
}, {
114-
code: 'ERR_INVALID_ARG_TYPE',
115-
});
128+
try {
129+
assert.throws(() => {
130+
logger.info('message', 'not an object');
131+
}, {
132+
code: 'ERR_INVALID_ARG_TYPE',
133+
});
134+
} finally {
135+
consumer.detach();
136+
}
116137
});
117138
});
118139
});

0 commit comments

Comments
 (0)