1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 01:03:35 +00:00
Files
browser/src/services/consoleLog.service.ts
Matt Gibson 0fed528b6f 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>
2020-12-04 12:38:26 -06:00

69 lines
2.0 KiB
TypeScript

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;
}
}