Skip to content

Commit

Permalink
Rename LogContext.addContext() to LogContext.withContext()
Browse files Browse the repository at this point in the history
  • Loading branch information
Darick committed May 18, 2023
1 parent 9c90f20 commit 9a2f520
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ lc.info('hello'); // prints "hello"
const lc2 = new LogContext('info', 'name');
lc.info('hello'); // prints "name hello"

const lc3 = lc2.addContext('bbb');
const lc3 = lc2.withContext('bbb');
lc3.info('hello'); // prints "name bbb hello"

const lc4 = lc3.addContext('ccc');
const lc4 = lc3.withContext('ccc');
lc4.info('hello'); // prints "name bbb ccc hello"

const lc5 = lc4.addContext('ddd', 'eee');
const lc5 = lc4.withContext('ddd', 'eee');
lc5.info('hello'); // prints "name bbb ccc ddd=eee hello"

// Or get a context logger appropriate for the Node environment.
Expand Down
16 changes: 8 additions & 8 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ test('LogContext formatting', () => {
lc.debug?.('aaa');
expect(mockDebug.lastCall.args).to.deep.equal(['aaa']);

const lc2 = new LogContext('debug').addContext('bbb');
const lc2 = new LogContext('debug').withContext('bbb');
lc2.debug?.('ccc');
expect(mockDebug.lastCall.args).to.deep.equal(['bbb', 'ccc']);

const lc3 = lc2.addContext('ddd');
const lc3 = lc2.withContext('ddd');
lc3.debug?.('eee');
expect(mockDebug.lastCall.args).to.deep.equal(['bbb', 'ddd', 'eee']);

const lc4 = lc2.addContext('fff', 'ggg');
const lc4 = lc2.withContext('fff', 'ggg');
lc4.debug?.('hhh');
expect(mockDebug.lastCall.args).to.deep.equal(['bbb', 'fff=ggg', 'hhh']);
});
Expand All @@ -165,11 +165,11 @@ test('Optional tag', () => {
lc.debug?.('a');
expect(mockDebug.lastCall.args).to.deep.equal(['a']);

const lc2 = lc.addContext('b');
const lc2 = lc.withContext('b');
lc2.debug?.('c');
expect(mockDebug.lastCall.args).to.deep.equal(['b', 'c']);

const lc3 = lc.addContext('d', 'e');
const lc3 = lc.withContext('d', 'e');
lc3.debug?.('f');
expect(mockDebug.lastCall.args).to.deep.equal(['d=e', 'f']);
});
Expand Down Expand Up @@ -237,9 +237,9 @@ test('Context-aware LogSink', () => {
const lc = new LogContext('debug', sink);

lc.info?.(1, 2);
lc.addContext('foo', {bar: 'baz'}).debug?.(3, 4);
lc.addContext('boo', 'oof').info?.(5, 6);
lc.addContext('abc', 'is').addContext('easy', 'as').info?.(1, 2, 3);
lc.withContext('foo', {bar: 'baz'}).debug?.(3, 4);
lc.withContext('boo', 'oof').info?.(5, 6);
lc.withContext('abc', 'is').withContext('easy', 'as').info?.(1, 2, 3);
lc.debug?.(7, 8);

expect(sink.messages).to.deep.equal([
Expand Down
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class SilentLogger implements OptionalLogger {}
*
* const lc = new LogContext('debug');
* lc.debug?.('hello');
* const lc2 = lc.addContext('foo');
* const lc2 = lc.withContext('foo');
* f(lc2); // logging inside f will contain 'foo' in the Context.
*/
export class LogContext extends OptionalLoggerImpl {
Expand All @@ -174,7 +174,7 @@ export class LogContext extends OptionalLoggerImpl {
* Creates a new Logger that with the given key and value
* added to the logged Context.
*/
addContext(key: string, value?: unknown): LogContext {
withContext(key: string, value?: unknown): LogContext {
const ctx = {...this._context, [key]: value};
return new LogContext(this._level, this._logSink, ctx);
}
Expand Down

0 comments on commit 9a2f520

Please sign in to comment.