(
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 = dual<(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>>(isDataFirst: (args: IArguments) => boolean, body: <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>): ((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>) (+1 overload)
Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual<
(
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 OpenFlag =
| "r"
| "r+"
| "w"
| "wx"
| "w+"
| "wx+"
| "a"
| "ax"
| "a+"
| "ax+"
File open flags that determine how a file is opened and what operations are allowed.
Details
These flags correspond to standard POSIX file open modes and control the file access
permissions and behavior when opening files.
"r" - Read-only. File must exist.
"r+" - Read/write. File must exist.
"w" - Write-only. Truncates file to zero length or creates new file.
"wx" - Like 'w' but fails if file exists.
"w+" - Read/write. Truncates file to zero length or creates new file.
"wx+" - Like 'w+' but fails if file exists.
"a" - Write-only. Appends to file or creates new file.
"ax" - Like 'a' but fails if file exists.
"a+" - Read/write. Appends to file or creates new file.
"ax+" - Like 'a+' but fails if file exists.
Example (Opening files with flags)
import { Effect, FileSystem } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// Open for reading only
const readFile = yield* fs.open("data.txt", { flag: "r" })
// Open for writing, truncating existing content
const writeFile = yield* fs.open("output.txt", { flag: "w" })
// Open for appending
const appendFile = yield* fs.open("log.txt", { flag: "a" })
// Open for read/write, but fail if file doesn't exist
const editFile = yield* fs.open("config.json", { flag: "r+" })
})
OpenFlag | undefined
readonly mode?: number | undefinedmode?: number | undefined
readonly batchWindow?: Duration.Input | undefinedbatchWindow?: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input | 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.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<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.FileSystem>,
<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 OpenFlag =
| "r"
| "r+"
| "w"
| "wx"
| "w+"
| "wx+"
| "a"
| "ax"
| "a+"
| "ax+"
File open flags that determine how a file is opened and what operations are allowed.
Details
These flags correspond to standard POSIX file open modes and control the file access
permissions and behavior when opening files.
"r" - Read-only. File must exist.
"r+" - Read/write. File must exist.
"w" - Write-only. Truncates file to zero length or creates new file.
"wx" - Like 'w' but fails if file exists.
"w+" - Read/write. Truncates file to zero length or creates new file.
"wx+" - Like 'w+' but fails if file exists.
"a" - Write-only. Appends to file or creates new file.
"ax" - Like 'a' but fails if file exists.
"a+" - Read/write. Appends to file or creates new file.
"ax+" - Like 'a+' but fails if file exists.
Example (Opening files with flags)
import { Effect, FileSystem } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// Open for reading only
const readFile = yield* fs.open("data.txt", { flag: "r" })
// Open for writing, truncating existing content
const writeFile = yield* fs.open("output.txt", { flag: "w" })
// Open for appending
const appendFile = yield* fs.open("log.txt", { flag: "a" })
// Open for read/write, but fail if file doesn't exist
const editFile = yield* fs.open("config.json", { flag: "r+" })
})
OpenFlag | undefined
readonly mode?: number | undefinedmode?: number | undefined
readonly batchWindow?: Duration.Input | undefinedbatchWindow?: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input | undefined
} | undefined
) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<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.FileSystem>
>(
(args: IArgumentsargs) => 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: IArgumentsargs[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: stringpath, 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.const FileSystem: Context.Service<
FileSystem,
FileSystem
>
const FileSystem: {
key: string;
Service: {
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>;
};
of: (this: void, self: FileSystem.FileSystem) => FileSystem.FileSystem;
context: (self: FileSystem.FileSystem) => Context.Context<FileSystem.FileSystem>;
use: (f: (service: FileSystem.FileSystem) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, FileSystem.FileSystem | R>;
useSync: (f: (service: FileSystem.FileSystem) => A) => Effect.Effect<A, never, FileSystem.FileSystem>;
Identifier: Identifier;
stack: string | undefined;
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;
}
Core interface for file system operations in Effect.
Details
The FileSystem interface provides a comprehensive set of file and directory operations
that work cross-platform. All operations return Effect values that can be composed,
transformed, and executed safely with proper error handling.
Example (Accessing file system operations)
import { Console, Effect, FileSystem } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// Basic file operations
const exists = yield* fs.exists("./config.json")
if (!exists) {
yield* fs.writeFileString("./config.json", "{\"env\": \"development\"}")
}
// Directory operations
yield* fs.makeDirectory("./logs", { recursive: true })
// File information
const stats = yield* fs.stat("./config.json")
yield* Console.log(`File size: ${stats.size} bytes`)
// Streaming operations
const content = yield* fs.readFileString("./config.json")
yield* Console.log("Config:", content)
})
Service tag for platform file-system operations.
When to use
Use to access or provide operations for files, directories, permissions,
streams, and sinks through the Effect context.
Details
This key is used to provide and access the FileSystem service in the Effect context.
Example (Accessing and providing FileSystem)
import { Effect, FileSystem } from "effect"
// Access the FileSystem service
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const exists = yield* fs.exists("./data.txt")
if (exists) {
const content = yield* fs.readFileString("./data.txt")
yield* Effect.log("File content:", content)
}
})
// Provide a custom FileSystem implementation
declare const platformImpl: Omit<
FileSystem.FileSystem,
"exists" | "readFileString" | "stream" | "sink" | "writeFileString"
>
const customFs = FileSystem.make(platformImpl)
const withCustomFs = Effect.provideService(
program,
FileSystem.FileSystem,
customFs
)
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.FileSystem.open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>Open a file at path with the specified options.
Details
The file handle will be automatically closed when the scope is closed.
open(path: stringpath, { flag?: FileSystem.OpenFlag | undefinedflag: "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
>)
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?: Duration.Input | undefinedbatchWindow ?? 1000,
flush: (
messages: string[]
) => Effect.Effect<void>
flush: (output: string[]output) => 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.File.write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>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: string[]output.globalThis.Array<string>.join(separator?: string): stringAdds all the elements of an array into a string, separated by the specified separator string.
join("\n") + "\n")))
})
})
)