Search by

denetorus / galaxy-logging

Denetorus

Writes structured request records to daily JSON Lines files and removes old logs with optional verified ZIP archiving.

Package info

github.com/Denetorus/GalaxyLogging

pkg:composer/denetorus/galaxy-logging

Statistics

Installs: 32

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v0.3.0 2026-12-03 12:13 UTC

This package is auto-updated.

Last update: 2026-12-03 12:15:35 UTC


README

Writes structured request records to daily JSON Lines files and removes old logs with optional verified ZIP archiving.

Русский · Architecture · MIT

Getting started

Requires PHP 8.4 or newer. Install version 0.3.0 from Packagist.

composer require denetorus/galaxy-logging:0.3.0

From the source checkout, install dependencies and run

composer install
php examples/log.php /absolute/path/to/logs

Complete example examples/log.php.

Supply a request ID, a channel and a descriptor. Start and finish records share the ID. With detailed logging the caller selects parameters and result sections. To archive older files before removing them, use JsonFileWriter::deleteBefore() with an archive directory and ext-zip.

Configuration

Levels are basic (default) and detailed, independently for startLevel and finishLevel. Basic records retain the previous compact schema. Detailed start adds parameters. Detailed finish adds details. Top-level errors/warnings/messages remain counts; details.errors/details.warnings contain complete entry arrays including code, text, context, UID/originPath and consoleCode where applicable. details.data is getAllData(). finishFields defaults to ['errors','warnings']; choose any subset of errors/warnings/data, or [] for no sections.

Parameter selection

  • No includeParameters/excludeParameters: all supplied parameters are logged.
  • includeParameters: only matching keys; [] selects nothing.
  • excludeParameters: all except matching keys; [] excludes nothing.
  • Both lists together are invalid. Lists must contain nonempty strings, compared exactly and case-sensitively.
  • Matching applies recursively by key name. Including a container key selects its whole subtree; excluding a container removes its whole subtree. Array indices can be selected by string representation.
  • Parameter lists apply only to start parameters, not result details. Detailed result sections are unredacted.

Without exclusions, secrets inside parameters are logged deliberately. Result data/error contexts may also contain secrets when selected. The application owns selection and log access. No implicit secret-name blacklist is applied.

Records and writer

Each record has event, timestamp UTC, requestId, application, channel, request. The request descriptor is supplied by the caller and reused for both records. Finish adds monotonic durationMs, success, business code, consoleCode, message/warning/error counts, and stageFailures (stage/phase/exceptionClass, no exception messages/traces). No HTTP status is inferred. Direct GLogger callers supply trusted request/config/parameters as optional final arguments.

JsonFileWriter writes UTC YYYY-MM-DD.jsonl under directory. Use an absolute local path outside the public document root. Directory is created as needed. A file lock spans the full JSON line and flush; partial writes are retried. Encoding occurs before opening the file. Start and finish are each attempted at most once per logger. The caller selects when to record them. An abrupt termination can leave a start without a finish. The caller may supply prepared summaries of stage and lifecycle failures.

Checks

php tests/run.php PATH_TO_DEPENDENCY_AUTOLOAD ABSOLUTE_VERIFICATION_DIRECTORY

Exercises levels, selectors, result details and concurrent writers. No public API calls.

Deleting old daily logs

Explicit maintenance API; never invoked automatically by the stages:

$writer = new JsonFileWriter($root . '/var/log');
$result = $writer->deleteBefore(
    '2026-11-01',
    archiveDirectory: $root . '/var/log-archives',
);

Only immediate regular files named exactly YYYY-MM-DD.jsonl with a valid calendar date strictly BEFORE cutoff AND before today's UTC date are eligible. The cutoff day, current/future days, subdirectories, links and other files are untouched. Dates are strict Y-m-d; invalid cutoff throws InvalidArgumentException. Paths must be absolute/local, without .. or symlink ancestors. Invalid/inaccessible paths produce a preparation error, without deletion. A missing log directory or empty selection returns empty lists; no archive is created.

Without archiveDirectory, eligible files are deleted directly. ZIP is optional (ext-zip is suggested, not required for normal logging/plain cleanup). With an archive directory, a unique ZIP is created, closed, reopened read-only with consistency checking, and every entry is streamed and checked for exact size and SHA-256 against manifest.json. The manifest itself and exact entry count are also checked. Source size/hash are rechecked before any deletion. The name contains the first and last ACTUALLY selected file dates, UTC creation time and random suffix: logs_2026-10-01_2026-10-31_20261125T143052Z_a83f92c15bbd0021.zip. Gaps in dates are allowed. Existing archives are never overwritten. On preparation/archive/verification failure ALL selected sources remain. An unsuccessful ZIP attempt may leave an unverified archive file; archive in the returned result stays null. No potentially incomplete archive is treated as permission to delete logs.

Return shape: ['archive'=>?string, 'deleted'=>list, 'errors'=>list]. archive is the full verified ZIP path; deleted contains basenames. Errors contain file (basename or null), operation and a safe message. Individual unlink failures are reported and that source remains; other verified files may already have been deleted. A verified ZIP is kept even if later source verification/unlink fails.

Writer and cleanup coordinate through .galaxy-logging.lock: writers take a shared directory lock, cleanup takes an exclusive nonblocking lock and shared per-file read locks. If busy, cleanup returns an error without deletion; callers may retry later. Upgrade all writers before using concurrent maintenance; arbitrary external processes that ignore these locks, hostile directory modifications and old writer versions are outside this coordination. Keep log/archive directories trusted and restrict access. Archive storage must protect the same data as logs. Cleanup does not rewrite/truncate entries within a daily file. No recursive deletion is performed.

php tests/retention.php DEPENDENCY_AUTOLOAD ABSOLUTE_VERIFICATION_DIRECTORY
php -n tests/retention-nozip.php ABSOLUTE_VERIFICATION_DIRECTORY

For an installed-package test pass --installed as the final argument to retention.php.

GalaxyResult 0.3 uses getBusinessCode(). Finish records keep the JSON key code; its value is the business code. Console codes remain separate.