aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 19:56:09 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 19:56:09 +0100
commite2a611620027063d3c8a60c257004c847bf636da (patch)
tree84f9f9c1d6d753d7ce15c2b7e441b6bb06869c3a /packages/main/src
parentfeat: Add consola logging (diff)
downloadsophie-e2a611620027063d3c8a60c257004c847bf636da.tar.gz
sophie-e2a611620027063d3c8a60c257004c847bf636da.tar.zst
sophie-e2a611620027063d3c8a60c257004c847bf636da.zip
refactor: Simpler logging with loglevel
Diffstat (limited to 'packages/main/src')
-rw-r--r--packages/main/src/controllers/config.ts7
-rw-r--r--packages/main/src/controllers/nativeTheme.ts5
-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.ts49
4 files changed, 56 insertions, 7 deletions
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
21import consola from 'consola';
22import { debounce } from 'lodash'; 21import { debounce } from 'lodash';
23import ms from 'ms'; 22import ms from 'ms';
24import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; 23import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree';
25 24
26import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; 25import type { ConfigPersistenceService } from '../services/ConfigPersistenceService';
27import type { Config, ConfigSnapshotOut } from '../stores/Config'; 26import type { Config, ConfigSnapshotOut } from '../stores/Config';
28import { Disposer } from '../utils'; 27import { Disposer, getLogger } from '../utils';
29 28
30const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); 29const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s');
31 30
32const logger = consola.withTag('sophie:controller:config'); 31const logger = getLogger('controller:config');
33 32
34export async function initConfig( 33export 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
21import consola from 'consola';
22import { nativeTheme } from 'electron'; 21import { nativeTheme } from 'electron';
23import { autorun } from 'mobx'; 22import { autorun } from 'mobx';
24 23
25import type { MainStore } from '../stores/MainStore'; 24import type { MainStore } from '../stores/MainStore';
26import { Disposer } from '../utils'; 25import { Disposer, getLogger } from '../utils';
27 26
28const logger = consola.withTag('sophie:controller:nativeTheme'); 27const logger = getLogger('controller:nativeTheme');
29 28
30export function initNativeTheme(store: MainStore): Disposer { 29export 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 @@
21import { IDisposer } from 'mobx-state-tree'; 21import { IDisposer } from 'mobx-state-tree';
22 22
23export type Disposer = IDisposer; 23export type Disposer = IDisposer;
24
25export { 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
21import loglevel, { Logger } from 'loglevel';
22import prefix from 'loglevel-plugin-prefix';
23
24const isDevelopment = import.meta.env.MODE === 'development';
25
26if (isDevelopment) {
27 loglevel.enableAll();
28}
29
30prefix.reg(loglevel);
31prefix.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
47export function getLogger(loggerName: string): Logger {
48 return loglevel.getLogger(`sophie:${loggerName}`);
49}