|
| 1 | +type LogLevel = "info" | "warn" | "error"; |
| 2 | +type LogFunction = (...args: unknown[]) => void; |
| 3 | + |
1 | 4 | /** ログ出力用の関数を生成する。ブラウザ専用。 */ |
2 | 5 | // TODO: window.backendをDIできるようにする |
3 | | -export function createLogger(scope: string) { |
4 | | - const createInner = |
5 | | - ( |
6 | | - method: "logInfo" | "logWarn" | "logError", |
7 | | - fallbackMethod: "info" | "warn" | "error", |
8 | | - ) => |
9 | | - (...args: unknown[]) => { |
10 | | - if (window.backend == undefined) { |
11 | | - // eslint-disable-next-line no-console |
12 | | - console[fallbackMethod](...args); |
| 6 | +export function createLogger(scope: string): Record<LogLevel, LogFunction> { |
| 7 | + return { |
| 8 | + info: createLogFunction("info"), |
| 9 | + warn: createLogFunction("warn"), |
| 10 | + error: createLogFunction("error"), |
| 11 | + }; |
| 12 | + |
| 13 | + function createLogFunction(logType: LogLevel): LogFunction { |
| 14 | + return (...args: unknown[]) => { |
| 15 | + if (window.backend != undefined) { |
| 16 | + const method = ( |
| 17 | + { |
| 18 | + info: "logInfo", |
| 19 | + warn: "logWarn", |
| 20 | + error: "logError", |
| 21 | + } as const |
| 22 | + )[logType]; |
| 23 | + window.backend[method](`[${scope}]`, ...args); |
13 | 24 | return; |
14 | 25 | } |
15 | | - window.backend[method](`[${scope}]`, ...args); |
| 26 | + |
| 27 | + // eslint-disable-next-line no-console |
| 28 | + console[logType](...args); |
16 | 29 | }; |
17 | | - return { |
18 | | - info: createInner("logInfo", "info"), |
19 | | - warn: createInner("logWarn", "warn"), |
20 | | - error: createInner("logError", "error"), |
21 | | - }; |
| 30 | + } |
22 | 31 | } |
0 commit comments