Skip to content

Commit

Permalink
Add timeStart function (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Apr 23, 2024
1 parent 7002737 commit 3cfae6d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion src/Logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect } from 'chai';
import type { LogLevel, LogMessage } from './Logger';
import { Logger, LogLevelColor, LogLevelNumeric } from './Logger';
import { Logger, LogLevelColor, LogLevelNumeric, chalk } from './Logger';
import { ConsoleTransport } from './transports/ConsoleTransport';
import { createSandbox } from 'sinon';
import { Stopwatch } from './Stopwatch';
const sinon = createSandbox();

describe('Logger', () => {
Expand All @@ -17,6 +18,8 @@ describe('Logger', () => {
sinon.restore();
sinon.stub(Logger.prototype as any, 'getCurrentDate').returns(now);
logger = new Logger();
//disable chalk colors for this test
chalk.level = 0;
});

afterEach(() => {
Expand Down Expand Up @@ -509,6 +512,32 @@ describe('Logger', () => {
});
});

describe('timeStart', () => {
it('skips logging when logLevel is disabled', async () => {
const stub = sinon.stub(logger, 'write').callThrough();
logger.logLevel = 'log';
const stop = logger.timeStart('info', 'message');
await sleep(10);
stop();
expect(stub.called).to.be.false;
});

it('logs when logLevel is enabled', async () => {
sinon.stub(Stopwatch.prototype, 'getDurationText').callsFake(() => '10ms');
const stub = sinon.stub(logger, 'write').callThrough();
logger.logLevel = 'info';
const stop = logger.timeStart('info', 'message');
await sleep(10);
stop();
expect(
stub.getCalls().map(x => x.args)
).to.eql([
['info', 'message'],
['info', 'message', `finished. (10ms)`]
]);
});
});

describe('time', () => {
it('calls action even if logLevel is wrong', () => {
logger.logLevel = 'error';
Expand Down Expand Up @@ -555,3 +584,9 @@ describe('Logger', () => {
});
});
});

async function sleep(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
21 changes: 21 additions & 0 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@ export class Logger {
this.write('error', ...messages);
}

/**
* Writes to the log (if logLevel matches), and also provides a function that can be called to mark the end of a time.
*/
public timeStart(logLevel: LogLevel, ...messages: unknown[]) {
//call the log if loglevel is in range
if (this.isLogLevelEnabled(logLevel)) {
const stopwatch = new Stopwatch();

//write the initial log
this.write(logLevel, ...messages);

stopwatch.start();

return (status = 'finished') => {
stopwatch.stop();
this.write(logLevel, ...messages, `${status}. (${chalk.blue(stopwatch.getDurationText())})`);
};
}
return noop;
}

/**
* Writes to the log (if logLevel matches), and also times how long the action took to occur.
* `action` is called regardless of logLevel, so this function can be used to nicely wrap
Expand Down
28 changes: 20 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"target": "es2018",
"declaration": true,
"noImplicitAny": false,
"target": "ES2017",
"module": "CommonJS",
"sourceMap": true,
"rootDir": "src",
"outDir": "dist",
"preserveConstEnums": true
"declaration": true,
"strict": true,
"strictNullChecks": false,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"preserveConstEnums": true,
"downlevelIteration": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"lib": [
"es2017",
"es2019.array",
"dom"
]
},
"include": [
"src/**/*.ts"
Expand All @@ -20,4 +32,4 @@
"ts-node": {
"transpileOnly": true
}
}
}

0 comments on commit 3cfae6d

Please sign in to comment.