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

Change the LogSink interface to take an optional Context object #18

Merged
merged 6 commits into from
May 19, 2023

Conversation

darkgnotic
Copy link
Contributor

@darkgnotic darkgnotic commented May 18, 2023

Logger-side changes to support https://github.com/rocicorp/mono/issues/191

Also rename LogContext.addContext() to LogContext.withContext() to better convey the immutability of the API.

Note: breaking API change

@darkgnotic darkgnotic requested a review from aboodman May 18, 2023 22:35
@darkgnotic darkgnotic requested a review from arv May 19, 2023 06:40
@darkgnotic
Copy link
Contributor Author

FYI, I will wait for review on this. (No rush, I know that there are a lot of things happening)

src/logger.ts Outdated
Comment on lines 113 to 116
console[level](
...this._format(level, ...stringified(context).concat(args)),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console[level](
...this._format(level, ...stringified(context).concat(args)),
);
}
console[level](
...this._format(
level,
...stringified(context),
...args),
);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thank you!

src/logger.ts Outdated
log(level: LogLevel, context: Context | undefined, ...args: unknown[]): void {
console[level](
logLevelPrefix[level],
...stringified(context).concat(args).map(normalizeArgument),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think you need to call normalizeArgument on the stringified context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, then I can unroll this one too.

src/logger.ts Outdated

constructor(level: LogLevel = 'info', logSink: LogSink = consoleLogSink) {
super(logSink, level);
private readonly _context?: Context;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no optional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

lc3.debug?.('f');
expect(mockDebug.lastCall.args).to.deep.equal(['d=e', 'f']);
});

class TestLogSink implements LogSink {
messages: [LogLevel, ...unknown[]][] = [];
// Note: Order in messages is different from log args order to verify
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also have used:

  messages: [LogLevel, Context | undefined, unknown[]][] = [];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it. Now my verification code reads as it should:

    ['info', {abc: 'is', easy: 'as'}, [1, 2, 3]],

Thanks!

[undefined, 'info', 1, 2],
[{foo: {bar: 'baz'}}, 'debug', 3, 4],
[{boo: 'oof'}, 'info', 5, 6],
[{abc: 'is', easy: 'as'}, 'info', 1, 2, 3],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁

@arv
Copy link
Contributor

arv commented May 19, 2023

LGTM

Copy link
Contributor Author

@darkgnotic darkgnotic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @arv!

lc3.debug?.('f');
expect(mockDebug.lastCall.args).to.deep.equal(['d=e', 'f']);
});

class TestLogSink implements LogSink {
messages: [LogLevel, ...unknown[]][] = [];
// Note: Order in messages is different from log args order to verify
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it. Now my verification code reads as it should:

    ['info', {abc: 'is', easy: 'as'}, [1, 2, 3]],

Thanks!

[undefined, 'info', 1, 2],
[{foo: {bar: 'baz'}}, 'debug', 3, 4],
[{boo: 'oof'}, 'info', 5, 6],
[{abc: 'is', easy: 'as'}, 'info', 1, 2, 3],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁

src/logger.ts Outdated
Comment on lines 113 to 116
console[level](
...this._format(level, ...stringified(context).concat(args)),
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thank you!

src/logger.ts Outdated
log(level: LogLevel, context: Context | undefined, ...args: unknown[]): void {
console[level](
logLevelPrefix[level],
...stringified(context).concat(args).map(normalizeArgument),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, then I can unroll this one too.

src/logger.ts Outdated

constructor(level: LogLevel = 'info', logSink: LogSink = consoleLogSink) {
super(logSink, level);
private readonly _context?: Context;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@darkgnotic darkgnotic removed the request for review from aboodman May 19, 2023 18:39
@darkgnotic darkgnotic merged commit 9f3df39 into main May 19, 2023
2 checks passed
@darkgnotic darkgnotic deleted the d-llama/logsink-context branch May 19, 2023 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants