Skip to content

Commit

Permalink
[#302] Add renderer logging and refactor main process logging
Browse files Browse the repository at this point in the history
  • Loading branch information
SRichner committed Mar 18, 2024
1 parent 5bc5fc7 commit 9f45f4c
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 42 deletions.
13 changes: 13 additions & 0 deletions src/electron/electron/config/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logger from 'electron-log/main';
import { LogFunctions } from 'electron-log';
import { LOG_FILE_NAME, LOG_LEVEL } from './logger.config';

export const getMainLogger = (loggerName: string): LogFunctions => {
logger.transports.file.fileName = `${LOG_FILE_NAME.BACKGROUND}.log`;
logger.transports.file.maxSize = 15 * 1024 * 1024;
logger.transports.file.level = LOG_LEVEL.FILE;
logger.transports.console.level = LOG_LEVEL.CONSOLE;
return logger.scope(loggerName);
};

export default getMainLogger;
4 changes: 2 additions & 2 deletions src/electron/electron/ipc/IpcHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExperienceSamplingService } from '../main/services/ExperienceSamplingService';
import { app, ipcMain, IpcMainInvokeEvent, shell, systemPreferences } from 'electron';
import { WindowService } from '../main/services/WindowService';
import { getLogger } from '../shared/Logger';
import { getMainLogger } from '../config/Logger';
import { TypedIpcMain } from '../../src/utils/TypedIpcMain';
import Commands from '../../src/utils/Commands';
import Events from '../../src/utils/Events';
Expand All @@ -18,7 +18,7 @@ import WindowActivityDto from '../../shared/dto/WindowActivityDto';
import ExperienceSamplingDto from '../../shared/dto/ExperienceSamplingDto';
import { is } from '../main/services/utils/helpers';

const LOG = getLogger('IpcHandler');
const LOG = getMainLogger('IpcHandler');

