Maintenance Job Result Logger
Logs job duration, counts, warnings, and errors in a consistent format.
Function signature
ogLogMaintenanceJobResult(job_name, job_result = array(), log_file = '')
Categories
- Forms and Validation
Parameters
job_nameLog-safe maintenance job name.job_resultStructured result values from a completed maintenance job.log_fileOptional approved log file path for appending the maintenance record.Return value
Public-safe status string returned by the function for explicit controller branching or logging.
- success
- message
- data
Compatibility
Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.
Minimum PHP version: 7.4
Security notes
Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.
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.
*/
/**
* Logs job duration, counts, warnings, and errors in a consistent format.
*
* Primary use case: Cron and admin maintenance.
* Typical inputs: job name, result data.
* Typical output: log record.
*
* Implementation note: Keep detailed errors server-side.
*
* @param string $job_name Maintenance job name.
* @param array $job_result Job result counts, warnings, and errors.
* @param string $log_file Optional approved JSONL log file path.
* @return array Maintenance log record result.
*/
function ogLogMaintenanceJobResult($job_name, $job_result = array(), $log_file = '') {
$result = array(
'success' => false,
'message' => '',
'data' => array()
);
$job_name = trim((string)$job_name);
$log_file = trim((string)$log_file);
if (empty($job_name)) {
$result['message'] = 'Job name is required.';
return $result;
}
if (!is_array($job_result)) {
$job_result = array();
}
$record = array(
'job_name' => $job_name,
'completed_at' => date('c'),
'success' => !empty($job_result['success']),
'duration_seconds' => 0,
'counts' => array(),
'warnings' => array(),
'errors' => array()
);
if (!empty($job_result['duration_seconds'])) {
$record['duration_seconds'] = (float)$job_result['duration_seconds'];
}
if (!empty($job_result['counts']) && is_array($job_result['counts'])) {
$record['counts'] = $job_result['counts'];
}
if (!empty($job_result['warnings']) && is_array($job_result['warnings'])) {
$record['warnings'] = $job_result['warnings'];
}
if (!empty($job_result['errors']) && is_array($job_result['errors'])) {
foreach ($job_result['errors'] as $error) {
$record['errors'][] = preg_replace('/(password|token|secret|api[_-]?key)\s*[:=]\s*[^\s,&]+/i', '$1=[redacted]', (string)$error);
}
}
$written = false;
if (!empty($log_file)) {
$directory = dirname($log_file);
if (is_dir($directory) && is_writable($directory)) {
$line = json_encode($record) . PHP_EOL;
if (file_put_contents($log_file, $line, FILE_APPEND | LOCK_EX) !== false) {
$written = true;
}
}
}
$result['success'] = true;
$result['message'] = 'Maintenance job result logged.';
$result['data'] = array('record' => $record, 'written' => $written);
return $result;
}