Skip to content

Commit a31d8c3

Browse files
committed
feat: replacing electron-log with custom logger
1 parent 81a6395 commit a31d8c3

25 files changed

+955
-116
lines changed

electron/common/logger/create-logger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
Logger as ElectronLogger,
77
} from 'electron-log';
88
import { initializeLogging } from './initialize-logging.js';
9-
import type { LogFunction, Logger } from './types.js';
9+
import type { LogLevelFunction, Logger } from './types.js';
1010

1111
// Cache loggers for the same scope.
1212
const scopedLoggers: Record<string, ElectronLogFunctions> = {};
@@ -15,7 +15,7 @@ interface ElectronLogFunctionsExtended extends ElectronLogFunctions {
1515
/**
1616
* Alternative to electron logger's 'silly' level.
1717
*/
18-
trace: LogFunction;
18+
trace: LogLevelFunction;
1919
}
2020

2121
export const createLogger = (options: {

electron/common/logger/format-log-data.ts

-55
This file was deleted.

electron/common/logger/__tests__/format-log-data.test.ts electron/common/logger/format/__tests__/format-log-data.test.ts

-14
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,4 @@ describe('format-log-data', () => {
9292
},
9393
});
9494
});
95-
96-
it('masks sensitive values', () => {
97-
const data = {
98-
password: 'secret',
99-
apiKey: 'secret',
100-
};
101-
102-
const result = formatLogData(data);
103-
104-
expect(result).toEqual({
105-
password: '***REDACTED***',
106-
apiKey: '***REDACTED***',
107-
});
108-
});
10995
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import type { LogFormatter, LogMessage } from '../../types.js';
3+
import { LogLevel } from '../../types.js';
4+
import { jsonLogFormatter } from '../json-log-formatter.js';
5+
6+
describe('json-log-formatter', () => {
7+
const logDate = new Date('2022-01-02T00:00:00Z');
8+
9+
let formatter: LogFormatter;
10+
11+
beforeEach(() => {
12+
formatter = jsonLogFormatter({ colors: false });
13+
});
14+
15+
it('formats log message', () => {
16+
const message: LogMessage = {
17+
level: LogLevel.INFO,
18+
scope: 'test',
19+
message: 'hello world',
20+
timestamp: logDate,
21+
};
22+
23+
const result = formatter([message]);
24+
25+
expect(result).toEqual(
26+
`{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()} }\n`
27+
);
28+
});
29+
30+
it('formats log message with custom properties', () => {
31+
const message: LogMessage = {
32+
level: LogLevel.INFO,
33+
scope: 'test',
34+
message: 'hello world',
35+
timestamp: logDate,
36+
// Custom properties
37+
foo: 'bar',
38+
};
39+
40+
const result = formatter([message]);
41+
42+
expect(result).toEqual(
43+
`{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, foo: 'bar' }\n`
44+
);
45+
});
46+
47+
it('formats log message with complex properties', () => {
48+
const message: LogMessage = {
49+
level: LogLevel.INFO,
50+
scope: 'test',
51+
message: 'hello world',
52+
timestamp: logDate,
53+
// Custom properties
54+
boolean: false,
55+
number: 42,
56+
array: [1, 2, 3],
57+
date: logDate,
58+
set: new Set(['A', 'B', 'C']),
59+
map: new Map<string, any>([
60+
['A', 1],
61+
['B', logDate],
62+
['C', { foo: 'bar' }],
63+
]),
64+
};
65+
66+
const result = formatter([message]);
67+
68+
expect(result).toEqual(
69+
`{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, boolean: false, number: 42, array: [ 1, 2, 3 ], date: ${logDate.toISOString()}, set: [ 'A', 'B', 'C' ], map: { A: 1, B: '${logDate.toISOString()}', C: { foo: 'bar' } } }\n`
70+
);
71+
});
72+
73+
it('formats log message with sensitive properties', () => {
74+
const message: LogMessage = {
75+
level: LogLevel.INFO,
76+
scope: 'test',
77+
message: 'hello world',
78+
timestamp: logDate,
79+
// Custom properties
80+
password: 'secret',
81+
accessToken: 'secret',
82+
apiKey: 'secret',
83+
};
84+
85+
const result = formatter([message]);
86+
87+
expect(result).toEqual(
88+
`{ level: 'info', scope: 'test', message: 'hello world', timestamp: ${logDate.toISOString()}, password: '***REDACTED***', accessToken: '***REDACTED***', apiKey: '***REDACTED***' }\n`
89+
);
90+
});
91+
92+
it('formats log messages with colors', () => {
93+
formatter = jsonLogFormatter({ colors: true });
94+
95+
const messages: Array<LogMessage> = [
96+
{
97+
level: LogLevel.ERROR,
98+
scope: 'test',
99+
message: 'hello world',
100+
timestamp: logDate,
101+
},
102+
{
103+
level: LogLevel.WARN,
104+
scope: 'test',
105+
message: 'hello world',
106+
timestamp: logDate,
107+
},
108+
{
109+
level: LogLevel.INFO,
110+
scope: 'test',
111+
message: 'hello world',
112+
timestamp: logDate,
113+
},
114+
{
115+
level: LogLevel.DEBUG,
116+
scope: 'test',
117+
message: 'hello world',
118+
timestamp: logDate,
119+
},
120+
{
121+
level: LogLevel.TRACE,
122+
scope: 'test',
123+
message: 'hello world',
124+
timestamp: logDate,
125+
},
126+
];
127+
128+
const result = formatter(messages);
129+
const lines = result.trim().split('\n');
130+
131+
expect(lines).toHaveLength(messages.length);
132+
133+
// Note, the json formatter colorizes the values based on data type.
134+
// The log levels are strings and so all have the same color.
135+
// This behavior differs from the pretty formatter.
136+
137+
expect(lines[0]).toEqual(
138+
`{ level: \u001b[32m'error'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }`
139+
);
140+
141+
expect(lines[1]).toEqual(
142+
`{ level: \u001b[32m'warn'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }`
143+
);
144+
145+
expect(lines[2]).toEqual(
146+
`{ level: \u001b[32m'info'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }`
147+
);
148+
149+
expect(lines[3]).toEqual(
150+
`{ level: \u001b[32m'debug'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }`
151+
);
152+
153+
expect(lines[4]).toEqual(
154+
`{ level: \u001b[32m'trace'\u001b[39m, scope: \u001b[32m'test'\u001b[39m, message: \u001b[32m'hello world'\u001b[39m, timestamp: \u001b[35m2022-01-02T00:00:00.000Z\u001b[39m }`
155+
);
156+
});
157+
});

