diff --git a/README.md b/README.md index d7a97cc..5730c38 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/logger.test.ts b/src/logger.test.ts index 7e8e559..8041509 100644 --- a/src/logger.test.ts +++ b/src/logger.test.ts @@ -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']); }); @@ -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']); }); @@ -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([ diff --git a/src/logger.ts b/src/logger.ts index f07475c..1c0a006 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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 { @@ -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); }