class

Logit::Backend::File

Inherits Logit::BufferedIO / Logit::Backend / Reference / Object

Backend that writes log events to a file.

Uses the Formatter::JSON formatter by default, which produces structured JSON output suitable for log aggregation and analysis systems.

Basic Usage

Logit.configure do |config|
  config.file("logs/app.log", level: Logit::LogLevel::Debug)
end

Security Features

  • Files are created with mode 0o600 (owner read/write only) by default
  • Symlinks are not followed by default (prevents log injection attacks)
  • Parent directory must exist (prevents path traversal)

Custom Configuration

backend = Logit::Backend::File.new(
  path: "logs/audit.log",
  name: "audit",
  level: Logit::LogLevel::Info,
  formatter: Logit::Formatter::Human.new,
  mode: 0o644,           # World-readable
  follow_symlinks: true  # Allow symlinks
)

Output Example (JSON formatter)

{"trace_id":"abc...","span_id":"def...","name":"find_user","level":"info",...}

Constants

DEFAULT_FILE_MODE = 384

Default file permission mode (owner read/write only).

Constructors

Instance methods

close

Closes the file handle, flushing any remaining buffered data.

Source
flush

Flushes the output buffer to disk.

Source
log(event : Event) : Nil

Logs an event to the file.

Source

Nested types