mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract app logging runtime adapters
This commit is contained in:
28
src/core/services/app-logging-runtime-service.test.ts
Normal file
28
src/core/services/app-logging-runtime-service.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createAppLoggingRuntimeService } from "./app-logging-runtime-service";
|
||||
|
||||
test("createAppLoggingRuntimeService routes logs and formats config warnings", () => {
|
||||
const lines: string[] = [];
|
||||
const logger = {
|
||||
log: (line: string) => lines.push(`log:${line}`),
|
||||
warn: (line: string) => lines.push(`warn:${line}`),
|
||||
error: (line: string) => lines.push(`error:${line}`),
|
||||
};
|
||||
|
||||
const runtime = createAppLoggingRuntimeService(logger);
|
||||
runtime.logInfo("hello");
|
||||
runtime.logWarning("careful");
|
||||
runtime.logNoRunningInstance();
|
||||
runtime.logConfigWarning({
|
||||
path: "x.y",
|
||||
value: "bad",
|
||||
fallback: "good",
|
||||
message: "invalid",
|
||||
});
|
||||
|
||||
assert.equal(lines[0], "log:hello");
|
||||
assert.equal(lines[1], "warn:careful");
|
||||
assert.equal(lines[2], "error:No running instance. Use --start to launch the app.");
|
||||
assert.match(lines[3], /^warn:\[config\] x\.y: invalid /);
|
||||
});
|
||||
28
src/core/services/app-logging-runtime-service.ts
Normal file
28
src/core/services/app-logging-runtime-service.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ConfigValidationWarning } from "../../types";
|
||||
import { logConfigWarningRuntimeService } from "./config-warning-runtime-service";
|
||||
|
||||
export interface AppLoggingRuntime {
|
||||
logInfo: (message: string) => void;
|
||||
logWarning: (message: string) => void;
|
||||
logNoRunningInstance: () => void;
|
||||
logConfigWarning: (warning: ConfigValidationWarning) => void;
|
||||
}
|
||||
|
||||
export function createAppLoggingRuntimeService(
|
||||
logger: Pick<Console, "log" | "warn" | "error"> = console,
|
||||
): AppLoggingRuntime {
|
||||
return {
|
||||
logInfo: (message) => {
|
||||
logger.log(message);
|
||||
},
|
||||
logWarning: (message) => {
|
||||
logger.warn(message);
|
||||
},
|
||||
logNoRunningInstance: () => {
|
||||
logger.error("No running instance. Use --start to launch the app.");
|
||||
},
|
||||
logConfigWarning: (warning) => {
|
||||
logConfigWarningRuntimeService(warning, (line) => logger.warn(line));
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user