(
path: string,
options?:
| {
readonly flag?: FileSystem.OpenFlag | undefined
readonly mode?: number | undefined
readonly batchWindow?: Duration.Input | undefined
}
| undefined
): <Message>(
self: Logger<Message, string>
) => Effect.Effect<
Logger<Message, void>,
PlatformError,
Scope.Scope | FileSystem.FileSystem
>
<Message>(
self: Logger<Message, string>,
path: string,
options?:
| {
readonly flag?: FileSystem.OpenFlag | undefined
readonly mode?: number | undefined
readonly batchWindow?: Duration.Input | undefined
}
| undefined
): Effect.Effect<
Logger<Message, void>,
PlatformError,
Scope.Scope | FileSystem.FileSystem
>Creates a scoped logger that writes string logger output to a file.
Details
The returned effect requires FileSystem and Scope. The file logger batches
string output, writes each batch to the specified path, and flushes remaining
entries when the scope closes.
Example (Writing JSON logs to a file)
import { Effect, Layer, Logger } from "effect"
import { NodeFileSystem, NodeRuntime } from "@effect/platform-node"
const fileLogger = Logger.formatJson.pipe(
Logger.toFile("/tmp/log.txt")
)
const LoggerLive = Logger.layer([fileLogger]).pipe(
Layer.provide(NodeFileSystem.layer)
)
Effect.log("a").pipe(
Effect.andThen(Effect.log("b")),
Effect.andThen(Effect.log("c")),
Effect.provide(LoggerLive),
NodeRuntime.runMain
)Example (Writing logs to files)
import { Duration, Effect, Logger } from "effect"
import { NodeFileSystem } from "@effect/platform-node"
// Basic file logging. The scope keeps the file open while logs are emitted
// and flushes pending entries when it closes.
const basicFileLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatJson.pipe(
Logger.toFile("/tmp/app.log")
)
yield* Effect.log("Application started").pipe(
Effect.provide(Logger.layer([fileLogger]))
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)
// File logger with custom batch window
const batchedFileLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatLogFmt.pipe(
Logger.toFile("/var/log/myapp.log", {
flag: "a",
batchWindow: Duration.seconds(5)
})
)
yield* Effect.all([
Effect.log("Event 1"),
Effect.log("Event 2"),
Effect.log("Event 3")
]).pipe(
Effect.provide(Logger.layer([fileLogger]))
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)
// Multiple loggers: console + file
const multiLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatJson.pipe(
Logger.toFile("/tmp/production.log")
)
const loggerLive = Logger.layer([
Logger.consolePretty(),
fileLogger
])
yield* Effect.log("Production event").pipe(
Effect.provide(loggerLive)
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)export const const toFile: ((
path: string,
options?:
| {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
) => <Message>(
self: Logger<Message, string>
) => Effect.Effect<
Logger<Message, void>,
PlatformError,
Scope.Scope | FileSystem.FileSystem
>) &
(<Message>(
self: Logger<Message, string>,
path: string,
options?:
| {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
) => Effect.Effect<
Logger<Message, void>,
PlatformError,
Scope.Scope | FileSystem.FileSystem
>)
Creates a scoped logger that writes string logger output to a file.
Details
The returned effect requires FileSystem and Scope. The file logger batches
string output, writes each batch to the specified path, and flushes remaining
entries when the scope closes.
Example (Writing JSON logs to a file)
import { Effect, Layer, Logger } from "effect"
import { NodeFileSystem, NodeRuntime } from "@effect/platform-node"
const fileLogger = Logger.formatJson.pipe(
Logger.toFile("/tmp/log.txt")
)
const LoggerLive = Logger.layer([fileLogger]).pipe(
Layer.provide(NodeFileSystem.layer)
)
Effect.log("a").pipe(
Effect.andThen(Effect.log("b")),
Effect.andThen(Effect.log("c")),
Effect.provide(LoggerLive),
NodeRuntime.runMain
)
Example (Writing logs to files)
import { Duration, Effect, Logger } from "effect"
import { NodeFileSystem } from "@effect/platform-node"
// Basic file logging. The scope keeps the file open while logs are emitted
// and flushes pending entries when it closes.
const basicFileLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatJson.pipe(
Logger.toFile("/tmp/app.log")
)
yield* Effect.log("Application started").pipe(
Effect.provide(Logger.layer([fileLogger]))
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)
// File logger with custom batch window
const batchedFileLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatLogFmt.pipe(
Logger.toFile("/var/log/myapp.log", {
flag: "a",
batchWindow: Duration.seconds(5)
})
)
yield* Effect.all([
Effect.log("Event 1"),
Effect.log("Event 2"),
Effect.log("Event 3")
]).pipe(
Effect.provide(Logger.layer([fileLogger]))
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)
// Multiple loggers: console + file
const multiLogger = Effect.scoped(
Effect.gen(function*() {
const fileLogger = yield* Logger.formatJson.pipe(
Logger.toFile("/tmp/production.log")
)
const loggerLive = Logger.layer([
Logger.consolePretty(),
fileLogger
])
yield* Effect.log("Production event").pipe(
Effect.provide(loggerLive)
)
})
).pipe(
Effect.provide(NodeFileSystem.layer)
)
toFile = import dualdual<
(
path: stringpath: string,
options: | {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
options?: {
readonly flag?: FileSystem.OpenFlag | undefinedflag?: import FileSystemFileSystem.type FileSystem.OpenFlag = /*unresolved*/ anyOpenFlag | undefined
readonly mode?: number | undefinedmode?: number | undefined
readonly batchWindow?: Duration.Input | undefinedbatchWindow?: import DurationDuration.type Duration.Input = /*unresolved*/ anyInput | undefined
} | undefined
) => <function (type parameter) Message in <Message>(self: Logger<Message, string>): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>Message>(
self: Logger<Message, string>(parameter) self: {
log: (options: Options<Message>) => 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; <…;
}
self: 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<function (type parameter) Message in <Message>(self: Logger<Message, string>): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>Message, string>
) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<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<function (type parameter) Message in <Message>(self: Logger<Message, string>): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>Message, void>, class PlatformErrorclass PlatformError {
message: string;
name: string;
stack: string;
cause: unknown;
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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
Tagged error used by platform APIs to report either invalid arguments or
system-level failures.
When to use
Use as the shared error type for platform APIs that expose invalid arguments
and host or operating-system failures through a single Effect error
channel.
Details
The reason field contains the underlying BadArgument or SystemError.
When that reason has a cause, the cause is preserved on the wrapper.
PlatformError, import ScopeScope.Scope | import FileSystemFileSystem.type FileSystem.FileSystem = /*unresolved*/ anyFileSystem>,
<function (type parameter) Message in <Message>(self: Logger<Message, string>, path: string, options?: {
readonly flag?: FileSystem.OpenFlag | undefined;
readonly mode?: number | undefined;
readonly batchWindow?: Duration.Input | undefined;
} | undefined): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>
Message>(
self: Logger<Message, string>(parameter) self: {
log: (options: Options<Message>) => 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; <…;
}
self: 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<function (type parameter) Message in <Message>(self: Logger<Message, string>, path: string, options?: {
readonly flag?: FileSystem.OpenFlag | undefined;
readonly mode?: number | undefined;
readonly batchWindow?: Duration.Input | undefined;
} | undefined): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>
Message, string>,
path: stringpath: string,
options: | {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
options?: {
readonly flag?: FileSystem.OpenFlag | undefinedflag?: import FileSystemFileSystem.type FileSystem.OpenFlag = /*unresolved*/ anyOpenFlag | undefined
readonly mode?: number | undefinedmode?: number | undefined
readonly batchWindow?: Duration.Input | undefinedbatchWindow?: import DurationDuration.type Duration.Input = /*unresolved*/ anyInput | undefined
} | undefined
) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<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<function (type parameter) Message in <Message>(self: Logger<Message, string>, path: string, options?: {
readonly flag?: FileSystem.OpenFlag | undefined;
readonly mode?: number | undefined;
readonly batchWindow?: Duration.Input | undefined;
} | undefined): Effect.Effect<Logger<Message, void>, PlatformError, Scope.Scope | FileSystem.FileSystem>
Message, void>, class PlatformErrorclass PlatformError {
message: string;
name: string;
stack: string;
cause: unknown;
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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
Tagged error used by platform APIs to report either invalid arguments or
system-level failures.
When to use
Use as the shared error type for platform APIs that expose invalid arguments
and host or operating-system failures through a single Effect error
channel.
Details
The reason field contains the underlying BadArgument or SystemError.
When that reason has a cause, the cause is preserved on the wrapper.
PlatformError, import ScopeScope.Scope | import FileSystemFileSystem.type FileSystem.FileSystem = /*unresolved*/ anyFileSystem>
>(
(args: anyargs) => const isLogger: (
u: unknown
) => u is Logger<unknown, unknown>
Returns true if the specified value is a Logger, otherwise returns false.
Example (Checking logger values)
import { Logger } from "effect"
const myLogger = Logger.make((options) => {
console.log(options.message)
})
console.log(Logger.isLogger(myLogger)) // true
console.log(Logger.isLogger("not a logger")) // false
console.log(Logger.isLogger({ log: () => {} })) // false
isLogger(args: anyargs[0]),
(self: Logger<Message, string>(parameter) self: {
log: (options: Options<Message>) => 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; <…;
}
self, path: anypath, options: | {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
options) =>
import effecteffect.const gen: <
Self,
Eff extends Effect.Effect<any, any, any>,
AEff
>(
...args:
| [
options: { readonly self: Self },
body: (
this: Self
) => Generator<Eff, AEff, never>
]
| [body: () => Generator<Eff, AEff, never>]
) => Effect.Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect.Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect.Effect<infer _A, infer _E, infer R>
]
? R
: never
>
gen(function*() {
const const fs: FileSystem.FileSystemconst fs: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
}
fs = yield* import FileSystemFileSystem.FileSystem
const const logFile: FileSystem.Fileconst logFile: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
logFile = yield* const fs: FileSystem.FileSystemconst fs: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
}
fs.open(path: anypath, { flag: stringflag: "a+", ...options: | {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
options })
const const encoder: TextEncoderencoder = new var TextEncoder: new () => TextEncoderThe TextEncoder interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
TextEncoder()
return yield* const batched: (<Output>(options: {
readonly window: Duration.Input
readonly flush: (
messages: Array<NoInfer<Output>>
) => Effect.Effect<void>
}) => <Message>(
self: Logger<Message, Output>
) => Effect.Effect<
Logger<Message, void>,
never,
Scope.Scope
>) &
(<Message, Output>(
self: Logger<Message, Output>,
options: {
readonly window: Duration.Input
readonly flush: (
messages: Array<NoInfer<Output>>
) => Effect.Effect<void>
}
) => Effect.Effect<
Logger<Message, void>,
never,
Scope.Scope
>)
Creates a scoped logger that batches the output of another logger.
Details
The returned effect starts a scoped background process that periodically
passes buffered outputs to flush. When the scope closes, the background
process is interrupted and any remaining buffered entries are flushed.
Example (Batching logger output)
import { Duration, Effect, Logger } from "effect"
// Create a batched logger that flushes every 5 seconds
const batchedLogger = Logger.batched(Logger.formatJson, {
window: Duration.seconds(5),
flush: (messages) =>
Effect.sync(() => {
console.log(`Flushing ${messages.length} log entries:`)
messages.forEach((msg, i) => console.log(`${i + 1}. ${msg}`))
})
})
const program = Effect.gen(function*() {
const logger = yield* batchedLogger
yield* Effect.provide(
Effect.all([
Effect.log("Event 1"),
Effect.log("Event 2"),
Effect.log("Event 3"),
Effect.sleep(Duration.seconds(6)), // Trigger flush
Effect.log("Event 4")
]),
Logger.layer([logger])
)
})
// Remote batch logging example
const remoteBatchLogger = Logger.batched(Logger.formatStructured, {
window: Duration.seconds(10),
flush: (entries) =>
Effect.sync(() => {
// Send batch to remote logging service
console.log(`Sending ${entries.length} log entries to remote service`)
})
})
batched(self: Logger<Message, string>(parameter) self: {
log: (options: Options<Message>) => 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; <…;
}
self, {
window: Duration.Inputwindow: options: | {
readonly flag?:
| FileSystem.OpenFlag
| undefined
readonly mode?: number | undefined
readonly batchWindow?:
| Duration.Input
| undefined
}
| undefined
options?.batchWindow ?? 1000,
flush: (
output: any
) => Effect.Effect<void, never, _R>
flush: (output: anyoutput) => import effecteffect.const ignore: <
Arg extends
| Effect.Effect<any, any, any>
| {
readonly log?:
| boolean
| LogLevel.Severity
| undefined
readonly message?: string | undefined
}
| undefined = {
readonly log?:
| boolean
| LogLevel.Severity
| undefined
readonly message?: string | undefined
}
>(
effectOrOptions: Arg,
options?:
| {
readonly log?:
| boolean
| LogLevel.Severity
| undefined
readonly message?: string | undefined
}
| undefined
) => [Arg] extends [
Effect.Effect<infer _A, infer _E, infer _R>
]
? Effect.Effect<void, never, _R>
: <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<void, never, R>
ignore(const logFile: FileSystem.Fileconst logFile: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
logFile.write(const encoder: TextEncoderencoder.TextEncoder.encode(input?: string): Uint8Array<ArrayBuffer>The TextEncoder.encode() method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
encode(output: anyoutput.join("\n") + "\n")))
})
})
)