Skip to content

Commit ef98f1a

Browse files
authored
feat: add electron-log (#1400)
* feat: add electron-log * feat: add electron-log
1 parent e227132 commit ef98f1a

File tree

10 files changed

+42
-25
lines changed

10 files changed

+42
-25
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"class-variance-authority": "0.7.0",
107107
"clsx": "2.1.1",
108108
"date-fns": "3.6.0",
109+
"electron-log": "5.1.6",
109110
"electron-updater": "6.3.0",
110111
"final-form": "4.20.10",
111112
"menubar": "9.5.0",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/electron/first-run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { app, dialog } = require('electron');
2-
32
const fs = require('node:fs');
43
const path = require('node:path');
4+
const log = require('electron-log');
55

66
async function onFirstRunMaybe() {
77
if (isFirstRun()) {
@@ -49,7 +49,7 @@ function isFirstRun() {
4949

5050
fs.writeFileSync(configPath, '');
5151
} catch (error) {
52-
console.warn('First run: Unable to write firstRun file', error);
52+
log.warn('First run: Unable to write firstRun file', error);
5353
}
5454

5555
return true;

src/electron/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const { menubar } = require('menubar');
99
const { autoUpdater } = require('electron-updater');
1010
const { onFirstRunMaybe } = require('./first-run');
1111
const path = require('node:path');
12+
const log = require('electron-log');
13+
14+
log.initialize();
15+
autoUpdater.logger = log;
1216

1317
// TODO: Remove @electron/remote use - see #650
1418
require('@electron/remote/main').initialize();

src/utils/api/client.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios, { type AxiosPromise, type AxiosResponse } from 'axios';
2+
import log from 'electron-log';
23
import {
34
mockGitHubCloudAccount,
45
mockGitHubEnterpriseServerAccount,
@@ -278,7 +279,7 @@ describe('utils/api/client.ts', () => {
278279
});
279280

280281
it('should handle error', async () => {
281-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
282+
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
282283

283284
const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');
284285

@@ -291,7 +292,7 @@ describe('utils/api/client.ts', () => {
291292
'123' as Token,
292293
);
293294

294-
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to get html url');
295+
expect(logErrorSpy).toHaveBeenCalledWith('Failed to get html url');
295296
});
296297
});
297298
});

src/utils/api/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AxiosPromise } from 'axios';
2+
import log from 'electron-log';
23
import { print } from 'graphql/language/printer';
34
import type {
45
Account,
@@ -224,7 +225,7 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
224225
const response = (await apiRequestAuth(url, 'GET', token)).data;
225226
return response.html_url;
226227
} catch (err) {
227-
console.error('Failed to get html url');
228+
log.error('Failed to get html url');
228229
}
229230
}
230231

src/utils/auth/migration.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import log from 'electron-log';
23
import nock from 'nock';
34
import { mockGitifyUser, mockToken } from '../../__mocks__/state-mocks';
45
import type { AuthState, Hostname } from '../../types';
@@ -20,6 +21,7 @@ describe('utils/auth/migration.ts', () => {
2021

2122
describe('migrateAuthenticatedAccounts', () => {
2223
it('migrate and save legacy accounts', async () => {
24+
const logInfoSpy = jest.spyOn(log, 'info').mockImplementation();
2325
jest.spyOn(localStorage.__proto__, 'getItem').mockReturnValueOnce(
2426
JSON.stringify({
2527
auth: {
@@ -36,7 +38,7 @@ describe('utils/auth/migration.ts', () => {
3638
await migrateAuthenticatedAccounts();
3739

3840
expect(localStorage.setItem).toHaveBeenCalledTimes(1);
39-
expect(console.log).toHaveBeenCalledTimes(2);
41+
expect(logInfoSpy).toHaveBeenCalledTimes(2);
4042
});
4143
});
4244

src/utils/auth/migration.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1+
import log from 'electron-log';
12
import type { Account, AuthState } from '../../types';
23
import Constants from '../constants';
34
import { loadState, saveState } from '../storage';
45
import { getUserData } from './utils';
56

6-
export function logMigrationProgress(msg: string) {
7-
// biome-ignore lint/suspicious/noConsoleLog: log migration progress
8-
console.log(`Account Migration: ${msg}`);
9-
}
10-
117
/**
128
* Migrate authenticated accounts from old data structure to new data structure (v5.7.0+).
139
*
@@ -16,17 +12,20 @@ export function logMigrationProgress(msg: string) {
1612
export async function migrateAuthenticatedAccounts() {
1713
const existing = loadState();
1814

19-
if (hasAccountsToMigrate(existing.auth)) {
20-
logMigrationProgress('Commencing authenticated accounts migration');
15+
if (!hasAccountsToMigrate(existing.auth)) {
16+
log.info('Account Migration: No accounts need migrating');
17+
return;
18+
}
19+
20+
log.info('Account Migration: Commencing authenticated accounts migration');
2121

22-
const migratedAccounts = await convertAccounts(existing.auth);
22+
const migratedAccounts = await convertAccounts(existing.auth);
2323

24-
saveState({
25-
auth: { ...existing.auth, accounts: migratedAccounts },
26-
settings: existing.settings,
27-
});
28-
logMigrationProgress('Authenticated accounts migration complete');
29-
}
24+
saveState({
25+
auth: { ...existing.auth, accounts: migratedAccounts },
26+
settings: existing.settings,
27+
});
28+
log.info('Account Migration: Authenticated accounts migration complete');
3029
}
3130

3231
export function hasAccountsToMigrate(existingAuthState: AuthState): boolean {

src/utils/subject.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const mockDiscussionAuthor: DiscussionAuthor = {
3030
avatar_url: 'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
3131
type: 'User',
3232
};
33+
import log from 'electron-log';
3334

3435
describe('utils/subject.ts', () => {
3536
beforeEach(() => {
@@ -1150,9 +1151,7 @@ describe('utils/subject.ts', () => {
11501151

11511152
describe('Error', () => {
11521153
it('catches error and logs message', async () => {
1153-
const consoleErrorSpy = jest
1154-
.spyOn(console, 'error')
1155-
.mockImplementation();
1154+
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
11561155

11571156
const mockError = new Error('Test error');
11581157
const mockNotification = partialMockNotification({
@@ -1167,7 +1166,7 @@ describe('utils/subject.ts', () => {
11671166

11681167
await getGitifySubjectDetails(mockNotification);
11691168

1170-
expect(consoleErrorSpy).toHaveBeenCalledWith(
1169+
expect(logErrorSpy).toHaveBeenCalledWith(
11711170
'Error occurred while fetching details for Issue notification: This issue will throw an error',
11721171
mockError,
11731172
);

src/utils/subject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import log from 'electron-log';
12
import type { Link } from '../types';
23
import type {
34
CheckSuiteAttributes,
@@ -48,7 +49,7 @@ export async function getGitifySubjectDetails(
4849
return null;
4950
}
5051
} catch (err) {
51-
console.error(
52+
log.error(
5253
`Error occurred while fetching details for ${notification.subject.type} notification: ${notification.subject.title}`,
5354
err,
5455
);

0 commit comments

Comments
 (0)