gbr/disk_log
GBR Disk Log: A Type-Safe wrapper for Erlang’s disk_log.
This module provides an idiomatic Gleam interface for creating and managing disk logs. It supports various log types (halt, wrap, rotate), formats (internal, external), and logging modes.
The disk_log module is part of Erlang’s kernel application and is widely
used for building reliable, disk-backed logging systems that prevent
Out-Of-Memory (OOM) errors in high-throughput environments.
When to use it?
It is ideal for telemetry, audit logs, and actor state persistence where you need a bounded disk footprint (ring buffers) without blocking actor mailboxes or causing OOM.
Types
Data returned from a chunked read operation.
pub type ChunkData {
Eof
Chunk(continuation: Continuation, terms: List(BitArray))
}
Constructors
-
EofEnd of file reached.
-
Chunk(continuation: Continuation, terms: List(BitArray))A chunk of data with its continuation.
A continuation for chunked reading of log data.
pub type Continuation {
Continuation(dynamic.Dynamic)
}
Constructors
-
Continuation(dynamic.Dynamic)
Possible errors returned by disk log operations.
pub type DiskLogError {
DiskLogError(reason: String)
}
Constructors
-
DiskLogError(reason: String)
The format of the log data.
pub type LogFormat {
Internal
External
}
Constructors
-
InternalInternal Erlang term format.
-
ExternalExternal binary format.
The access mode for the log.
pub type LogMode {
ReadOnly
ReadWrite
}
Constructors
-
ReadOnlyOpen the log in read-only mode.
-
ReadWriteOpen the log in read-write mode.
Configuration options for opening a disk log.
pub opaque type LogOptions
The strategy for repairing a log if it’s found to be corrupted.
pub type LogRepair {
Enable
Truncate
Disabled
}
Constructors
-
EnableEnable repair if needed.
-
TruncateTruncate the log if corrupted.
-
DisabledDisable repair.
The maximum size of the log.
pub type LogSize {
Infinity
MaxBytes(Int)
WrapSize(max_bytes: Int, max_files: Int)
}
Constructors
-
InfinityNo size limit.
-
MaxBytes(Int)Maximum bytes for a single file.
-
WrapSize(max_bytes: Int, max_files: Int)Maximum bytes and number of files for a wrap log.
Values
pub fn async_log(
log: LogDisk,
data: BitArray,
) -> Result(LogDisk, DiskLogError)
Log data to a disk log asynchronously.
This function returns immediately after sending the data to the log process.
pub fn binary_async_log(
log: LogDisk,
data: BitArray,
) -> Result(LogDisk, DiskLogError)
Log binary data to a disk log asynchronously.
Similar to async_log, but optimized for binary data.
pub fn block(
log: LogDisk,
queue: Bool,
) -> Result(LogDisk, DiskLogError)
Block a log for maintenance.
When blocked, no new entries can be logged until unblock is called.
If queue is true, logging requests are queued.
pub fn chunk(
log: LogDisk,
cont: Continuation,
) -> Result(ChunkData, DiskLogError)
Read a chunk of data from the log starting at the given continuation.
Use start_continuation() to begin reading from the start of the log.
pub fn file(options: LogOptions, path: String) -> LogOptions
Set the file path for the log.
pub fn increment_wrap_file(
log: LogDisk,
) -> Result(LogDisk, DiskLogError)
Force a wrap log to move to the next file in the sequence.
pub fn info(log: LogDisk) -> Result(LogInfo, DiskLogError)
Get detailed information about the log state.
pub fn log(
log: LogDisk,
data: BitArray,
) -> Result(LogDisk, DiskLogError)
Log data to a disk log synchronously.
This function waits until the data is written to disk (or the OS buffers).
pub fn new(name: String) -> LogDisk
Create a new LogDisk instance from a name.
let log = disk_log.new("my_log")
pub fn open(log: LogDisk) -> Result(LogDisk, DiskLogError)
Open a disk log with default options.
If the log is already open, it returns the existing handle.
pub fn open_options(
log: LogDisk,
options: LogOptions,
) -> Result(LogDisk, DiskLogError)
Open a disk log with specific options.
pub fn start_continuation() -> Continuation
Get the initial continuation for reading from the beginning of the log.
pub fn sync(log: LogDisk) -> Result(LogDisk, DiskLogError)
Synchronize the log buffers to disk.
pub fn unblock(log: LogDisk) -> Result(LogDisk, DiskLogError)
Unblock a previously blocked log.