Sovite.Core.Logging.FileHandler (sovite v0.2.0)

Copy Markdown View Source

:logger handler that writes to rotating log files, in the style of pino-roll.

File names come from a pattern such as sovite.{date}.{n}.log. {date} is the current date formatted with date_format (a Calendar.strftime/2 format), and {n} is a number that starts at 1 and goes up with each rotation:

sovite.2026-10-04.1.log
sovite.2026-10-04.2.log
sovite.2026-10-05.1.log

A new file is started when the next line would push the current file over max_size, or when the rotation period (hour, day, ISO week, or month) changes. After a rotation, only the newest max_files files that match the pattern are kept (0 keeps all). On restart, logging continues in the newest file for the current date if it is not full. With symlink set, a symlink of that name in the directory always points to the current file. Dates use local time, or UTC when config :logger, utc_log: true is set.

Run it under a supervisor. It installs the :logger handler when it starts and removes it when it stops:

{Sovite.Core.Logging.FileHandler,
 id: :sovite_file,
 formatter: Sovite.Core.Logging.formatter(:text),
 config: %{directory: "/var/log/sovite", file_name: "sovite.{date}.{n}.log", ...}}

Options

  • :id - the :logger handler ID. Required.
  • :config - a map with the keys in config_keys/0. Required.
  • :formatter - the {module, config} formatter. Required.
  • :level - the handler level. Defaults to :all.
  • :replace - ID of a handler that is silenced (level :none) while this one runs, and whose filters this one copies.

Overload

Events are formatted in the logging process and sent to the writer process. While fewer than 10 events are waiting, logging does not block. Above that, callers wait until their event is buffered; above 200, events are dropped, and a line with the number of dropped events is written once the writer catches up. Writes are batched and flushed as soon as the writer is idle.

If the file cannot be written, the error goes to standard error and opening it is retried every second. Events in between are dropped.

Summary

Functions

Keys of the :config option.

Starts the writer and installs the :logger handler.

Writes everything buffered by handler id to its file. Events logged before the call are included.

Types

rotation()

@type rotation() :: :never | :hourly | :daily | :weekly | :monthly

Functions

config_keys()

@spec config_keys() :: [atom()]

Keys of the :config option.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the writer and installs the :logger handler.

sync(id)

@spec sync(:logger.handler_id()) :: :ok | {:error, term()}

Writes everything buffered by handler id to its file. Events logged before the call are included.