aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 20:13:45 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 20:13:45 +0100
commit5c38af061348ec604337280009775832edc66270 (patch)
tree4047f1e120116c8e2f893d9173b2f60e9ca36525
parentrefactor: Simpler logging with loglevel (diff)
downloadsophie-5c38af061348ec604337280009775832edc66270.tar.gz
sophie-5c38af061348ec604337280009775832edc66270.tar.zst
sophie-5c38af061348ec604337280009775832edc66270.zip
refactor: Switch back to consola for prettyness
-rw-r--r--packages/main/package.json3
-rw-r--r--packages/main/src/controllers/config.ts2
-rw-r--r--packages/main/src/utils/logging.ts28
-rw-r--r--packages/main/vite.config.js6
-rw-r--r--yarn.lock24
5 files changed, 22 insertions, 41 deletions
diff --git a/packages/main/package.json b/packages/main/package.json
index c4670a8..5010092 100644
--- a/packages/main/package.json
+++ b/packages/main/package.json
@@ -12,11 +12,10 @@
12 "dependencies": { 12 "dependencies": {
13 "@sophie/service-shared": "workspace:*", 13 "@sophie/service-shared": "workspace:*",
14 "@sophie/shared": "workspace:*", 14 "@sophie/shared": "workspace:*",
15 "consola": "^2.15.3",
15 "electron": "16.0.5", 16 "electron": "16.0.5",
16 "json5": "^2.2.0", 17 "json5": "^2.2.0",
17 "lodash": "^4.17.21", 18 "lodash": "^4.17.21",
18 "loglevel": "^1.8.0",
19 "loglevel-plugin-prefix": "^0.8.4",
20 "mobx": "^6.3.10", 19 "mobx": "^6.3.10",
21 "mobx-state-tree": "^5.1.0", 20 "mobx-state-tree": "^5.1.0",
22 "ms": "^2.1.3" 21 "ms": "^2.1.3"
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts
index 7187ab4..564658b 100644
--- a/packages/main/src/controllers/config.ts
+++ b/packages/main/src/controllers/config.ts
@@ -70,7 +70,7 @@ export async function initConfig(
70 logger.info('Config file was not found'); 70 logger.info('Config file was not found');
71 try { 71 try {
72 await writeConfig(); 72 await writeConfig();
73 logger.info('Created config file'); 73 logger.success('Created config file');
74 } catch (err) { 74 } catch (err) {
75 logger.error('Failed to initialize config'); 75 logger.error('Failed to initialize config');
76 } 76 }
diff --git a/packages/main/src/utils/logging.ts b/packages/main/src/utils/logging.ts
index 9f1133f..66f9068 100644
--- a/packages/main/src/utils/logging.ts
+++ b/packages/main/src/utils/logging.ts
@@ -18,32 +18,16 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import loglevel, { Logger } from 'loglevel'; 21import consola, { BasicReporter, Consola } from 'consola';
22import prefix from 'loglevel-plugin-prefix';
23 22
24const isDevelopment = import.meta.env.MODE === 'development'; 23const isDevelopment = import.meta.env.MODE === 'development';
25 24
26if (isDevelopment) { 25if (isDevelopment) {
27 loglevel.enableAll(); 26 // No fancy console in dev mode.
27 consola.setReporters([new BasicReporter()]);
28 consola.level = 4;
28} 29}
29 30
30prefix.reg(loglevel); 31export function getLogger(loggerName: string): Consola {
31prefix.apply(loglevel, { 32 return consola.withTag(`sophie:${loggerName}`);
32 format(level, name, timestamp) {
33 let shortName = 'global';
34 if (name !== undefined) {
35 const nameSegments = name.split(':');
36 const lastSegment = nameSegments.pop();
37 shortName = [...nameSegments.map((segment) => segment[0]), lastSegment].join(':');
38 }
39 if (isDevelopment) {
40 // `watch.js` already appends timestamps.
41 return `${level} (${shortName})`;
42 }
43 return `[${timestamp}] ${level} (${shortName})`;
44 },
45});
46
47export function getLogger(loggerName: string): Logger {
48 return loglevel.getLogger(`sophie:${loggerName}`);
49} 33}
diff --git a/packages/main/vite.config.js b/packages/main/vite.config.js
index bdb0ca1..b239b96 100644
--- a/packages/main/vite.config.js
+++ b/packages/main/vite.config.js
@@ -20,6 +20,12 @@ const config = makeConfig({
20 'global.process.env': 'global.process["env"]', 20 'global.process.env': 'global.process["env"]',
21 'globalThis.process.env': 'globalThis.process["env"]', 21 'globalThis.process.env': 'globalThis.process["env"]',
22 }, 22 },
23 resolve: {
24 alias: {
25 // Force the nodejs version of consola to be resolved.
26 'consola': 'consola/dist/consola.js',
27 },
28 },
23 build: { 29 build: {
24 target: node, 30 target: node,
25 lib: { 31 lib: {
diff --git a/yarn.lock b/yarn.lock
index d50d999..3040ea2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1204,12 +1204,11 @@ __metadata:
1204 "@types/lodash": ^4.14.178 1204 "@types/lodash": ^4.14.178
1205 "@types/ms": ^0.7.31 1205 "@types/ms": ^0.7.31
1206 "@types/node": ^17.0.5 1206 "@types/node": ^17.0.5
1207 consola: ^2.15.3
1207 electron: 16.0.5 1208 electron: 16.0.5
1208 electron-devtools-installer: ^3.2.0 1209 electron-devtools-installer: ^3.2.0
1209 json5: ^2.2.0 1210 json5: ^2.2.0
1210 lodash: ^4.17.21 1211 lodash: ^4.17.21
1211 loglevel: ^1.8.0
1212 loglevel-plugin-prefix: ^0.8.4
1213 mobx: ^6.3.10 1212 mobx: ^6.3.10
1214 mobx-state-tree: ^5.1.0 1213 mobx-state-tree: ^5.1.0
1215 ms: ^2.1.3 1214 ms: ^2.1.3
@@ -2587,6 +2586,13 @@ __metadata:
2587 languageName: node 2586 languageName: node
2588 linkType: hard 2587 linkType: hard
2589 2588
2589"consola@npm:^2.15.3":
2590 version: 2.15.3
2591 resolution: "consola@npm:2.15.3"
2592 checksum: 8ef7a09b703ec67ac5c389a372a33b6dc97eda6c9876443a60d76a3076eea0259e7f67a4e54fd5a52f97df73690822d090cf8b7e102b5761348afef7c6d03e28
2593 languageName: node
2594 linkType: hard
2595
2590"console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0": 2596"console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0":
2591 version: 1.1.0 2597 version: 1.1.0
2592 resolution: "console-control-strings@npm:1.1.0" 2598 resolution: "console-control-strings@npm:1.1.0"
@@ -5302,20 +5308,6 @@ __metadata:
5302 languageName: node 5308 languageName: node
5303 linkType: hard 5309 linkType: hard
5304 5310
5305"loglevel-plugin-prefix@npm:^0.8.4":
5306 version: 0.8.4
5307 resolution: "loglevel-plugin-prefix@npm:0.8.4"
5308 checksum: 5fe0632fa04263e083f87204107a06aa53e40a3537e08752539f5c0fd9a0ef112fe9ba6bdaed791502156c67a4ff7993a2b2871404615f0163f4c49649c362e4
5309 languageName: node
5310 linkType: hard
5311
5312"loglevel@npm:^1.8.0":
5313 version: 1.8.0
5314 resolution: "loglevel@npm:1.8.0"
5315 checksum: 41aeea17de24aba8dba68084a31fe9189648bce4f39c1277e021bb276c3c53a75b0d337395919cf271068ad40ecefabad0e4fdeb4a8f11908beee532b898f4a7
5316 languageName: node
5317 linkType: hard
5318
5319"loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": 5311"loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0":
5320 version: 1.4.0 5312 version: 1.4.0
5321 resolution: "loose-envify@npm:1.4.0" 5313 resolution: "loose-envify@npm:1.4.0"