diff options
author | 2022-05-03 19:47:39 +0200 | |
---|---|---|
committer | 2022-05-16 00:55:02 +0200 | |
commit | d2aa484b600dc0d122bb994b9bb29504ffe4cf12 (patch) | |
tree | 1d3e38fdb22f7afe02f1ebcf4b07734761970985 /packages/main/src/utils/getLogger.ts | |
parent | refactor: config file saving and debugging (diff) | |
download | sophie-d2aa484b600dc0d122bb994b9bb29504ffe4cf12.tar.gz sophie-d2aa484b600dc0d122bb994b9bb29504ffe4cf12.tar.zst sophie-d2aa484b600dc0d122bb994b9bb29504ffe4cf12.zip |
build: integration testing support
Run integration tests in an electron environment for the main process.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/utils/getLogger.ts')
-rw-r--r-- | packages/main/src/utils/getLogger.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/main/src/utils/getLogger.ts b/packages/main/src/utils/getLogger.ts new file mode 100644 index 0000000..7ed38de --- /dev/null +++ b/packages/main/src/utils/getLogger.ts | |||
@@ -0,0 +1,62 @@ | |||
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 | // eslint-disable-next-line unicorn/import-style -- Import the type `ChalkInstance` separately. | ||
22 | import chalk, { ChalkInstance } from 'chalk'; | ||
23 | import loglevel from 'loglevel'; | ||
24 | import prefix from 'loglevel-plugin-prefix'; | ||
25 | |||
26 | switch (import.meta.env.MODE) { | ||
27 | case 'development': | ||
28 | loglevel.setLevel('debug', false); | ||
29 | break; | ||
30 | case 'test': | ||
31 | loglevel.setLevel('silent', false); | ||
32 | break; | ||
33 | default: | ||
34 | loglevel.setLevel('info', false); | ||
35 | break; | ||
36 | } | ||
37 | |||
38 | const COLORS: Map<string, ChalkInstance> = new Map([ | ||
39 | ['TRACE', chalk.magenta], | ||
40 | ['DEBUG', chalk.cyan], | ||
41 | ['INFO', chalk.blue], | ||
42 | ['WARN', chalk.yellow], | ||
43 | ['ERROR', chalk.red], | ||
44 | ['CRITICAL', chalk.red], | ||
45 | ]); | ||
46 | |||
47 | prefix.reg(loglevel); | ||
48 | prefix.apply(loglevel, { | ||
49 | format(level, name, timestamp) { | ||
50 | const levelColor = COLORS.get(level) ?? chalk.gray; | ||
51 | const timeStr = timestamp.toString(); | ||
52 | const nameStr = | ||
53 | typeof name === 'undefined' | ||
54 | ? levelColor(':') | ||
55 | : ` ${chalk.green(`${name}:`)}`; | ||
56 | return `${chalk.gray(`[${timeStr}]`)} ${levelColor(level)}${nameStr}`; | ||
57 | }, | ||
58 | }); | ||
59 | |||
60 | export default function getLogger(loggerName: string): loglevel.Logger { | ||
61 | return loglevel.getLogger(loggerName); | ||
62 | } | ||