1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-06-26 10:36:19 +02:00

log service implementation

This commit is contained in:
Kyle Spearrin 2018-02-23 23:12:06 -05:00
parent 0ad1a482b9
commit 546d1e91e2
4 changed files with 68 additions and 2 deletions

2
jslib

@ -1 +1 @@
Subproject commit 9adabdab483aa463e38382bebdad40305f4974f1
Subproject commit b747830c5b4360bf50d6e02126ebaab24e3028d5

View File

@ -11,6 +11,7 @@ import { DesktopPlatformUtilsService } from '../../services/desktopPlatformUtils
import { DesktopRendererMessagingService } from '../../services/desktopRendererMessaging.service';
import { DesktopRendererSecureStorageService } from '../../services/desktopRendererSecureStorage.service';
import { DesktopStorageService } from '../../services/desktopStorage.service';
import { LogService } from '../../services/log.service';
import { I18nService } from '../../services/i18n.service';
import { AuthGuardService } from './auth-guard.service';
@ -49,6 +50,7 @@ import { EnvironmentService as EnvironmentServiceAbstraction } from 'jslib/abstr
import { FolderService as FolderServiceAbstraction } from 'jslib/abstractions/folder.service';
import { I18nService as I18nServiceAbstraction } from 'jslib/abstractions/i18n.service';
import { LockService as LockServiceAbstraction } from 'jslib/abstractions/lock.service';
import { LogService as LogServiceAbstraction } from 'jslib/abstractions/log.service';
import { MessagingService as MessagingServiceAbstraction } from 'jslib/abstractions/messaging.service';
import {
PasswordGenerationService as PasswordGenerationServiceAbstraction,
@ -63,6 +65,7 @@ import { TotpService as TotpServiceAbstraction } from 'jslib/abstractions/totp.s
import { UserService as UserServiceAbstraction } from 'jslib/abstractions/user.service';
import { UtilsService as UtilsServiceAbstraction } from 'jslib/abstractions/utils.service';
const logService = new LogService();
const i18nService = new I18nService(window.navigator.language, './locales');
const utilsService = new UtilsService();
const stateService = new StateService();
@ -161,6 +164,7 @@ function initFactory(): Function {
{ provide: LockServiceAbstraction, useValue: lockService },
{ provide: StorageServiceAbstraction, useValue: storageService },
{ provide: StateServiceAbstraction, useValue: stateService },
{ provide: LogServiceAbstraction, useValue: logService },
{
provide: APP_INITIALIZER,
useFactory: initFactory,

View File

@ -4,6 +4,7 @@ import * as path from 'path';
import { DesktopMainMessagingService } from './services/desktopMainMessaging.service';
import { DesktopStorageService } from './services/desktopStorage.service';
import { I18nService } from './services/i18n.service';
import { LogService } from './services/log.service';
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
@ -15,6 +16,7 @@ import { WindowMain } from './main/window.main';
const osLocale = require('os-locale');
export class Main {
logService: LogService;
i18nService: I18nService;
storageService: DesktopStorageService;
messagingService: DesktopMainMessagingService;
@ -38,8 +40,8 @@ export class Main {
if (appDataPath != null) {
app.setPath('userData', appDataPath);
app.setPath('logs', path.join(appDataPath, 'logs'));
}
app.setPath('logs', path.join(app.getPath('userData'), 'logs'));
const args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch');
@ -49,6 +51,7 @@ export class Main {
require('electron-reload')(__dirname, {});
}
this.logService = new LogService(null, app.getPath('logs'));
this.i18nService = new I18nService('en', './locales/');
this.storageService = new DesktopStorageService();
this.messagingService = new DesktopMainMessagingService(this);

View File

@ -0,0 +1,59 @@
import log from 'electron-log';
import * as path from 'path';
import { isDev } from '../scripts/utils';
import { LogLevelType } from 'jslib/enums/logLevelType';
import { LogService as LogServiceAbstraction } from 'jslib/abstractions/log.service';
export class LogService implements LogServiceAbstraction {
constructor(private filter: (level: LogLevelType) => boolean = null, logDir: string = null) {
if (logDir != null) {
log.transports.file.file = path.join(logDir, 'app.log');
}
}
debug(message: string) {
if (!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:
log.debug(message);
break;
case LogLevelType.Info:
log.info(message);
break;
case LogLevelType.Warning:
log.warn(message);
break;
case LogLevelType.Error:
log.error(message);
break;
default:
break;
}
}
}