diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-28 19:56:09 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-28 19:56:09 +0100 |
commit | e2a611620027063d3c8a60c257004c847bf636da (patch) | |
tree | 84f9f9c1d6d753d7ce15c2b7e441b6bb06869c3a | |
parent | feat: Add consola logging (diff) | |
download | sophie-e2a611620027063d3c8a60c257004c847bf636da.tar.gz sophie-e2a611620027063d3c8a60c257004c847bf636da.tar.zst sophie-e2a611620027063d3c8a60c257004c847bf636da.zip |
refactor: Simpler logging with loglevel
-rw-r--r-- | packages/main/package.json | 3 | ||||
-rw-r--r-- | packages/main/src/controllers/config.ts | 7 | ||||
-rw-r--r-- | packages/main/src/controllers/nativeTheme.ts | 5 | ||||
-rw-r--r-- | packages/main/src/utils/index.ts (renamed from packages/main/src/utils.ts) | 2 | ||||
-rw-r--r-- | packages/main/src/utils/logging.ts | 49 | ||||
-rw-r--r-- | packages/main/vite.config.js | 6 | ||||
-rw-r--r-- | yarn.lock | 24 |
7 files changed, 74 insertions, 22 deletions
diff --git a/packages/main/package.json b/packages/main/package.json index 5010092..c4670a8 100644 --- a/packages/main/package.json +++ b/packages/main/package.json | |||
@@ -12,10 +12,11 @@ | |||
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", | ||
16 | "electron": "16.0.5", | 15 | "electron": "16.0.5", |
17 | "json5": "^2.2.0", | 16 | "json5": "^2.2.0", |
18 | "lodash": "^4.17.21", | 17 | "lodash": "^4.17.21", |
18 | "loglevel": "^1.8.0", | ||
19 | "loglevel-plugin-prefix": "^0.8.4", | ||
19 | "mobx": "^6.3.10", | 20 | "mobx": "^6.3.10", |
20 | "mobx-state-tree": "^5.1.0", | 21 | "mobx-state-tree": "^5.1.0", |
21 | "ms": "^2.1.3" | 22 | "ms": "^2.1.3" |
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts index b6aba35..7187ab4 100644 --- a/packages/main/src/controllers/config.ts +++ b/packages/main/src/controllers/config.ts | |||
@@ -18,18 +18,17 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import consola from 'consola'; | ||
22 | import { debounce } from 'lodash'; | 21 | import { debounce } from 'lodash'; |
23 | import ms from 'ms'; | 22 | import ms from 'ms'; |
24 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; | 23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; |
25 | 24 | ||
26 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; | 25 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; |
27 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; | 26 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; |
28 | import { Disposer } from '../utils'; | 27 | import { Disposer, getLogger } from '../utils'; |
29 | 28 | ||
30 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); | 29 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); |
31 | 30 | ||
32 | const logger = consola.withTag('sophie:controller:config'); | 31 | const logger = getLogger('controller:config'); |
33 | 32 | ||
34 | export async function initConfig( | 33 | export async function initConfig( |
35 | config: Config, | 34 | config: Config, |
@@ -71,7 +70,7 @@ export async function initConfig( | |||
71 | logger.info('Config file was not found'); | 70 | logger.info('Config file was not found'); |
72 | try { | 71 | try { |
73 | await writeConfig(); | 72 | await writeConfig(); |
74 | logger.success('Created config file'); | 73 | logger.info('Created config file'); |
75 | } catch (err) { | 74 | } catch (err) { |
76 | logger.error('Failed to initialize config'); | 75 | logger.error('Failed to initialize config'); |
77 | } | 76 | } |
diff --git a/packages/main/src/controllers/nativeTheme.ts b/packages/main/src/controllers/nativeTheme.ts index 6548771..9f9bc21 100644 --- a/packages/main/src/controllers/nativeTheme.ts +++ b/packages/main/src/controllers/nativeTheme.ts | |||
@@ -18,14 +18,13 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import consola from 'consola'; | ||
22 | import { nativeTheme } from 'electron'; | 21 | import { nativeTheme } from 'electron'; |
23 | import { autorun } from 'mobx'; | 22 | import { autorun } from 'mobx'; |
24 | 23 | ||
25 | import type { MainStore } from '../stores/MainStore'; | 24 | import type { MainStore } from '../stores/MainStore'; |
26 | import { Disposer } from '../utils'; | 25 | import { Disposer, getLogger } from '../utils'; |
27 | 26 | ||
28 | const logger = consola.withTag('sophie:controller:nativeTheme'); | 27 | const logger = getLogger('controller:nativeTheme'); |
29 | 28 | ||
30 | export function initNativeTheme(store: MainStore): Disposer { | 29 | export function initNativeTheme(store: MainStore): Disposer { |
31 | logger.debug('Initializing controller'); | 30 | logger.debug('Initializing controller'); |
diff --git a/packages/main/src/utils.ts b/packages/main/src/utils/index.ts index 0d469dd..9a57e02 100644 --- a/packages/main/src/utils.ts +++ b/packages/main/src/utils/index.ts | |||
@@ -21,3 +21,5 @@ | |||
21 | import { IDisposer } from 'mobx-state-tree'; | 21 | import { IDisposer } from 'mobx-state-tree'; |
22 | 22 | ||
23 | export type Disposer = IDisposer; | 23 | export type Disposer = IDisposer; |
24 | |||
25 | export { getLogger } from './logging'; | ||
diff --git a/packages/main/src/utils/logging.ts b/packages/main/src/utils/logging.ts new file mode 100644 index 0000000..9f1133f --- /dev/null +++ b/packages/main/src/utils/logging.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import loglevel, { Logger } from 'loglevel'; | ||
22 | import prefix from 'loglevel-plugin-prefix'; | ||
23 | |||
24 | const isDevelopment = import.meta.env.MODE === 'development'; | ||
25 | |||
26 | if (isDevelopment) { | ||
27 | loglevel.enableAll(); | ||
28 | } | ||
29 | |||
30 | prefix.reg(loglevel); | ||
31 | prefix.apply(loglevel, { | ||
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 | |||
47 | export function getLogger(loggerName: string): Logger { | ||
48 | return loglevel.getLogger(`sophie:${loggerName}`); | ||
49 | } | ||
diff --git a/packages/main/vite.config.js b/packages/main/vite.config.js index d6f9373..bdb0ca1 100644 --- a/packages/main/vite.config.js +++ b/packages/main/vite.config.js | |||
@@ -20,12 +20,6 @@ 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 resolve consola to the nodejs version. | ||
26 | consola: 'consola/dist/consola.js', | ||
27 | }, | ||
28 | }, | ||
29 | build: { | 23 | build: { |
30 | target: node, | 24 | target: node, |
31 | lib: { | 25 | lib: { |
@@ -1204,11 +1204,12 @@ __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 | ||
1208 | electron: 16.0.5 | 1207 | electron: 16.0.5 |
1209 | electron-devtools-installer: ^3.2.0 | 1208 | electron-devtools-installer: ^3.2.0 |
1210 | json5: ^2.2.0 | 1209 | json5: ^2.2.0 |
1211 | lodash: ^4.17.21 | 1210 | lodash: ^4.17.21 |
1211 | loglevel: ^1.8.0 | ||
1212 | loglevel-plugin-prefix: ^0.8.4 | ||
1212 | mobx: ^6.3.10 | 1213 | mobx: ^6.3.10 |
1213 | mobx-state-tree: ^5.1.0 | 1214 | mobx-state-tree: ^5.1.0 |
1214 | ms: ^2.1.3 | 1215 | ms: ^2.1.3 |
@@ -2586,13 +2587,6 @@ __metadata: | |||
2586 | languageName: node | 2587 | languageName: node |
2587 | linkType: hard | 2588 | linkType: hard |
2588 | 2589 | ||
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 | |||
2596 | "console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0": | 2590 | "console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0": |
2597 | version: 1.1.0 | 2591 | version: 1.1.0 |
2598 | resolution: "console-control-strings@npm:1.1.0" | 2592 | resolution: "console-control-strings@npm:1.1.0" |
@@ -5308,6 +5302,20 @@ __metadata: | |||
5308 | languageName: node | 5302 | languageName: node |
5309 | linkType: hard | 5303 | linkType: hard |
5310 | 5304 | ||
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 | |||
5311 | "loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": | 5319 | "loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": |
5312 | version: 1.4.0 | 5320 | version: 1.4.0 |
5313 | resolution: "loose-envify@npm:1.4.0" | 5321 | resolution: "loose-envify@npm:1.4.0" |