Skip to content

Commit 0a176aa

Browse files
authored
Logger: ignore empty lines in JSON Mode (#906)
* logger: don't log empty log lines * integration test * another changeset
1 parent ce5022a commit 0a176aa

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

.changeset/eight-parents-add.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/logger': patch
3+
---
4+
5+
In JSON mode, don't log empty messages

.changeset/old-swans-clean.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/ws-worker': patch
3+
---
4+
5+
Ignore empty log lines (don't send them to lightning)

integration-tests/worker/test/integration.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,31 @@ test.serial("Don't send job logs to stdout", (t) => {
561561
});
562562
});
563563

564+
// This is a test against job logs - but it should work
565+
// exactly the same way for all logs
566+
test.serial("Don't send empty logs to lightning", (t) => {
567+
return new Promise(async (done) => {
568+
const attempt = {
569+
id: crypto.randomUUID(),
570+
jobs: [
571+
{
572+
adaptor: '@openfn/language-common@latest',
573+
body: 'fn((s) => { console.log(); return s })',
574+
},
575+
],
576+
};
577+
578+
lightning.once('run:complete', () => {
579+
// The engine logger shouldn't print out any job logs
580+
const jobLogs = engineLogger._history.filter((l) => l.name === 'JOB');
581+
t.is(jobLogs.length, 0);
582+
done();
583+
});
584+
585+
lightning.enqueueRun(attempt);
586+
});
587+
});
588+
564589
test.serial('Include timestamps on basically everything', (t) => {
565590
return new Promise(async (done) => {
566591
const attempt = {

packages/logger/src/logger.ts

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ export default function (name?: string, options: LogOptions = {}): Logger {
158158
level: LogFns,
159159
...args: LogArgs
160160
) => {
161+
if (args.length === 0) {
162+
// In JSON mode, don't log empty lines
163+
// (mostly because this spams Lightning with nonsense, but more generally
164+
// if you have machine readable logs then "whitespace" or "formatting" logs,
165+
// like console.break(), are meaningless)
166+
return;
167+
}
168+
161169
const message = args.map((o) =>
162170
sanitize(o, {
163171
stringify: false,

packages/logger/test/logger.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ test('json mode should serialize errors nicely', (t) => {
354354
t.deepEqual(result.message[0], { name: 'Error', message: 'wibble' });
355355
});
356356

357+
test('json mode should not log empty lines', (t) => {
358+
const logger = createLogger<JSONLog>(undefined, {
359+
level: 'info',
360+
json: true,
361+
});
362+
363+
logger.info();
364+
365+
t.is(logger._history.length, 0);
366+
});
367+
357368
test('with level=default, logs success, error and warning but not info and debug', (t) => {
358369
const logger = createLogger<StringLog>('x', { level: 'default' });
359370

0 commit comments

Comments
 (0)