-
-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy path10_logging.ts
More file actions
66 lines (57 loc) · 2.41 KB
/
Copy path10_logging.ts
File metadata and controls
66 lines (57 loc) · 2.41 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
61
62
63
64
65
66
/**
* @title Customizing logging
*
* Configure loggers & log-level filtering for production applications.
*/
import { NodeFileSystem } from "@effect/platform-node"
import { Config, Effect, Layer, Logger, References } from "effect"
// Build a logger layer that emits one JSON line per log entry.
export const JsonLoggerLayer = Logger.layer([Logger.consoleJson])
// Raise the minimum level to "Warn" to skip debug/info logs.
export const WarnAndAbove = Layer.succeed(References.MinimumLogLevel, "Warn")
// There is a built-in logger for writing to a file
export const FileLoggerLayer = Logger.layer([
Logger.toFile(Logger.formatSimple, "app.log")
]).pipe(
Layer.provide(NodeFileSystem.layer)
)
// Define a custom logger for app-specific formatting and routing.
export const appLogger = Effect.gen(function*() {
// Here you could initialize a connection to an external logging service, set
// up log file rotation, etc.
yield* Effect.logDebug("initializing app logger")
return yield* Logger.batched(Logger.formatStructured, {
window: "1 second",
flush: Effect.fn(function*(batch) {
// In a real implementation, this is where you would send the batch of log entries to an external logging service or write them to a file.
console.log(`Flushing ${batch.length} log entries`)
})
})
})
export const AppLoggerLayer = Logger.layer([appLogger]).pipe(
Layer.provideMerge(WarnAndAbove) // Start with "Warn" level for the app logger.
)
// Create a logger layer that uses the default logger for development, and the
// custom logger for production
export const LoggerLayer = Layer.unwrap(Effect.gen(function*() {
const env = yield* Config.String("NODE_ENV").pipe(Config.withDefault("development"))
if (env === "production") {
return AppLoggerLayer
}
return Logger.layer([Logger.defaultLogger])
}))
// Example effect that logs at various levels during a checkout flow.
export const logCheckoutFlow = Effect.gen(function*() {
yield* Effect.logDebug("loading checkout state")
yield* Effect.logInfo("validating cart")
yield* Effect.logWarning("inventory is low for one line item")
yield* Effect.logError("payment provider timeout")
}).pipe(
// Attach structured metadata to all log lines emitted by this effect.
Effect.annotateLogs({
service: "checkout-api",
route: "POST /checkout"
}),
// Add a duration span so each log line includes checkout=<N>ms metadata.
Effect.withLogSpan("checkout")
)