Skip to content

Commit 0e8d274

Browse files
authored
refactor(shared): create electron-log wrapper (#1692)
* refactor: extract error logger helper Signed-off-by: Adam Setch <[email protected]> * refactor: extract all loggers to helper Signed-off-by: Adam Setch <[email protected]> * refactor: extract all loggers to helper Signed-off-by: Adam Setch <[email protected]> * move to shared folder Signed-off-by: Adam Setch <[email protected]> * refactor: extract all loggers to helper Signed-off-by: Adam Setch <[email protected]> * refactor: extract all loggers to helper Signed-off-by: Adam Setch <[email protected]> --------- Signed-off-by: Adam Setch <[email protected]>
1 parent 0997a6e commit 0e8d274

21 files changed

+246
-70
lines changed

src/main/first-run.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { app, dialog } from 'electron';
4-
import log from 'electron-log';
4+
5+
import { logError } from '../shared/logger';
56

67
export async function onFirstRunMaybe() {
78
if (isFirstRun()) {
@@ -49,7 +50,7 @@ function isFirstRun() {
4950

5051
fs.writeFileSync(configPath, '');
5152
} catch (err) {
52-
log.error('First run: Unable to write firstRun file', err);
53+
logError('isFirstRun', 'Unable to write firstRun file', err);
5354
}
5455

5556
return true;

src/main/updater.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import log from 'electron-log';
22
import { autoUpdater } from 'electron-updater';
33
import type { Menubar } from 'menubar';
44
import { updateElectronApp } from 'update-electron-app';
5+
6+
import { logError, logInfo } from '../shared/logger';
57
import type MenuBuilder from './menu';
68

79
export default class Updater {
@@ -18,29 +20,29 @@ export default class Updater {
1820
});
1921

2022
autoUpdater.on('checking-for-update', () => {
21-
log.info('Auto Updater: Checking for update');
23+
logInfo('auto updater', 'Checking for update');
2224
this.menuBuilder.setCheckForUpdatesMenuEnabled(false);
2325
});
2426

25-
autoUpdater.on('error', (error) => {
26-
log.error('Auto Updater: error checking for update', error);
27+
autoUpdater.on('error', (err) => {
28+
logError('auto updater', 'Error checking for update', err);
2729
this.menuBuilder.setCheckForUpdatesMenuEnabled(true);
2830
});
2931

3032
autoUpdater.on('update-available', () => {
31-
log.info('Auto Updater: New update available');
33+
logInfo('auto updater', 'New update available');
3234
menuBuilder.setUpdateAvailableMenuEnabled(true);
3335
this.menubar.tray.setToolTip('Gitify\nA new update is available');
3436
});
3537

3638
autoUpdater.on('update-downloaded', () => {
37-
log.info('Auto Updater: Update downloaded');
39+
logInfo('auto updater', 'Update downloaded');
3840
menuBuilder.setUpdateReadyForInstallMenuEnabled(true);
3941
this.menubar.tray.setToolTip('Gitify\nA new update is ready to install');
4042
});
4143

4244
autoUpdater.on('update-not-available', () => {
43-
log.info('Auto Updater: update not available');
45+
logInfo('auto updater', 'Update not available');
4446
this.menuBuilder.setCheckForUpdatesMenuEnabled(true);
4547
});
4648
}

src/main/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { dialog, shell } from 'electron';
55
import log from 'electron-log';
66
import type { Menubar } from 'menubar';
77

8+
import { logError, logInfo } from '../shared/logger';
9+
810
export function takeScreenshot(mb: Menubar) {
911
const date = new Date();
1012
const dateStr = date.toISOString().replace(/:/g, '-');
1113

1214
const capturedPicFilePath = `${os.homedir()}/${dateStr}-gitify-screenshot.png`;
1315
mb.window.capturePage().then((img) => {
1416
fs.writeFile(capturedPicFilePath, img.toPNG(), () =>
15-
log.info(`Screenshot saved ${capturedPicFilePath}`),
17+
logInfo('takeScreenshot', `Screenshot saved ${capturedPicFilePath}`),
1618
);
1719
});
1820
}
@@ -41,7 +43,11 @@ export function openLogsDirectory() {
4143
const logDirectory = path.dirname(log.transports.file?.getFile()?.path);
4244

4345
if (!logDirectory) {
44-
log.error('Could not find log directory!');
46+
logError(
47+
'openLogsDirectory',
48+
'Could not find log directory!',
49+
new Error('Directory not found'),
50+
);
4551
return;
4652
}
4753

src/renderer/hooks/useNotifications.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
22
import axios, { AxiosError } from 'axios';
33
import nock from 'nock';
44

5-
import log from 'electron-log';
5+
import * as logger from '../../shared/logger';
66
import {
77
mockAuth,
88
mockGitHubCloudAccount,
@@ -17,7 +17,7 @@ import { Errors } from '../utils/errors';
1717
import { useNotifications } from './useNotifications';
1818

1919
describe('renderer/hooks/useNotifications.ts', () => {
20-
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
20+
const logErrorSpy = jest.spyOn(logger, 'logError').mockImplementation();
2121

2222
beforeEach(() => {
2323
// axios will default to using the XHR adapter which can't be intercepted

src/renderer/hooks/useNotifications.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import log from 'electron-log';
21
import { useCallback, useState } from 'react';
2+
3+
import { logError } from '../../shared/logger';
34
import type {
45
Account,
56
AccountNotifications,
@@ -121,8 +122,9 @@ export const useNotifications = (): NotificationsState => {
121122
setNotifications(updatedNotifications);
122123
setTrayIconColor(updatedNotifications);
123124
} catch (err) {
124-
log.error(
125-
'[markNotificationsAsRead]: Error occurred while marking notifications as read',
125+
logError(
126+
'markNotificationsAsRead',
127+
'Error occurred while marking notifications as read',
126128
err,
127129
);
128130
}
@@ -158,8 +160,9 @@ export const useNotifications = (): NotificationsState => {
158160
setNotifications(updatedNotifications);
159161
setTrayIconColor(updatedNotifications);
160162
} catch (err) {
161-
log.error(
162-
'[markNotificationsAsDone]: error occurred while marking notifications as done',
163+
logError(
164+
'markNotificationsAsDone',
165+
'Error occurred while marking notifications as done',
163166
err,
164167
);
165168
}
@@ -186,9 +189,11 @@ export const useNotifications = (): NotificationsState => {
186189
await markNotificationsAsRead(state, [notification]);
187190
}
188191
} catch (err) {
189-
log.error(
190-
'[unsubscribeNotification]: error occurred while unsubscribing from notification thread',
192+
logError(
193+
'unsubscribeNotification',
194+
'Error occurred while unsubscribing from notification thread',
191195
err,
196+
notification,
192197
);
193198
}
194199

src/renderer/routes/Accounts.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
StarIcon,
1111
SyncIcon,
1212
} from '@primer/octicons-react';
13-
14-
import log from 'electron-log';
1513
import { type FC, useCallback, useContext } from 'react';
1614
import { useNavigate } from 'react-router-dom';
15+
16+
import { logError } from '../../shared/logger';
1717
import { Header } from '../components/Header';
1818
import { AuthMethodIcon } from '../components/icons/AuthMethodIcon';
1919
import { AvatarIcon } from '../components/icons/AvatarIcon';
@@ -57,7 +57,7 @@ export const AccountsRoute: FC = () => {
5757
try {
5858
await loginWithGitHubApp();
5959
} catch (err) {
60-
log.error('Auth: failed to login with GitHub', err);
60+
logError('loginWithGitHub', 'failed to login with GitHub', err);
6161
}
6262
}, []);
6363

src/renderer/routes/Login.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { KeyIcon, MarkGithubIcon, PersonIcon } from '@primer/octicons-react';
2-
import log from 'electron-log';
32
import { type FC, useCallback, useContext, useEffect } from 'react';
43
import { useNavigate } from 'react-router-dom';
4+
5+
import { logError } from '../../shared/logger';
56
import { Button } from '../components/buttons/Button';
67
import { LogoIcon } from '../components/icons/LogoIcon';
78
import { AppContext } from '../context/App';
@@ -23,7 +24,7 @@ export const LoginRoute: FC = () => {
2324
try {
2425
await loginWithGitHubApp();
2526
} catch (err) {
26-
log.error('Auth: failed to login with GitHub', err);
27+
logError('loginWithGitHubApp', 'failed to login with GitHub', err);
2728
}
2829
}, [loginWithGitHubApp]);
2930

src/renderer/routes/LoginWithOAuthApp.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BookIcon, PersonIcon, SignInIcon } from '@primer/octicons-react';
2-
import log from 'electron-log';
32
import { type FC, useCallback, useContext } from 'react';
43
import { Form, type FormRenderProps } from 'react-final-form';
54
import { useNavigate } from 'react-router-dom';
5+
6+
import { logError } from '../../shared/logger';
67
import { Header } from '../components/Header';
78
import { Button } from '../components/buttons/Button';
89
import { FieldInput } from '../components/fields/FieldInput';
@@ -131,7 +132,7 @@ export const LoginWithOAuthAppRoute: FC = () => {
131132
await loginWithOAuthApp(data as LoginOAuthAppOptions);
132133
navigate(-1);
133134
} catch (err) {
134-
log.error('Auth: Failed to login with oauth app', err);
135+
logError('loginWithOAuthApp', 'Failed to login with OAuth App', err);
135136
}
136137
},
137138
[loginWithOAuthApp],

src/renderer/routes/LoginWithPersonalAccessToken.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BookIcon, KeyIcon, SignInIcon } from '@primer/octicons-react';
2-
import log from 'electron-log';
32
import { type FC, useCallback, useContext, useState } from 'react';
43
import { Form, type FormRenderProps } from 'react-final-form';
54
import { useNavigate } from 'react-router-dom';
5+
6+
import { logError } from '../../shared/logger';
67
import { Header } from '../components/Header';
78
import { Button } from '../components/buttons/Button';
89
import { FieldInput } from '../components/fields/FieldInput';
@@ -129,7 +130,11 @@ export const LoginWithPersonalAccessTokenRoute: FC = () => {
129130
);
130131
navigate(-1);
131132
} catch (err) {
132-
log.error('Auth: failed to login with personal access token', err);
133+
logError(
134+
'loginWithPersonalAccessToken',
135+
'Failed to login with PAT',
136+
err,
137+
);
133138
setIsValidToken(false);
134139
}
135140
},

src/renderer/utils/api/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { type AxiosPromise, type AxiosResponse } from 'axios';
2-
import log from 'electron-log';
2+
import * as logger from '../../../shared/logger';
33
import {
44
mockGitHubCloudAccount,
55
mockGitHubEnterpriseServerAccount,
@@ -268,7 +268,7 @@ describe('renderer/utils/api/client.ts', () => {
268268
});
269269

270270
it('should handle error', async () => {
271-
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
271+
const logErrorSpy = jest.spyOn(logger, 'logError').mockImplementation();
272272

273273
const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');
274274

0 commit comments

Comments
 (0)