Repository navigation
Expand file tree
/
Copy patheffect-logger.ts
More file actions
60 lines (53 loc) · 1.78 KB
/
Copy patheffect-logger.ts
File metadata and controls
60 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Cause, Layer, Logger } from "effect"
import { Option } from "effect"
import type { PushNotification } from "@magnitudedev/client-common"
import { TracingLayer } from "@magnitudedev/client-common"
const stringifyMessagePart = (value: unknown): string => {
if (typeof value === "string") return value
if (value instanceof Error) return value.message
try {
return JSON.stringify(value)
} catch {
return String(value)
}
}
const formatToastMessage = (message: unknown, cause: Cause.Cause<unknown>): string => {
const text = (Array.isArray(message) ? message : [message])
.map(stringifyMessagePart)
.filter(Boolean)
.join(" ")
if (text) return text
return Cause.isEmpty(cause) ? "An unexpected error occurred" : Cause.pretty(cause)
}
export interface MakeCliEffectLoggingLayerOptions {
readonly debug: boolean
readonly publishNotification?: (notification: PushNotification) => void
}
/**
* CLI Effect logging fan-out. OTLP remains the structured log destination;
* errors are also surfaced in the TUI, as are warnings in debug mode.
*/
export const makeCliEffectLoggingLayer = (
options: MakeCliEffectLoggingLayerOptions,
): Layer.Layer<never, never, never> => {
const toastLogger = Logger.make(({ logLevel, message, cause }) => {
const tone = logLevel.label === "FATAL" || logLevel.label === "ERROR"
? "error"
: logLevel.label === "WARN" && options.debug
? "warning"
: null
if (tone) {
options.publishNotification?.({
message: formatToastMessage(message, cause),
priority: tone,
action: Option.none(),
dismissAfterMilliseconds: 5_000,
})
}
})
return Layer.mergeAll(
TracingLayer,
Logger.replace(Logger.defaultLogger, Logger.none),
Logger.add(toastLogger),
)
}