diff options
Diffstat (limited to 'packages/main/src/utils')
-rw-r--r-- | packages/main/src/utils/logging.ts | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/packages/main/src/utils/logging.ts b/packages/main/src/utils/logging.ts index 5cb5d21..6c4cba2 100644 --- a/packages/main/src/utils/logging.ts +++ b/packages/main/src/utils/logging.ts | |||
@@ -18,28 +18,37 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import chalk, { ChalkInstance } from 'chalk'; | ||
21 | import loglevel, { Logger } from 'loglevel'; | 22 | import loglevel, { Logger } from 'loglevel'; |
22 | import prefix from 'loglevel-plugin-prefix'; | 23 | import prefix from 'loglevel-plugin-prefix'; |
23 | 24 | ||
24 | const isDevelopment = import.meta.env.MODE === 'development'; | 25 | if (import.meta.env.DEV) { |
26 | loglevel.setLevel('debug'); | ||
27 | } else { | ||
28 | loglevel.setLevel('info'); | ||
29 | } | ||
30 | |||
31 | const COLORS: Partial<Record<string, ChalkInstance>> = { | ||
32 | TRACE: chalk.magenta, | ||
33 | DEBUG: chalk.cyan, | ||
34 | INFO: chalk.blue, | ||
35 | WARN: chalk.yellow, | ||
36 | ERROR: chalk.red, | ||
37 | CRITICAL: chalk.red, | ||
38 | }; | ||
25 | 39 | ||
26 | if (isDevelopment) { | 40 | function getColor(level: string): ChalkInstance { |
27 | loglevel.enableAll(); | 41 | return COLORS[level] ?? chalk.gray; |
28 | } | 42 | } |
29 | 43 | ||
30 | prefix.reg(loglevel); | 44 | prefix.reg(loglevel); |
31 | prefix.apply(loglevel, { | 45 | prefix.apply(loglevel, { |
32 | format(level, name, timestamp) { | 46 | format(level, name, timestamp) { |
33 | let shortName = 'global'; | 47 | const levelColor = getColor(level); |
34 | if (name !== undefined) { | 48 | return `${chalk.gray(`[${timestamp}]`)} ${levelColor(level)} ${chalk.green(`${name}:`)}`; |
35 | const nameSegments = name.split(':'); | ||
36 | const lastSegment = nameSegments.pop(); | ||
37 | shortName = [...nameSegments.map((segment) => segment[0]), lastSegment].join(':'); | ||
38 | } | ||
39 | return `[${timestamp}] ${level} (${shortName})`; | ||
40 | }, | 49 | }, |
41 | }); | 50 | }); |
42 | 51 | ||
43 | export function getLogger(loggerName: string): Logger { | 52 | export function getLogger(loggerName: string): Logger { |
44 | return loglevel.getLogger(`sophie:${loggerName}`); | 53 | return loglevel.getLogger(loggerName); |
45 | } | 54 | } |