Live logs

Every admin log page of yf has a Live button: Admin activity, Audit log, DB revisions, the admin requests (exec) log, admin and user log-ins and failed log-ins, redirects, sent e-mails, and System logs (the PHP error log, the framework error logs and the errors table). It opens ?object=log_stream&source=<log>, which follows the log as it is written: new entries appear at the bottom without reloading, each with the details of its log. The list pages themselves do not change; ?object=log_stream without a source lists the logs the admin may follow. Try it on the demo logs/live of these docs.

The view

  • Refresh interval (the ⟳ button): every 5, 10, 15, 30 or 60 seconds, or Off; 10 seconds until chosen. The choice applies at once and is kept for this log and this admin (a cookie); with Off, Refresh now reads the new entries.
  • Pause / Play: while paused the list stays as it is and the badge counts what came meanwhile ("120 new while paused"); Play adds them. Follow keeps the newest row in view; scrolling up stops it, Jump to newest starts it again.
  • Filter shown rows hides the rows without the text, Highlight marks the rows with it, the level select keeps notices, warnings or errors and above; all three look at the whole entry (message, tags, details). Keep is how many rows stay in the page (100 to 5000, default 500): older ones are removed, so a view can stay open for hours. Clear empties it.
  • The log's own filters (IP, admin, login, URL, table, slow requests, failed mails...) are sent to the server and restart the view with matching entries only.
  • A row opens to its details. What each log shows:
    • Admin requests: the URL, admin and group, IP, run time (slow requests are warnings from 1 s, errors from 5 s), DB queries, page size, browser, referer; links to the requests of the IP and of the admin.
    • Log-ins: login, account, group, IP, browser, referer, the first characters of the session id. Failed log-ins: the login tried, the reason (wrong login or blocked, which is an error), the IP; the tried password is never shown. A counter on each row says how many times its IP came up in the view, so a brute force stands out.
    • Admin activity and the audit log: who changed which record of which module, the fields that changed (old and new value; password, token and key fields as ***), the URL and browser; DB revisions add the table, the query method and where in the code it came from.
    • System logs and the errors table: level, message, file and line, the site code that called into the framework, the trace, the URL, and the suggested fix of the log viewer with a link to its group.
    • Redirects: from, to, reason and trace. Sent e-mails: subject, addresses, driver, time, the provider's error.

Secrets are masked before they leave the server (tokens and keys in URLs, cookies, session ids, Bearer headers), and every text is shown as text: a log line with markup or template tags cannot run.

How it works

The page asks the server for the entries after its cursor at the chosen interval, at most 200 at a time; when more are waiting it asks again at once. Each answer is a short request: no PHP worker is held between polls, nothing needs configuring in nginx or a proxy, and the request closes the PHP session before it reads the log, so it never makes the admin's other tabs wait. A table is read by its primary key (id > cursor; an id that commits late is asked for again for 10 seconds); the failed log-in tables by their time stamp, 2 seconds behind. A file is read from the byte offset of the cursor, only the new bytes; a rotated log (renamed to .1) is finished from the rotated copy, a truncated one read again from the start, and an entry with a stack trace is shown once complete. The page pauses polling while its tab is hidden and retries with a growing delay (2 to 60 seconds) when the server cannot be reached; when the session ends it stops and says so.

Access and settings

Super-admins (group 1) only; a DENY block rule does not lock them out. Another admin group needs three things: an ACCESS entry for the log (or '*'), a center-block rule that lets the group open the log_stream module (like any admin page), and the right to open that log's own list page (its block rules again). A log without a list page of its own (a SOURCES entry without module) has no block rule to ask, so '*' does not open it: only an ACCESS entry that names it does.

$PROJECT_CONF['admin_log_stream'] = [
    'ACCESS' => ['admin_wall' => [2], 'audit' => [2]], // or '*' => [2] for every log
    'INTERVAL_MS' => 10000,    // until the admin chooses another interval
    'IDLE_INTERVAL_MS' => 6000, // after five empty answers, when the chosen interval is shorter
    'MAX_ROWS' => 200,         // per answer
    'RING_ROWS' => 500,        // rows kept by default
    'WAIT_SECONDS' => 0,       // > 0: long-poll (each waiting poll holds a PHP worker)
    'ENABLED' => true,         // false: no Live buttons, no live pages
];

A site adds a log of its own with a source class (a subclass of yf_log_stream_source_db names its table and renders a row) and 'SOURCES' => ['my_log' => ['class' => 'my_log_stream_source', 'title' => 'My log', 'module' => 'my_log_page']] (module: the admin list page whose block rules other groups must pass); false in place of the array removes a built-in one. The cursor queries need no new index: every log table has a primary key that grows.