Skip to content
← Back to Snippets
Code

Maintenance Job Result Logger

Formats maintenance job outcomes into normalized log rows with duration, counts, and severity.

Purpose

Formats maintenance job outcomes into normalized log rows with duration, counts, and severity.

Snippet details

ContextSystemLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Maintenance Job Result Logger.
 *
 * Purpose:
 * Formats maintenance job outcomes into normalized log rows with duration, counts, and severity.
 *
 * @param string $job_name Job identifier.
 * @param float $started_at Start timestamp from microtime(true).
 * @param array $counts Count fields such as scanned, changed, and failed.
 * @return array Normalized maintenance log row.
 */
function ogSnippetMaintenanceJobResultLogger(string $job_name, float $started_at, array $counts): array {
	$failed = 0;
	if (isset($counts['failed'])) {
		$failed = (int) $counts['failed'];
	}
	$severity = 'info';
	if ($failed > 0) {
		$severity = 'warning';
	}
	return array(
		'job' => $job_name,
		'finished_at' => date('c'),
		'duration_ms' => (int) round((microtime(true) - $started_at) * 1000),
		'counts' => $counts,
		'severity' => $severity
	);
}

$job_row = ogSnippetMaintenanceJobResultLogger('deflector-cache-cleanup', microtime(true) - 0.042, array('scanned' => 48, 'changed' => 7, 'failed' => 0));
echo $job_row['severity'];