{
  "schemaVersion": 1,
  "citationId": "ecn:v4/platform/platformlogger@bf46254#tofile",
  "citeUrl": "/cite/09291cde999f6b4b.json",
  "digest": "09291cde999f6b4b",
  "slug": "v4/platform/platformlogger",
  "version": "v4",
  "title": "PlatformLogger",
  "anchor": "tofile",
  "headingPath": [
    "toFile"
  ],
  "pageUrl": "/docs/v4/platform/platformlogger/",
  "deepLink": "/docs/v4/platform/platformlogger/#tofile",
  "officialUrl": "https://effect.website/docs/v4/platform/platformlogger",
  "upstreamPath": "v4/platform/platformlogger.mdx",
  "upstreamCommit": "bf4625446a02894046b6937a317dde2cde115fe7",
  "upstreamRawUrl": "https://raw.githubusercontent.com/Effect-TS/website/bf4625446a02894046b6937a317dde2cde115fe7/apps/web/src/content/docs/v4/platform/platformlogger.mdx",
  "status": "published",
  "stale": false,
  "contentHash": "b0cc7c07ddc0f618",
  "chunkText": "基于已有的字符串 logger 创建一个新的 logger，并把它的输出写入指定文件。\n\n如果在调用 toFile 时传入一个 batchWindow 时长，日志会先在该时间窗口内批量累积，然后再写入。当你的应用产生大量日志条目时，这可以降低开销。若不设置 batchWindow，日志会在到达时立即写入。\n\n请注意，toFile 返回一个 Effect，如果文件无法打开或写入，它可能以 PlatformError 失败。如果你需要对文件 I/O 问题作出反应，请务必处理这种可能性。\n\n示例（将日志写入文件）\n\n这个 logger 需要一个 FileSystem 实现来打开并写入文件。在 Node.js 上，你可以使用 NodeFileSystem.layer。\n\n// Create a string-based logger (formatLogFmt in this case)\nconst myStringLogger = Logger.formatLogFmt\n\n// Apply toFile to write logs to \"/tmp/log.txt\"\nconst fileLogger = myStringLogger.pipe(Logger.toFile(\"/tmp/log.txt\"))\n\n// Replace the default logger, providing NodeFileSystem\n// to access the file system\nconst LoggerLive = Logger.layer([fileLogger]).pipe(\n Layer.provide(NodeFileS …\n\n在下面的示例中，日志会同时写入控制台和文件。控制台使用 pretty logger，而文件使用 logfmt 格式。\n\n示例（同时将日志写入文件和控制台）\n\nconst fileLogger = Logger.formatLogFmt.pipe(Logger.toFile(\"/tmp/log.txt\"))\n\n// Combine the pretty logger for console output with the file logger\nconst LoggerLive = Logger.layer([Logger.consolePretty(), fileLogger]).pipe(\n Layer.provide(NodeFileSystem.layer),\n)\n\nconst program = Effect.log(\"Hello\")\n\n// Run the program, writing logs to both the console (pretty format)\n// and \"/tmp/log …",
  "generatedAt": "2026-09-18T14:48:33.525Z"
}
