Files
Martin Slachta 0d829845c4 initial
2026-06-11 19:03:29 +02:00

1.8 KiB

Logger Module

A file-based logger that writes structured entries to a JSONL file in the WordPress uploads directory. Designed as a drop-in replacement for error_log() that produces entries you can read, table-display, and download from the admin UI.

Namespace: Reservair\Logger
Log file: wp-content/uploads/reservair/reservair.log

Usage

use Reservair\Logger\Logger;

Logger::info('Timetable reservation created.');
Logger::warning('Maintainer email not set for timetable #' . $id);
Logger::error('Insert failed: ' . $message);

// Accepts Throwable directly — replaces error_log($e) call sites directly
Logger::error($exception);

Admin UI integration

use Reservair\Logger\Logger;

// Table — get_entries() returns newest-first; each entry has time/level/message keys
foreach (Logger::get_entries() as $entry) {
    // $entry['time']    e.g. "2026-05-30 14:22:01"
    // $entry['level']   "info" | "warning" | "error"
    // $entry['message'] the log text
}

// Download button — serve this path as a file download
$path = Logger::get_path();

// Clear the log
Logger::clear();

Log file format

Each line is a JSON object (JSONL). Safe to tail, grep, or import into any log viewer.

{"time":"2026-05-30 14:22:01","level":"info","message":"Reservation #12 created."}
{"time":"2026-05-30 14:22:03","level":"error","message":"Insert failed: Duplicate entry"}

Notes

  • Written to wp-content/uploads/reservair/ rather than the plugin directory so the file survives plugin updates.
  • The directory is created on first write with an .htaccess that blocks direct browser access (Deny from all).
  • LOCK_EX is used on every write to prevent interleaved entries under concurrent requests.
  • wp_upload_dir() result is cached statically to avoid repeated database lookups per request.