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:
parent
6fb0646481
commit
0fed528b6f
|
@ -6,4 +6,6 @@ export abstract class LogService {
|
||||||
warning: (message: string) => void;
|
warning: (message: string) => void;
|
||||||
error: (message: string) => void;
|
error: (message: string) => void;
|
||||||
write: (level: LogLevelType, message: string) => void;
|
write: (level: LogLevelType, message: string) => void;
|
||||||
|
time: (label: string) => void;
|
||||||
|
timeEnd: (label: string) => bigint;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ export abstract class BaseProgram {
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
if (process.env.BW_QUIET !== 'true') {
|
if (process.env.BW_QUIET !== 'true') {
|
||||||
if (process.env.BW_RESPONSE === 'true') {
|
if (process.env.BW_RESPONSE === 'true') {
|
||||||
this.writeLn(this.getJson(response), true, true);
|
this.writeLn(this.getJson(response), true, false);
|
||||||
} else {
|
} else {
|
||||||
this.writeLn(chalk.redBright(response.message), true, true);
|
this.writeLn(chalk.redBright(response.message), true, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,10 @@
|
||||||
import { LogLevelType } from '../../enums/logLevelType';
|
import { LogLevelType } from '../../enums/logLevelType';
|
||||||
|
|
||||||
import { LogService as LogServiceAbstraction } from '../../abstractions/log.service';
|
import { ConsoleLogService as BaseConsoleLogService } from '../../services/consoleLog.service';
|
||||||
|
|
||||||
export class ConsoleLogService implements LogServiceAbstraction {
|
export class ConsoleLogService extends BaseConsoleLogService {
|
||||||
constructor(private isDev: boolean, private filter: (level: LogLevelType) => boolean = null) { }
|
constructor(isDev: boolean, filter: (level: LogLevelType) => boolean = null) {
|
||||||
|
super(isDev, filter);
|
||||||
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) {
|
write(level: LogLevelType, message: string) {
|
||||||
|
@ -29,25 +12,12 @@ export class ConsoleLogService implements LogServiceAbstraction {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (level) {
|
if (process.env.BW_RESPONSE) {
|
||||||
case LogLevelType.Debug:
|
// tslint:disable-next-line
|
||||||
// tslint:disable-next-line
|
console.error(message);
|
||||||
console.log(message);
|
return;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.write(level, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ import { LogLevelType } from '../../enums/logLevelType';
|
||||||
import { LogService as LogServiceAbstraction } from '../../abstractions/log.service';
|
import { LogService as LogServiceAbstraction } from '../../abstractions/log.service';
|
||||||
|
|
||||||
export class ElectronLogService implements LogServiceAbstraction {
|
export class ElectronLogService implements LogServiceAbstraction {
|
||||||
|
private timersMap: Map<string, bigint> = new Map();
|
||||||
|
|
||||||
constructor(private filter: (level: LogLevelType) => boolean = null, logDir: string = null) {
|
constructor(private filter: (level: LogLevelType) => boolean = null, logDir: string = null) {
|
||||||
if (log.transports == null) {
|
if (log.transports == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -61,4 +63,17 @@ export class ElectronLogService implements LogServiceAbstraction {
|
||||||
break;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,17 @@ import { FolderView } from '../models/view/folderView';
|
||||||
import { LoginView } from '../models/view/loginView';
|
import { LoginView } from '../models/view/loginView';
|
||||||
import { SecureNoteView } from '../models/view/secureNoteView';
|
import { SecureNoteView } from '../models/view/secureNoteView';
|
||||||
|
|
||||||
|
import { LogService } from '../abstractions/log.service';
|
||||||
import { CipherType } from '../enums/cipherType';
|
import { CipherType } from '../enums/cipherType';
|
||||||
import { FieldType } from '../enums/fieldType';
|
import { FieldType } from '../enums/fieldType';
|
||||||
import { SecureNoteType } from '../enums/secureNoteType';
|
import { SecureNoteType } from '../enums/secureNoteType';
|
||||||
|
import { ConsoleLogService } from '../services/consoleLog.service';
|
||||||
|
|
||||||
export abstract class BaseImporter {
|
export abstract class BaseImporter {
|
||||||
organization = false;
|
organization = false;
|
||||||
|
|
||||||
|
protected logService: LogService = new ConsoleLogService(false);
|
||||||
|
|
||||||
protected newLineRegex = /(?:\r\n|\r|\n)/;
|
protected newLineRegex = /(?:\r\n|\r|\n)/;
|
||||||
|
|
||||||
protected passwordFieldNames = [
|
protected passwordFieldNames = [
|
||||||
|
@ -84,7 +88,7 @@ export abstract class BaseImporter {
|
||||||
result.errors.forEach((e) => {
|
result.errors.forEach((e) => {
|
||||||
if (e.row != null) {
|
if (e.row != null) {
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
console.warn('Error parsing row ' + e.row + ': ' + e.message);
|
this.logService.warning('Error parsing row ' + e.row + ': ' + e.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,13 @@ import { AppIdService } from '../abstractions/appId.service';
|
||||||
import { AuthService as AuthServiceAbstraction } from '../abstractions/auth.service';
|
import { AuthService as AuthServiceAbstraction } from '../abstractions/auth.service';
|
||||||
import { CryptoService } from '../abstractions/crypto.service';
|
import { CryptoService } from '../abstractions/crypto.service';
|
||||||
import { I18nService } from '../abstractions/i18n.service';
|
import { I18nService } from '../abstractions/i18n.service';
|
||||||
|
import { LogService } from '../abstractions/log.service';
|
||||||
import { MessagingService } from '../abstractions/messaging.service';
|
import { MessagingService } from '../abstractions/messaging.service';
|
||||||
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
||||||
import { TokenService } from '../abstractions/token.service';
|
import { TokenService } from '../abstractions/token.service';
|
||||||
import { UserService } from '../abstractions/user.service';
|
import { UserService } from '../abstractions/user.service';
|
||||||
import { VaultTimeoutService } from '../abstractions/vaultTimeout.service';
|
import { VaultTimeoutService } from '../abstractions/vaultTimeout.service';
|
||||||
|
import { ConsoleLogService } from '../services/consoleLog.service';
|
||||||
|
|
||||||
export const TwoFactorProviders = {
|
export const TwoFactorProviders = {
|
||||||
[TwoFactorProviderType.Authenticator]: {
|
[TwoFactorProviderType.Authenticator]: {
|
||||||
|
@ -91,7 +93,12 @@ export class AuthService implements AuthServiceAbstraction {
|
||||||
private userService: UserService, private tokenService: TokenService,
|
private userService: UserService, private tokenService: TokenService,
|
||||||
private appIdService: AppIdService, private i18nService: I18nService,
|
private appIdService: AppIdService, private i18nService: I18nService,
|
||||||
private platformUtilsService: PlatformUtilsService, private messagingService: MessagingService,
|
private platformUtilsService: PlatformUtilsService, private messagingService: MessagingService,
|
||||||
private vaultTimeoutService: VaultTimeoutService, private setCryptoKeys = true) { }
|
private vaultTimeoutService: VaultTimeoutService, private logService?: LogService,
|
||||||
|
private setCryptoKeys = true) {
|
||||||
|
if (!logService) {
|
||||||
|
this.logService = new ConsoleLogService(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
TwoFactorProviders[TwoFactorProviderType.Email].name = this.i18nService.t('emailTitle');
|
TwoFactorProviders[TwoFactorProviderType.Email].name = this.i18nService.t('emailTitle');
|
||||||
|
@ -351,7 +358,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||||
tokenResponse.privateKey = keyPair[1].encryptedString;
|
tokenResponse.privateKey = keyPair[1].encryptedString;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
console.error(e);
|
this.logService.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,8 +10,10 @@ import { ProfileOrganizationResponse } from '../models/response/profileOrganizat
|
||||||
|
|
||||||
import { CryptoService as CryptoServiceAbstraction } from '../abstractions/crypto.service';
|
import { CryptoService as CryptoServiceAbstraction } from '../abstractions/crypto.service';
|
||||||
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
||||||
|
import { LogService } from '../abstractions/log.service';
|
||||||
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
||||||
import { StorageService } from '../abstractions/storage.service';
|
import { StorageService } from '../abstractions/storage.service';
|
||||||
|
import { ConsoleLogService } from '../services/consoleLog.service';
|
||||||
|
|
||||||
import { ConstantsService } from './constants.service';
|
import { ConstantsService } from './constants.service';
|
||||||
|
|
||||||
|
@ -37,7 +39,12 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
private orgKeys: Map<string, SymmetricCryptoKey>;
|
private orgKeys: Map<string, SymmetricCryptoKey>;
|
||||||
|
|
||||||
constructor(private storageService: StorageService, private secureStorageService: StorageService,
|
constructor(private storageService: StorageService, private secureStorageService: StorageService,
|
||||||
private cryptoFunctionService: CryptoFunctionService, private platformUtilService: PlatformUtilsService) { }
|
private cryptoFunctionService: CryptoFunctionService, private platformUtilService: PlatformUtilsService,
|
||||||
|
private logService?: LogService) {
|
||||||
|
if (!logService) {
|
||||||
|
this.logService = new ConsoleLogService(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async setKey(key: SymmetricCryptoKey): Promise<any> {
|
async setKey(key: SymmetricCryptoKey): Promise<any> {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
|
@ -546,14 +553,12 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
||||||
|
|
||||||
if (theKey.macKey != null && mac == null) {
|
if (theKey.macKey != null && mac == null) {
|
||||||
// tslint:disable-next-line
|
this.logService.error('mac required.');
|
||||||
console.error('mac required.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (theKey.encType !== encType) {
|
if (theKey.encType !== encType) {
|
||||||
// tslint:disable-next-line
|
this.logService.error('encType unavailable.');
|
||||||
console.error('encType unavailable.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -563,8 +568,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
fastParams.macKey, 'sha256');
|
fastParams.macKey, 'sha256');
|
||||||
const macsEqual = await this.cryptoFunctionService.compareFast(fastParams.mac, computedMac);
|
const macsEqual = await this.cryptoFunctionService.compareFast(fastParams.mac, computedMac);
|
||||||
if (!macsEqual) {
|
if (!macsEqual) {
|
||||||
// tslint:disable-next-line
|
this.logService.error('mac failed.');
|
||||||
console.error('mac failed.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -596,8 +600,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
|
|
||||||
const macsMatch = await this.cryptoFunctionService.compare(mac, computedMac);
|
const macsMatch = await this.cryptoFunctionService.compare(mac, computedMac);
|
||||||
if (!macsMatch) {
|
if (!macsMatch) {
|
||||||
// tslint:disable-next-line
|
this.logService.error('mac failed.');
|
||||||
console.error('mac failed.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,12 @@ import { NotificationType } from '../enums/notificationType';
|
||||||
import { ApiService } from '../abstractions/api.service';
|
import { ApiService } from '../abstractions/api.service';
|
||||||
import { AppIdService } from '../abstractions/appId.service';
|
import { AppIdService } from '../abstractions/appId.service';
|
||||||
import { EnvironmentService } from '../abstractions/environment.service';
|
import { EnvironmentService } from '../abstractions/environment.service';
|
||||||
|
import { LogService } from '../abstractions/log.service'
|
||||||
import { NotificationsService as NotificationsServiceAbstraction } from '../abstractions/notifications.service';
|
import { NotificationsService as NotificationsServiceAbstraction } from '../abstractions/notifications.service';
|
||||||
import { SyncService } from '../abstractions/sync.service';
|
import { SyncService } from '../abstractions/sync.service';
|
||||||
import { UserService } from '../abstractions/user.service';
|
import { UserService } from '../abstractions/user.service';
|
||||||
import { VaultTimeoutService } from '../abstractions/vaultTimeout.service';
|
import { VaultTimeoutService } from '../abstractions/vaultTimeout.service';
|
||||||
|
import { ConsoleLogService } from '../services/consoleLog.service';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
NotificationResponse,
|
NotificationResponse,
|
||||||
|
@ -27,7 +29,12 @@ export class NotificationsService implements NotificationsServiceAbstraction {
|
||||||
|
|
||||||
constructor(private userService: UserService, private syncService: SyncService,
|
constructor(private userService: UserService, private syncService: SyncService,
|
||||||
private appIdService: AppIdService, private apiService: ApiService,
|
private appIdService: AppIdService, private apiService: ApiService,
|
||||||
private vaultTimeoutService: VaultTimeoutService, private logoutCallback: () => Promise<void>) { }
|
private vaultTimeoutService: VaultTimeoutService,
|
||||||
|
private logoutCallback: () => Promise<void>, private logService?: LogService) {
|
||||||
|
if (!logService) {
|
||||||
|
this.logService = new ConsoleLogService(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async init(environmentService: EnvironmentService): Promise<void> {
|
async init(environmentService: EnvironmentService): Promise<void> {
|
||||||
this.inited = false;
|
this.inited = false;
|
||||||
|
@ -87,8 +94,7 @@ export class NotificationsService implements NotificationsServiceAbstraction {
|
||||||
await this.signalrConnection.stop();
|
await this.signalrConnection.stop();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// tslint:disable-next-line
|
this.logService.error(e.toString())
|
||||||
console.error(e.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,15 +5,20 @@ import { CipherView } from '../models/view/cipherView';
|
||||||
import { CipherService } from '../abstractions/cipher.service';
|
import { CipherService } from '../abstractions/cipher.service';
|
||||||
import { SearchService as SearchServiceAbstraction } from '../abstractions/search.service';
|
import { SearchService as SearchServiceAbstraction } from '../abstractions/search.service';
|
||||||
|
|
||||||
|
import { LogService } from '../abstractions/log.service';
|
||||||
import { CipherType } from '../enums/cipherType';
|
import { CipherType } from '../enums/cipherType';
|
||||||
import { FieldType } from '../enums/fieldType';
|
import { FieldType } from '../enums/fieldType';
|
||||||
import { UriMatchType } from '../enums/uriMatchType';
|
import { UriMatchType } from '../enums/uriMatchType';
|
||||||
|
import { ConsoleLogService } from '../services/consoleLog.service';
|
||||||
|
|
||||||
export class SearchService implements SearchServiceAbstraction {
|
export class SearchService implements SearchServiceAbstraction {
|
||||||
private indexing = false;
|
private indexing = false;
|
||||||
private index: lunr.Index = null;
|
private index: lunr.Index = null;
|
||||||
|
|
||||||
constructor(private cipherService: CipherService) {
|
constructor(private cipherService: CipherService, private logService?: LogService) {
|
||||||
|
if (!logService) {
|
||||||
|
this.logService = new ConsoleLogService(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clearIndex(): void {
|
clearIndex(): void {
|
||||||
|
@ -30,8 +35,8 @@ export class SearchService implements SearchServiceAbstraction {
|
||||||
if (this.indexing) {
|
if (this.indexing) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// tslint:disable-next-line
|
|
||||||
console.time('search indexing');
|
this.logService.time('search indexing');
|
||||||
this.indexing = true;
|
this.indexing = true;
|
||||||
this.index = null;
|
this.index = null;
|
||||||
const builder = new lunr.Builder();
|
const builder = new lunr.Builder();
|
||||||
|
@ -62,8 +67,8 @@ export class SearchService implements SearchServiceAbstraction {
|
||||||
ciphers.forEach((c) => builder.add(c));
|
ciphers.forEach((c) => builder.add(c));
|
||||||
this.index = builder.build();
|
this.index = builder.build();
|
||||||
this.indexing = false;
|
this.indexing = false;
|
||||||
// tslint:disable-next-line
|
|
||||||
console.timeEnd('search indexing');
|
this.logService.timeEnd('search indexing');
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchCiphers(query: string,
|
async searchCiphers(query: string,
|
||||||
|
|
Loading…
Reference in New Issue