export class IpcHandler {
private readonly actions: any;
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { release } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import log from 'electron-log/main';
import { getLogger } from '../shared/Logger';
import { getMainLogger } from '../config/Logger';
import { DatabaseService } from './services/DatabaseService';
import { SettingsService } from './services/SettingsService';
import { TrackerType } from '../enums/TrackerType.enum';
Expand Down Expand Up @@ -59,7 +59,7 @@ if (is.macOS) {

// Optional, initialize the logger for any renderer process
log.initialize();
const LOG = getLogger('Main');
const LOG = getMainLogger('Main');

app.whenReady().then(async () => {
app.setAppUserModelId('ch.ifi.hasel.personal-analytics');
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/services/AppUpdaterService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import { dialog, net } from 'electron';
import { EventEmitter } from 'events';
import updater from 'electron-updater';
import studyConfig from '../../../shared/study.config';

const { autoUpdater } = updater;

const LOG = getLogger('AutoUpdater');
const LOG = getMainLogger('AutoUpdater');

export default class AppUpdaterService extends EventEmitter {
private checkForUpdatesInterval: NodeJS.Timeout | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/services/DataExportService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataExportType } from '../../../shared/DataExportType.enum';
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import path from 'path';
import { app } from 'electron';
import { is } from './utils/helpers';
Expand All @@ -11,7 +11,7 @@ import { Settings } from '../entities/Settings';
import { UsageDataService } from './UsageDataService';
import { UsageDataEventType } from '../../enums/UsageDataEventType.enum';

const LOG = getLogger('DataExportService');
const LOG = getMainLogger('DataExportService');

export class DataExportService {
private readonly windowActivityTrackerService: WindowActivityTrackerService =
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/services/DatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { DataSource, DataSourceOptions } from 'typeorm';
import { app } from 'electron';
import path from 'path';
import { is } from './utils/helpers';
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import { WindowActivityEntity } from '../entities/WindowActivityEntity';
import { ExperienceSamplingResponseEntity } from '../entities/ExperienceSamplingResponseEntity';
import { UserInputEntity } from '../entities/UserInputEntity';
import { Settings } from '../entities/Settings';
import { UsageDataEntity } from '../entities/UsageDataEntity';

const LOG = getLogger('DatabaseService');
const LOG = getMainLogger('DatabaseService');

export class DatabaseService {
public dataSource: DataSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ExperienceSamplingResponseEntity } from '../entities/ExperienceSamplingResponseEntity';
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import ExperienceSamplingDto from '../../../shared/dto/ExperienceSamplingDto';

const LOG = getLogger('ExperienceSamplingService');
const LOG = getMainLogger('ExperienceSamplingService');

export class ExperienceSamplingService {
public async createExperienceSample(
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/services/SettingsService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Settings } from '../entities/Settings';
import { generateAlphaNumericString } from './utils/helpers';
import studyConfig from '../../../shared/study.config';
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';

const LOG = getLogger('SettingsService');
const LOG = getMainLogger('SettingsService');

export class SettingsService {
public async init(): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/main/services/UsageDataService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import { UsageDataEventType } from '../../enums/UsageDataEventType.enum';
import { UsageDataEntity } from '../entities/UsageDataEntity';

const LOG = getLogger('UsageDataService');
const LOG = getMainLogger('UsageDataService');

export class UsageDataService {
public static async createNewUsageDataEvent(
Expand Down
5 changes: 3 additions & 2 deletions src/electron/electron/main/services/WindowService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { app, BrowserWindow, Menu, nativeImage, screen, shell, Tray } from 'electron';
import { getLogger } from '../../shared/Logger';
import getMainLogger from '../../config/Logger';
import AppUpdaterService from './AppUpdaterService';
import { is } from './utils/helpers';
import path from 'path';
import MenuItemConstructorOptions = Electron.MenuItemConstructorOptions;
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import studyConfig from '../../../shared/study.config';

import { Settings } from '../entities/Settings';
import { UsageDataService } from './UsageDataService';
import { UsageDataEventType } from '../../enums/UsageDataEventType.enum';

const LOG = getLogger('WindowService');
const LOG = getMainLogger('WindowService');

export class WindowService {
private readonly appUpdaterService: AppUpdaterService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as schedule from 'node-schedule';
import { WindowService } from '../WindowService';
import { Tracker } from './Tracker';
import { getLogger } from '../../../shared/Logger';
import getMainLogger from '../../../config/Logger';
import { Settings } from '../../entities/Settings';
import { powerMonitor } from 'electron';

const LOG = getLogger('ExperienceSamplingTracker');
const LOG = getMainLogger('ExperienceSamplingTracker');

export class ExperienceSamplingTracker implements Tracker {
private checkIfExperienceSamplingIsDueJob: schedule.Job;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as schedule from 'node-schedule';
import { Tracker } from './Tracker';
import { TrackerConfig } from '../../../types/StudyConfig';
import { TrackerType } from '../../../enums/TrackerType.enum';
import { getLogger } from '../../../shared/Logger';
import getMainLogger from '../../../config/Logger';
import { ExperienceSamplingTracker } from './ExperienceSamplingTracker';
import { WindowService } from '../WindowService';
import studyConfig from '../../../../shared/study.config';
import { UserInputEntity } from '../../entities/UserInputEntity';
import { MoreThanOrEqual } from 'typeorm';

const LOG = getLogger('TrackerService');
const LOG = getMainLogger('TrackerService');

export class TrackerService {
private trackers: Tracker[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ActiveWindow from 'windows-activity-tracker/dist/types/ActiveWindow';
import { WindowActivityEntity } from '../../entities/WindowActivityEntity';
import { In } from 'typeorm';
import { getLogger } from '../../../shared/Logger';
import getMainLogger from '../../../config/Logger';
import WindowActivityDto from '../../../../shared/dto/WindowActivityDto';

const LOG = getLogger('WindowActivityTrackerService');
const LOG = getMainLogger('WindowActivityTrackerService');

export class WindowActivityTrackerService {
private randomStringMap: Map<string, string> = new Map<string, string>();
Expand Down
17 changes: 0 additions & 17 deletions src/electron/electron/shared/Logger.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/electron/src/utils/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LogFunctions } from 'electron-log';
import rendererLogger from 'electron-log/renderer';

const getRendererLogger = (loggerName: string): LogFunctions => {
return rendererLogger.scope(`Renderer/${loggerName}`);
};
export default getRendererLogger;
5 changes: 4 additions & 1 deletion src/electron/src/views/DataExportView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import WindowActivityDto from '../../shared/dto/WindowActivityDto';
import UserInputDto from '../../shared/dto/UserInputDto';
import DataExportExperienceSamplingTracker from '../components/DataExportExperienceSamplingTracker.vue';
import ExperienceSamplingDto from '../../shared/dto/ExperienceSamplingDto';
import getRendererLogger from '../utils/Logger';
const LOG = getRendererLogger('DataExportView');
const currentStep = ref(0);
const transitionName = ref('slide-lef-right');
Expand Down Expand Up @@ -169,7 +172,7 @@ async function handleNextStep() {
// Also update the DataExportService if you change the file name here
fileName.value = `PA_${studyInfo.value?.subjectId}_${nowStr}.sqlite`;
} catch (e) {
console.error(e);
LOG.error(e);
hasExportError.value = true;
}
isExporting.value = false;
Expand Down

0 comments on commit 9f45f4c

Please sign in to comment.