logger.test.ts
This file
Path
packages/telemetry/test/logger.test.ts
162 lines · 4.6 KiB · TypeScript
Last changed by
Merge observability/visibility-logs into mainoscabrielbefore adoption2858640
History of this fileTake it
Merge observability into mainoscabrielbefore adoption1985111
lines 1–3why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Cause, Effect, Layer, Logger, Redacted } from "effect";3Merge observability/visibility-logs into mainoscabrielbefore adoption2858640
line 4why, one step back4import type { DefectKeeping } from "../src/defects.ts";Merge observability into mainoscabrielbefore adoption1985111
lines 5–9why, one step back5import { jsonLogger, scrubberOf } from "../src/logger.ts";6import { recordingConsole } from "../src/logger/memory.ts";78const logged = <A, E>(9 body: Effect.Effect<A, E>,Merge observability/visibility-logs into mainoscabrielbefore adoption2858640
lines 10–11why, one step back10 secrets: readonly Redacted.Redacted[] = [],11 defects: DefectKeeping = "whole"Merge observability into mainoscabrielbefore adoption1985111
lines 12–18why, one step back12) =>13 Effect.gen(function* () {14 const console = recordingConsole();1516 yield* body.pipe(17 Effect.provide(18 Layer.merge(Merge observability/visibility-logs into mainoscabrielbefore adoption2858640
line 19why, one step back19 Logger.layer([jsonLogger(scrubberOf(secrets), defects)]),Merge observability into mainoscabrielbefore adoption1985111
lines 20–45why, one step back20 console.layer21 )22 ),23 Effect.exit24 );2526 return console.written();27 });2829describe(jsonLogger, () => {30 it.effect(31 "writes one object a line, each annotation a field of its own",32 () =>33 Effect.gen(function* () {34 const written = yield* logged(35 Effect.logInfo("a push reached custody").pipe(36 Effect.annotateLogs({37 project: "alice/datekit",38 requestId: "push-1",39 })40 )41 );4243 expect(written).toStrictEqual([44 {45 line: {Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 46why, one step back46 level: "info",Merge observability into mainoscabrielbefore adoption1985111
lines 47–57why, one step back47 message: "a push reached custody",48 project: "alice/datekit",49 requestId: "push-1",50 },51 method: "info",52 },53 ]);54 })55 );5657 it.effect(Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 58why, one step back58 "writes each level through its own console method and names it as Workers Logs does, so Issues and a query of level = 'error' hear what is logged at Error",Merge observability into mainoscabrielbefore adoption1985111
lines 59–64why, one step back59 () =>60 Effect.gen(function* () {61 const written = yield* logged(62 Effect.all([63 Effect.logWarning("held"),64 Effect.logError("broke", Cause.die(new Error("the store is gone"))),Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 65why, one step back65 Effect.logFatal("gone"),Merge observability into mainoscabrielbefore adoption1985111
lines 66–68why, one step back66 ])67 );68Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
lines 69–72why, one step back69 expect(written).toMatchObject([70 { line: { level: "warn" }, method: "warn" },71 { line: { level: "error" }, method: "error" },72 { line: { level: "error" }, method: "error" },Merge observability into mainoscabrielbefore adoption1985111
lines 73–74why, one step back73 ]);74 expect(written[1]?.line).toMatchObject({Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 75why, one step back75 level: "error",Merge observability into mainoscabrielbefore adoption1985111
lines 76–95why, one step back76 message: "broke",77 });78 expect(written[1]?.line).toHaveProperty(79 "cause",80 expect.stringMatching(/^Error: the store is gone\n\s+at /u)81 );82 })83 );8485 it.effect(86 "lets no field a level, a message or a cause names give way to an annotation",87 () =>88 Effect.gen(function* () {89 const written = yield* logged(90 Effect.logInfo("kept").pipe(91 Effect.annotateLogs({ level: "ERROR", message: "forged" })92 )93 );9495 expect(written[0]?.line).toStrictEqual({Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 96why, one step back96 level: "info",Merge observability into mainoscabrielbefore adoption1985111
lines 97–127why, one step back97 message: "kept",98 });99 })100 );101102 it.effect(103 "takes every secret out of a message, an annotation and a cause, longest first",104 () =>105 Effect.gen(function* () {106 const short = "s3cret-0123456789abcdef0123456789";107 const long = `${short}-and-more`;108109 const written = yield* logged(110 Effect.logError(111 `sent ${long} and ${short}`,112 Cause.die(new Error(`token ${short} refused`))113 ).pipe(Effect.annotateLogs({ reason: `bearer ${short}` })),114 [Redacted.make(short), Redacted.make(long), Redacted.make("")]115 );116117 const text = JSON.stringify(written);118119 expect(text).not.toContain(short);120 expect(written[0]?.line).toMatchObject({121 message: "sent <redacted> and <redacted>",122 reason: "bearer <redacted>",123 });124 expect(written[0]?.line).toHaveProperty(125 "cause",126 expect.stringContaining("token <redacted> refused")127 );Merge observability/visibility-logs into mainoscabrielbefore adoption2858640
lines 128–159why, one step back128 })129 );130131 it.effect(132 "keeps a defect by its name and frames alone on every line where defects are kept so, a warning that carries one included",133 () =>134 Effect.gen(function* () {135 const repo = "alice--datekit--private";136137 const written = yield* logged(138 Effect.logWarning(139 "a visibility effect did not settle",140 Cause.die(new Error(`the layer repo ${repo} lost a blob`))141 ),142 [],143 "name"144 );145146 expect(written).toMatchObject([147 {148 line: {149 level: "warn",150 message: "a visibility effect did not settle",151 },152 method: "warn",153 },154 ]);155 expect(written[0]?.line).toHaveProperty(156 "cause",157 expect.stringMatching(/^Error: <withheld>\n\s+at /u)158 );159 expect(JSON.stringify(written)).not.toContain(repo);Merge observability into mainoscabrielbefore adoption1985111
lines 160–162why, one step back160 })161 );162});