Skip to content

PlatformLogger

File logging builds on Effect's Logger module: pair a string formatter with Logger.toFile and install it via Logger.layer, whose scope owns the file handle and flushes pending batches on exit.

Writing formatted logs to a file

import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
const FileLogger = Logger.layer([
  Logger.toFile(Logger.formatLogFmt, "./application.log", {
    batchWindow: "250 millis"
  })
])
 
effect function application() {
  try log("Index build started")
  try log("Index build completed")
}
// Run in an effect function:
//   provide NodeFileSystem.layer {
//     provide FileLogger { try application() }
//     const fs = try FileSystem.FileSystem
//     const text = try fs.readFileString("./application.log")
//     try Console.log(text.includes("Index build started") && text.indexOf("Index build started") < text.indexOf("Index build completed"))
//   }
// Output: true

Logger.toFile returns a scoped Effect value; the file opens only when Logger.layer builds, not at module load. Logger.layer replaces the active loggers with its list.

In Effect 4.1.1, toFile batches writes with a 1-second default batchWindow and flushes pending entries when its scope closes. Opening the file fails with PlatformError; subsequent background flush errors are ignored rather than failing individual log calls.

Supplying FileSystem

// main.vibe
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
const LoggerLive = Layer.provide(
  Logger.layer([
    Logger.toFile(Logger.formatLogFmt, "./application.log")
  ]),
  NodeFileSystem.layer
)
 
effect function main() {
  provide LoggerLive {
    try log("Report generated")
  }
  // LoggerLive's scope has closed and its final batch has flushed.
  const fs = try FileSystem.FileSystem
  const text = try fs.readFileString("./application.log")
  try Console.log(text.includes("Report generated"))
  return text
}
 
NodeRuntime.runMain(Effect.provide(main(), NodeFileSystem.layer))
// Output: true

Logger.layer satisfies toFile's Scope requirement, and Layer.provide(..., NodeFileSystem.layer) supplies FileSystem. Reading the file after the provide block exits guarantees the final batch has flushed.

Combining file and console loggers

// main.vibe
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
const LoggerLive = Layer.provide(
  Logger.layer([
    Logger.withConsoleLog(Logger.make(({ message }) => String(message))),
    Logger.toFile(Logger.formatLogFmt, "./application.log", {
      batchWindow: "500 millis"
    })
  ]),
  NodeFileSystem.layer
)
 
effect function main() {
  try log("Worker ready")
}
 
NodeRuntime.runMain(Effect.provide(main(), LoggerLive))
// Output: Worker ready

Pass multiple loggers to Logger.layer, or pass { mergeWithExisting: true } to keep previously installed loggers (Logger.consolePretty() formats timestamped console output). Wrap secrets in Redacted and keep raw customer payloads out of logs.

Coming from Effect

Pass the Effect returned by Logger.toFile(...) directly inside the Logger.layer([...]) array; constructing it does not open the file.


The matching Effect page: effect.website/docs/v4/platform/platformlogger