Logger<unknown, void>A Logger which outputs logs using a structured format serialized as JSON
on a single line and writes them to the console.
Details
For example, console JSON output can render as
{"message":["hello"],"level":"INFO","timestamp":"2025-01-03T14:28:57.508Z",
"annotations":{"key":"value"},"spans":{"label":0},"fiberId":"#1"}.
Example (Logging JSON output to the console)
import { Effect, Logger } from "effect"
// Use the console JSON logger
const jsonProgram = Effect.log("Hello JSON Console").pipe(
Effect.provide(Logger.layer([Logger.consoleJson]))
)
// Perfect for production logging and log aggregation
const productionProgram = Effect.gen(function*() {
yield* Effect.log("Server started", { port: 3000, env: "production" })
yield* Effect.logInfo("Request", {
method: "POST",
url: "/api/users",
body: { name: "Alice" }
})
yield* Effect.logError("Database error", {
error: "Connection timeout",
retryCount: 3
})
}).pipe(
Effect.annotateLogs("service", "user-api"),
Effect.annotateLogs("version", "1.2.3"),
Effect.withLogSpan("request-processing"),
Effect.provide(Logger.layer([Logger.consoleJson]))
)
// Easy to pipe to log aggregation services
const productionSetup = Logger.layer([
Logger.consoleJson, // For stdout JSON logs
Logger.consolePretty() // For local debugging
])
// Ideal for containerized environments (Docker, Kubernetes)
const containerProgram = Effect.log("Container ready", {
containerId: "abc123",
image: "myapp:latest"
}).pipe(
Effect.provide(Logger.layer([Logger.consoleJson]))
)export const const consoleJson: Logger<unknown, void>const consoleJson: {
log: (options: Options<unknown>) => void;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
A Logger which outputs logs using a structured format serialized as JSON
on a single line and writes them to the console.
Details
For example, console JSON output can render as
{"message":["hello"],"level":"INFO","timestamp":"2025-01-03T14:28:57.508Z",
"annotations":{"key":"value"},"spans":{"label":0},"fiberId":"#1"}.
Example (Logging JSON output to the console)
import { Effect, Logger } from "effect"
// Use the console JSON logger
const jsonProgram = Effect.log("Hello JSON Console").pipe(
Effect.provide(Logger.layer([Logger.consoleJson]))
)
// Perfect for production logging and log aggregation
const productionProgram = Effect.gen(function*() {
yield* Effect.log("Server started", { port: 3000, env: "production" })
yield* Effect.logInfo("Request", {
method: "POST",
url: "/api/users",
body: { name: "Alice" }
})
yield* Effect.logError("Database error", {
error: "Connection timeout",
retryCount: 3
})
}).pipe(
Effect.annotateLogs("service", "user-api"),
Effect.annotateLogs("version", "1.2.3"),
Effect.withLogSpan("request-processing"),
Effect.provide(Logger.layer([Logger.consoleJson]))
)
// Easy to pipe to log aggregation services
const productionSetup = Logger.layer([
Logger.consoleJson, // For stdout JSON logs
Logger.consolePretty() // For local debugging
])
// Ideal for containerized environments (Docker, Kubernetes)
const containerProgram = Effect.log("Container ready", {
containerId: "abc123",
image: "myapp:latest"
}).pipe(
Effect.provide(Logger.layer([Logger.consoleJson]))
)
consoleJson: interface Logger<in Message, out Output>A logger that transforms a runtime log event into an output value.
Details
The runtime calls log with the message, level, cause, fiber, and timestamp
for each log event. Use Logger.layer to install one or more loggers for an
effect.
Example (Creating custom loggers)
import { Effect, Logger } from "effect"
// Create a custom logger that accepts unknown messages and returns void
const stringLogger = Logger.make<unknown, void>((options) => {
console.log(`[${options.logLevel}] ${options.message}`)
})
// Create a logger that accepts any message type and returns a formatted string
const formattedLogger = Logger.make<unknown, string>((options) =>
`${options.date.toISOString()} [${options.logLevel}] ${options.message}`
)
// Use the logger in an Effect program
const program = Effect.log("Hello World").pipe(
Effect.provide(Logger.layer([stringLogger]))
)
Logger<unknown, void> = const withConsoleLog: <Message, Output>(
self: Logger<Message, Output>
) => Logger<Message, void>
Returns a new Logger that writes all output of the specified Logger to
the console using console.log.
When to use
Use when a logger's string or object output should be routed to console.log for
development or debugging.
Example (Writing logger output with console.log)
import { Effect, Logger } from "effect"
// Create a custom formatter
const customFormatter = Logger.make((options) =>
`[${options.date.toISOString()}] ${options.logLevel}: ${options.message}`
)
// Route to console
const consoleLogger = Logger.withConsoleLog(customFormatter)
const program = Effect.log("Hello World").pipe(
Effect.provide(Logger.layer([consoleLogger]))
)
withConsoleLog(const formatJson: Logger<unknown, string>const formatJson: {
log: (options: Options<unknown>) => string;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
A Logger which outputs logs using a structured format serialized as JSON
on a single line.
Details
For example, a JSON entry can render as {"message":["hello"],"level":"INFO",
"timestamp":"2025-01-03T14:28:57.508Z","annotations":{"key":"value"},
"spans":{"label":0},"fiberId":"#1"}.
Example (Formatting logs as JSON)
import { Effect, Logger } from "effect"
// Use the JSON format logger
const jsonLoggerProgram = Effect.log("Hello JSON Format").pipe(
Effect.provide(Logger.layer([Logger.formatJson]))
)
// Perfect for log aggregation and processing systems
const productionProgram = Effect.gen(function*() {
yield* Effect.log("Server started", { port: 3000, env: "production" })
yield* Effect.logInfo("Request received", {
method: "GET",
path: "/api/users"
})
yield* Effect.logError("Database error", { error: "Connection timeout" })
}).pipe(
Effect.annotateLogs("service", "api-server"),
Effect.withLogSpan("request-processing"),
Effect.provide(Logger.layer([Logger.formatJson]))
)
// Adapt the JSON string before giving it to an output sink
const envelopedJsonLogger = Logger.map(
Logger.formatJson,
(jsonString) => `{"service":"api-server","entry":${jsonString}}`
)
const envelopedConsoleLogger = Logger.withConsoleLog(envelopedJsonLogger)
formatJson)