Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger: ignore empty lines in JSON Mode #906

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-parents-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/logger': patch
---

In JSON mode, don't log empty messages
5 changes: 5 additions & 0 deletions .changeset/old-swans-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/ws-worker': patch
---

Ignore empty log lines (don't send them to lightning)
25 changes: 25 additions & 0 deletions integration-tests/worker/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,31 @@ test.serial("Don't send job logs to stdout", (t) => {
});
});

// This is a test against job logs - but it should work
// exactly the same way for all logs
test.serial("Don't send empty logs to lightning", (t) => {
return new Promise(async (done) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: 'fn((s) => { console.log(); return s })',
},
],
};

lightning.once('run:complete', () => {
// The engine logger shouldn't print out any job logs
const jobLogs = engineLogger._history.filter((l) => l.name === 'JOB');
t.is(jobLogs.length, 0);
done();
});

lightning.enqueueRun(attempt);
});
});

test.serial('Include timestamps on basically everything', (t) => {
return new Promise(async (done) => {
const attempt = {
Expand Down
8 changes: 8 additions & 0 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export default function (name?: string, options: LogOptions = {}): Logger {
level: LogFns,
...args: LogArgs
) => {
if (args.length === 0) {
// In JSON mode, don't log empty lines
// (mostly because this spams Lightning with nonsense, but more generally
// if you have machine readable logs then "whitespace" or "formatting" logs,
// like console.break(), are meaningless)
return;
}

const message = args.map((o) =>
sanitize(o, {
stringify: false,
Expand Down
11 changes: 11 additions & 0 deletions packages/logger/test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,17 @@ test('json mode should serialize errors nicely', (t) => {
t.deepEqual(result.message[0], { name: 'Error', message: 'wibble' });
});

test('json mode should not log empty lines', (t) => {
const logger = createLogger<JSONLog>(undefined, {
level: 'info',
json: true,
});

logger.info();

t.is(logger._history.length, 0);
});

test('with level=default, logs success, error and warning but not info and debug', (t) => {
const logger = createLogger<StringLog>('x', { level: 'default' });

Expand Down