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

  • Eof

    End 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

Possible errors returned by disk log operations.

pub type DiskLogError {
  DiskLogError(reason: String)
}

Constructors

  • DiskLogError(reason: String)

A handle to a disk log.

pub opaque type LogDisk

The format of the log data.

pub type LogFormat {
  Internal
  External
}

Constructors

  • Internal

    Internal Erlang term format.

  • External

    External binary format.

Detailed information about a disk log.

pub type LogInfo {
  LogInfo(
    name: String,
    file: String,
    type_: LogType,
    format: LogFormat,
    size: LogSize,
    mode: LogMode,
  )
}

Constructors

The access mode for the log.

pub type LogMode {
  ReadOnly
  ReadWrite
}

Constructors

  • ReadOnly

    Open the log in read-only mode.

  • ReadWrite

    Open 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

  • Enable

    Enable repair if needed.

  • Truncate

    Truncate the log if corrupted.

  • Disabled

    Disable repair.

The maximum size of the log.

pub type LogSize {
  Infinity
  MaxBytes(Int)
  WrapSize(max_bytes: Int, max_files: Int)
}

Constructors

  • Infinity

    No 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.

The type of log to create.

pub type LogType {
  Halt
  Wrap
  Rotate
}

Constructors

  • Halt

    A single file log.

  • Wrap

    A sequence of wrap files.

  • Rotate

    A sequence of rotate files (only external format).

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 close(log: LogDisk) -> Result(LogDisk, DiskLogError)

Close a disk log.

pub fn file(options: LogOptions, path: String) -> LogOptions

Set the file path for the log.

pub fn format(options: LogOptions, f: LogFormat) -> LogOptions

Set the log format.

pub fn head(
  options: LogOptions,
  h: dynamic.Dynamic,
) -> LogOptions

Set the log header.

pub fn head_func(
  options: LogOptions,
  module: String,
  function: String,
  args: List(dynamic.Dynamic),
) -> LogOptions

Set the log header function (MFA).

This function is used to generate a header for each new log file.

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 mode(options: LogOptions, m: LogMode) -> LogOptions

Set the access mode for the log.

pub fn new(name: String) -> LogDisk

Create a new LogDisk instance from a name.

let log = disk_log.new("my_log")
pub fn notify(options: LogOptions, n: Bool) -> LogOptions

Set the notification flag.

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 const options_empty: LogOptions

Empty configuration options.

pub fn quiet(options: LogOptions, q: Bool) -> LogOptions

Set the quiet flag to suppress error messages to the terminal.

pub fn repair(options: LogOptions, r: LogRepair) -> LogOptions

Set the repair strategy.

pub fn size(options: LogOptions, s: LogSize) -> LogOptions

Set the log size.

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 type_(options: LogOptions, t: LogType) -> LogOptions

Set the log type.

pub fn unblock(log: LogDisk) -> Result(LogDisk, DiskLogError)

Unblock a previously blocked log.

Search Document