ServerLogger (ServerLogger v0.3.0)

Copy Markdown View Source

Context module for server log persistence and querying. Provides functions for inserting, listing, counting, pruning logs, and managing monthly partitions.

Summary

Functions

Counts logs matching the given filters (ignores limit/offset).

Drops a partition for the given year/month.

Creates a partition for the given year/month. Idempotent.

Bulk inserts log entries into the server_logs table. Uses log: false to prevent Ecto query logs from causing recursion.

Lists logs with filtering, sorting, and pagination.

Lists all child partitions of the server_logs table. Returns a list of maps with :name, :from, and :to keys.

Conditionally logs a message with the [ServerLogger] prefix, based on the :logging_enabled config setting.

Checks if a partition exists for the given year/month.

Deletes logs of the specified level older than the given number of days. Deletes in batches of 10,000 to avoid long locks.

Returns the total disk usage of the server_logs table (including all partitions and indexes) in bytes.

Truncates a message if it exceeds the configured max_message_size_mb. Appends " [truncated]" to truncated messages.

Functions

count_logs(opts \\ [])

Counts logs matching the given filters (ignores limit/offset).

drop_partition(year, month)

Drops a partition for the given year/month.

ensure_partition_exists(year, month)

Creates a partition for the given year/month. Idempotent.

insert_logs(entries)

Bulk inserts log entries into the server_logs table. Uses log: false to prevent Ecto query logs from causing recursion.

list_logs(opts \\ [])

Lists logs with filtering, sorting, and pagination.

Options

  • :levels — list of level strings to include
  • :search — keyword search via ILIKE on message, module, request_id
  • :from / :to — datetime range filter on logged_at
  • :sort_by — column atom (default: :logged_at)
  • :sort_dir — :asc or :desc (default: :desc)
  • :limit — max results (default: 100)
  • :offset — offset for pagination (default: 0)

list_partitions()

Lists all child partitions of the server_logs table. Returns a list of maps with :name, :from, and :to keys.

maybe_log_message(message)

Conditionally logs a message with the [ServerLogger] prefix, based on the :logging_enabled config setting.

partition_exists?(year, month)

Checks if a partition exists for the given year/month.

prune_logs(level, older_than_days)

Deletes logs of the specified level older than the given number of days. Deletes in batches of 10,000 to avoid long locks.

table_size_bytes()

Returns the total disk usage of the server_logs table (including all partitions and indexes) in bytes.

truncate_message(message)

Truncates a message if it exceeds the configured max_message_size_mb. Appends " [truncated]" to truncated messages.