Skip to content
Myr

memory.ts

This file

Path

packages/telemetry/src/logger/memory.ts

57 lines · 1.6 KiB · TypeScript

Last changed by

Merge observability into mainoscabrielbefore adoption1985111

History of this file

Take it

Merge observability into mainoscabrielbefore adoption1985111

lines 1–57why, one step back
1// A console in memory, for the logger to write to in a test: each line kept
2// as the logger handed it over, by the method it was written through.3import { Console, Layer } from "effect";4import { constVoid } from "effect/Function";56export type Method = "debug" | "error" | "info" | "log" | "trace" | "warn";78export interface Written {9 readonly method: Method;10 readonly line: unknown;11}1213export interface RecordingConsole {14 readonly layer: Layer.Layer<never>;15 readonly written: () => readonly Written[];16 /** What reached `console.error`, where Issues hears it. */17 readonly errors: () => readonly Written["line"][];18}1920export const recordingConsole = (): RecordingConsole => {21 const written: Written[] = [];2223 const write =24 (method: Method) =>25 (...lines: readonly Written["line"][]) => {26 written.push(...lines.map((line) => ({ line, method })));27 };2829 const console: Console.Console = {30 assert: constVoid,31 clear: constVoid,32 count: constVoid,33 countReset: constVoid,34 debug: write("debug"),35 dir: constVoid,36 dirxml: constVoid,37 error: write("error"),38 group: constVoid,39 groupCollapsed: constVoid,40 groupEnd: constVoid,41 info: write("info"),42 log: write("log"),43 table: constVoid,44 time: constVoid,45 timeEnd: constVoid,46 timeLog: constVoid,47 trace: write("trace"),48 warn: write("warn"),49 };5051 return {52 errors: () =>53 written.flatMap(({ line, method }) => (method === "error" ? [line] : [])),54 layer: Layer.succeed(Console.Console, console),55 written: () => [...written],56 };57};