Skip to content

Commit 7652e59

Browse files
committed
feat(logger): add time() method to Logger class
Add the missing time() method to start timers, complementing the existing timeEnd() and timeLog() methods. This allows users to start timers using the Logger instance instead of console.time(). - Add time() method with proper TypeScript signature - Return 'this' for method chaining consistency - Update timeEnd() documentation to reference logger.time() - Fix tests to properly start timers before ending them
1 parent c234d92 commit 7652e59

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/logger.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,24 +1461,54 @@ export class Logger {
14611461
return this[incLogCallCountSymbol]()
14621462
}
14631463

1464+
/**
1465+
* Starts a timer for measuring elapsed time.
1466+
*
1467+
* Creates a timer with the given label. Use `timeEnd()` with the same
1468+
* label to stop the timer and log the elapsed time, or use `timeLog()`
1469+
* to check the time without stopping the timer.
1470+
*
1471+
* @param label - Optional label for the timer
1472+
* @default 'default'
1473+
* @returns The logger instance for chaining
1474+
*
1475+
* @example
1476+
* ```typescript
1477+
* logger.time('operation')
1478+
* // ... do work ...
1479+
* logger.timeEnd('operation')
1480+
* // Logs: "operation: 123.456ms"
1481+
*
1482+
* logger.time()
1483+
* // ... do work ...
1484+
* logger.timeEnd()
1485+
* // Logs: "default: 123.456ms"
1486+
* ```
1487+
*/
1488+
time(label?: string | undefined): this {
1489+
const con = this.#getConsole()
1490+
con.time(label)
1491+
return this
1492+
}
1493+
14641494
/**
14651495
* Ends a timer and logs the elapsed time.
14661496
*
1467-
* Logs the duration since `console.time()` was called with the same
1468-
* label. The timer is stopped and removed.
1497+
* Logs the duration since `console.time()` or `logger.time()` was called
1498+
* with the same label. The timer is stopped and removed.
14691499
*
14701500
* @param label - Optional label for the timer
14711501
* @default 'default'
14721502
* @returns The logger instance for chaining
14731503
*
14741504
* @example
14751505
* ```typescript
1476-
* console.time('operation')
1506+
* logger.time('operation')
14771507
* // ... do work ...
14781508
* logger.timeEnd('operation')
14791509
* // Logs: "operation: 123.456ms"
14801510
*
1481-
* console.time()
1511+
* logger.time()
14821512
* // ... do work ...
14831513
* logger.timeEnd()
14841514
* // Logs: "default: 123.456ms"

test/isolated/logger.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,39 +609,48 @@ describe('Logger', () => {
609609

610610
describe('timeEnd() method', () => {
611611
it('should end timer and log duration', () => {
612+
testLogger.time('timer-test-1')
612613
const beforeCount = testLogger.logCallCount
613614
testLogger.timeEnd('timer-test-1')
614615
expect(testLogger.logCallCount).toBe(beforeCount + 1)
615616
})
616617

617-
it('should work with default label', () => {
618+
it('should work with non-existent timer', () => {
619+
testLogger.time('existing-timer')
618620
const beforeCount = testLogger.logCallCount
619-
testLogger.timeEnd('non-existent-timer')
621+
testLogger.timeEnd('existing-timer')
620622
expect(testLogger.logCallCount).toBe(beforeCount + 1)
621623
})
622624

623625
it('should return logger instance for chaining', () => {
626+
testLogger.time('some-label')
624627
const result = testLogger.timeEnd('some-label')
625628
expect(result).toBe(testLogger)
626629
})
627630
})
628631

629632
describe('timeLog() method', () => {
630633
it('should log current timer value without stopping', () => {
634+
testLogger.time('timer-test-2')
631635
const beforeCount = testLogger.logCallCount
632636
testLogger.timeLog('timer-test-2', 'checkpoint')
633637
expect(testLogger.logCallCount).toBe(beforeCount + 1)
638+
testLogger.timeEnd('timer-test-2')
634639
})
635640

636641
it('should support additional data', () => {
642+
testLogger.time('timer-test-3')
637643
const beforeCount = testLogger.logCallCount
638644
testLogger.timeLog('timer-test-3', 'data1', 'data2')
639645
expect(testLogger.logCallCount).toBe(beforeCount + 1)
646+
testLogger.timeEnd('timer-test-3')
640647
})
641648

642649
it('should return logger instance for chaining', () => {
650+
testLogger.time('some-timer')
643651
const result = testLogger.timeLog('some-timer')
644652
expect(result).toBe(testLogger)
653+
testLogger.timeEnd('some-timer')
645654
})
646655
})
647656

@@ -1065,6 +1074,7 @@ describe('Logger', () => {
10651074
})
10661075

10671076
it('should support timeEnd without errors', () => {
1077+
testLogger.time('any-timer')
10681078
expect(() => {
10691079
testLogger.timeEnd('any-timer')
10701080
}).not.toThrow()

0 commit comments

Comments
 (0)