aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/utils/getLogger.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/utils/getLogger.ts')
-rw-r--r--packages/main/src/utils/getLogger.ts60
1 files changed, 60 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..b7bbadf
--- /dev/null
+++ b/packages/main/src/utils/getLogger.ts
@@ -0,0 +1,60 @@
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.
22import chalk, { ChalkInstance } from 'chalk';
23import loglevel from 'loglevel';
24import prefix from 'loglevel-plugin-prefix';
25
26switch (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
38const 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
47prefix.reg(loglevel);
48prefix.apply(loglevel, {
49 format(level, name, timestamp) {
50 const levelColor = COLORS.get(level) ?? chalk.gray;
51 const timeStr = timestamp.toString();
52 const nameStr =
53 name === undefined ? levelColor(':') : ` ${chalk.green(`${name}:`)}`;
54 return `${chalk.gray(`[${timeStr}]`)} ${levelColor(level)}${nameStr}`;
55 },
56});
57
58export default function getLogger(loggerName: string): loglevel.Logger {
59 return loglevel.getLogger(loggerName);
60}