Skip to content

Commit 22ffd1a

Browse files
committed
refactor: extract all loggers to helper
Signed-off-by: Adam Setch <[email protected]>
1 parent 1966a83 commit 22ffd1a

File tree

5 files changed

+122
-30
lines changed

5 files changed

+122
-30
lines changed

src/renderer/utils/auth/migration.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import log from 'electron-log';
21
import type { Account, AuthState } from '../../types';
32
import { Constants } from '../constants';
3+
import { logInfo } from '../logger';
44
import { loadState, saveState } from '../storage';
55
import { getUserData } from './utils';
66

@@ -16,15 +16,22 @@ export async function migrateAuthenticatedAccounts() {
1616
return;
1717
}
1818

19-
log.info('Account Migration: Commencing authenticated accounts migration');
19+
logInfo(
20+
'migrateAuthenticatedAccounts',
21+
'Commencing authenticated accounts migration',
22+
);
2023

2124
const migratedAccounts = await convertAccounts(existing.auth);
2225

2326
saveState({
2427
auth: { ...existing.auth, accounts: migratedAccounts },
2528
settings: existing.settings,
2629
});
27-
log.info('Account Migration: Authenticated accounts migration complete');
30+
31+
logInfo(
32+
'migrateAuthenticatedAccounts',
33+
'Authenticated accounts migration complete',
34+
);
2835
}
2936

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