electron/common/logger/__tests__/mask-log-data.test.ts electron/common/logger/format/__tests__/mask-log-data.test.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
import { describe, expect, it } from 'vitest';
2-
import { isNotMaskable, maskSensitiveValues } from '../mask-log-data.js';
2+
import {
3+
isNotMaskable,
4+
maskLogData,
5+
maskSensitiveValues,
6+
} from '../mask-log-data.js';
37

48
describe('mask-log-data', () => {
9+
describe('#maskLogData', () => {
10+
const data: Record<string, any> = {
11+
accessToken: 'accessToken1',
12+
password: 'password1',
13+
apiKey: 'apiKey1',
14+
credential: 'credential1',
15+
nested: {
16+
accessToken: 'accessToken2',
17+
password: 'password2',
18+
apiKey: 'apiKey2',
19+
credential: 'credential2',
20+
},
21+
};
22+
23+
it('masks password, accessToken, and apiKey properties by default', () => {
24+
const result = maskLogData(data);
25+
26+
expect(result).toEqual({
27+
accessToken: '***REDACTED***',
28+
password: '***REDACTED***',
29+
apiKey: '***REDACTED***',
30+
credential: 'credential1',
31+
nested: {
32+
accessToken: '***REDACTED***',
33+
password: '***REDACTED***',
34+
apiKey: '***REDACTED***',
35+
credential: 'credential2',
36+
},
37+
});
38+
});
39+
});
40+
541
describe('#maskSensitiveValues', () => {
642
const data: Record<string, any> = {
743
accessToken: 'accessToken1',
@@ -18,7 +54,7 @@ describe('mask-log-data', () => {
1854

1955
it('masks password, accessToken, and apiKey properties by default', () => {
2056
const result = maskSensitiveValues({
21-
json: data,
57+
object: data,
2258
});
2359

2460
expect(result).toEqual({
@@ -37,7 +73,7 @@ describe('mask-log-data', () => {
3773

3874
it('masks specified properties', () => {
3975
const result = maskSensitiveValues({
40-
json: data,
76+
object: data,
4177
keys: ['apiKey', 'credential'],
4278
});
4379

@@ -57,7 +93,7 @@ describe('mask-log-data', () => {
5793

5894
it('masks specified properties with custom mask', () => {
5995
const result = maskSensitiveValues({
60-
json: data,
96+
object: data,
6197
keys: ['apiKey', 'credential'],
6298
mask: '***MASKED***',
6399
});

0 commit comments

Comments
 (0)