1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00

Use logService for console messages (#214)

* Use logService for console messages

* linter autofixes

* Use full import path

* Implement a base ConsoleLog service

Use this class as a default for other services that would like to output
to console. This service is overriden in CLI and Desktop to use CLI's
consoleLogService and electronLogService, respectively.

* linter fixes

Co-authored-by: Matt Gibson <mdgibson@Matts-MBP.lan>
This commit is contained in:
Matt Gibson
2020-12-04 12:38:26 -06:00
committed by GitHub
parent 6fb0646481
commit 0fed528b6f
10 changed files with 141 additions and 61 deletions

View File

@@ -0,0 +1,68 @@
import { LogLevelType } from '../enums/logLevelType';
import { LogService as LogServiceAbstraction } from '../abstractions/log.service';
export class ConsoleLogService implements LogServiceAbstraction {
private timersMap: Map<string, bigint> = new Map();
constructor(protected isDev: boolean, protected filter: (level: LogLevelType) => boolean = null) { }
debug(message: string) {
if (!this.isDev) {
return;
}
this.write(LogLevelType.Debug, message);
}
info(message: string) {
this.write(LogLevelType.Info, message);
}
warning(message: string) {
this.write(LogLevelType.Warning, message);
}
error(message: string) {
this.write(LogLevelType.Error, message);
}
write(level: LogLevelType, message: string) {
if (this.filter != null && this.filter(level)) {
return;
}
switch (level) {
case LogLevelType.Debug:
// tslint:disable-next-line
console.log(message);
break;
case LogLevelType.Info:
// tslint:disable-next-line
console.log(message);
break;
case LogLevelType.Warning:
// tslint:disable-next-line
console.warn(message);
break;
case LogLevelType.Error:
// tslint:disable-next-line
console.error(message);
break;
default:
break;
}
}
time(label: string = 'default') {
if (!this.timersMap.has(label)) {
this.timersMap.set(label, process.hrtime.bigint());
}
}
timeEnd(label: string = 'default'): bigint {
const elapsed = (process.hrtime.bigint() - this.timersMap.get(label)) / BigInt(1000000);
this.timersMap.delete(label);
this.write(LogLevelType.Info, `${label}: ${elapsed}ms`);
return elapsed;
}
}