src/renderer/utils/helpers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import {
44
ChevronRightIcon,
55
} from '@primer/octicons-react';
66
import { formatDistanceToNowStrict, parseISO } from 'date-fns';
7-
import log from 'electron-log';
87
import { defaultSettings } from '../context/App';
98
import type { Chevron, Hostname, Link, SettingsState } from '../types';
109
import type { Notification } from '../typesGitHub';
1110
import { getHtmlUrl, getLatestDiscussion } from './api/client';
1211
import type { PlatformType } from './auth/types';
1312
import { Constants } from './constants';
14-
import { logError } from './logger';
13+
import { logError, logWarn } from './logger';
1514
import {
1615
getCheckSuiteAttributes,
1716
getLatestDiscussionComment,
@@ -159,9 +158,9 @@ export async function generateGitHubWebUrl(
159158
notification,
160159
);
161160

162-
log.warn(
163-
'Will fall back to opening repository root url for',
164-
notification.repository.full_name,
161+
logWarn(
162+
'generateGitHubWebUrl',
163+
`Falling back to repository root url: ${notification.repository.full_name}`,
165164
);
166165
}
167166

src/renderer/utils/logger.test.ts

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,78 @@
11
import log from 'electron-log';
22
import { mockSingleNotification } from './api/__mocks__/response-mocks';
3-
import { logError } from './logger';
3+
import { logError, logInfo, logWarn } from './logger';
44

55
describe('renderer/utils/logger.ts', () => {
6+
const logInfoSpy = jest.spyOn(log, 'info').mockImplementation();
7+
const logWarnSpy = jest.spyOn(log, 'warn').mockImplementation();
68
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
9+
710
const mockError = new Error('baz');
811

912
beforeEach(() => {
13+
logInfoSpy.mockReset();
14+
logWarnSpy.mockReset();
1015
logErrorSpy.mockReset();
1116
});
1217

13-
it('log error without notification', () => {
14-
logError('foo', 'bar', mockError);
18+
describe('logInfo', () => {
19+
it('log info without notification', () => {
20+
logInfo('foo', 'bar');
21+
22+
expect(logInfoSpy).toHaveBeenCalledTimes(1);
23+
expect(logInfoSpy).toHaveBeenCalledWith('[foo]:', 'bar');
24+
});
25+
26+
it('log info with notification', () => {
27+
logInfo('foo', 'bar', mockSingleNotification);
28+
29+
expect(logInfoSpy).toHaveBeenCalledTimes(1);
30+
expect(logInfoSpy).toHaveBeenCalledWith(
31+
'[foo]:',
32+
'bar',
33+
'[Issue]: I am a robot and this is a test! for repository gitify-app/notifications-test',
34+
);
35+
});
36+
});
37+
38+
describe('logWarn', () => {
39+
it('log warn without notification', () => {
40+
logWarn('foo', 'bar');
41+
42+
expect(logWarnSpy).toHaveBeenCalledTimes(1);
43+
expect(logWarnSpy).toHaveBeenCalledWith('[foo]:', 'bar');
44+
});
45+
46+
it('log warn with notification', () => {
47+
logWarn('foo', 'bar', mockSingleNotification);
1548

16-
expect(logErrorSpy).toHaveBeenCalledTimes(1);
17-
expect(logErrorSpy).toHaveBeenCalledWith('[foo]: bar', mockError);
49+
expect(logWarnSpy).toHaveBeenCalledTimes(1);
50+
expect(logWarnSpy).toHaveBeenCalledWith(
51+
'[foo]:',
52+
'bar',
53+
'[Issue]: I am a robot and this is a test! for repository gitify-app/notifications-test',
54+
);
55+
});
1856
});
1957

20-
it('log error with notification', () => {
21-
logError('foo', 'bar', mockError, mockSingleNotification);
58+
describe('logError', () => {
59+
it('log error without notification', () => {
60+
logError('foo', 'bar', mockError);
61+
62+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
63+
expect(logErrorSpy).toHaveBeenCalledWith('[foo]:', 'bar', mockError);
64+
});
65+
66+
it('log error with notification', () => {
67+
logError('foo', 'bar', mockError, mockSingleNotification);
2268

23-
expect(logErrorSpy).toHaveBeenCalledTimes(1);
24-
expect(logErrorSpy).toHaveBeenCalledWith(
25-
'[foo]: bar',
26-
'[Issue]: I am a robot and this is a test! for repository gitify-app/notifications-test',
27-
mockError,
28-
);
69+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
70+
expect(logErrorSpy).toHaveBeenCalledWith(
71+
'[foo]:',
72+
'bar',
73+
'[Issue]: I am a robot and this is a test! for repository gitify-app/notifications-test',
74+
mockError,
75+
);
76+
});
2977
});
3078
});

src/renderer/utils/logger.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
11
import log from 'electron-log';
22
import type { Notification } from '../typesGitHub';
33

4-
export function logError(
4+
function logMessage(
5+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
6+
logFunction: (...params: any[]) => void,
57
type: string,
68
message: string,
7-
err: Error,
9+
err?: Error,
810
notification?: Notification,
911
) {
10-
if (notification) {
11-
log.error(
12-
`[${type}]: ${message}`,
12+
if (notification && err) {
13+
logFunction(
14+
`[${type}]:`,
15+
message,
1316
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
1417
err,
1518
);
19+
} else if (notification) {
20+
logFunction(
21+
`[${type}]:`,
22+
message,
23+
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
24+
);
25+
} else if (err) {
26+
logFunction(`[${type}]:`, message, err);
1627
} else {
17-
log.error(`[${type}]: ${message}`, err);
28+
logFunction(`[${type}]:`, message);
1829
}
1930
}
31+
32+
export function logInfo(
33+
type: string,
34+
message: string,
35+
notification?: Notification,
36+
) {
37+
logMessage(log.info, type, message, null, notification);
38+
}
39+
40+
export function logWarn(
41+
type: string,
42+
message: string,
43+
notification?: Notification,
44+
) {
45+
logMessage(log.warn, type, message, null, notification);
46+
}
47+
48+
export function logError(
49+
type: string,
50+
message: string,
51+
err: Error,
52+
notification?: Notification,
53+
) {
54+
logMessage(log.error, type, message, err, notification);
55+
}

src/renderer/utils/notifications.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import path from 'node:path';
2-
import log from 'electron-log';
32
import type {
43
AccountNotifications,
54
GitifyState,
@@ -12,7 +11,7 @@ import { getAccountUUID } from './auth/utils';
1211
import { hideWindow, showWindow, updateTrayIcon } from './comms';
1312
import { Constants } from './constants';
1413
import { openNotification } from './links';
15-
import { logError } from './logger';
14+
import { logError, logWarn } from './logger';
1615
import { isWindows } from './platform';
1716
import { getGitifySubjectDetails } from './subject';
1817

@@ -198,7 +197,10 @@ export async function enrichNotifications(
198197
notification,
199198
);
200199

201-
log.warn('Continuing with base notification details');
200+
logWarn(
201+
'enrichNotifications',
202+
'Continuing with base notification details',
203+
);
202204
}
203205

204206
return {

0 commit comments

Comments
 (0)