1000 ? substr($value, 0, 1000) . '...[truncated]' : $value; } /** * Recursively redact and bound structured log context. * * @param mixed $value Value to sanitize. * @param int $depth Current recursion depth. * @return mixed */ function vasp_logger_redact_context($value, $depth = 0) { if ($depth >= 5) { return '[TRUNCATED DEPTH]'; } if (is_array($value)) { $safe = []; $count = 0; foreach ($value as $key => $item) { if ($count >= 50) { $safe['_truncated_items'] = count($value) - $count; break; } $safe[$key] = vasp_logger_is_sensitive_key((string) $key) ? '[REDACTED]' : vasp_logger_redact_context($item, $depth + 1); $count++; } return $safe; } if (is_object($value)) { return $value instanceof Throwable ? '[REDACTED EXCEPTION]' : '[OBJECT ' . get_class($value) . ']'; } if (is_resource($value)) { return '[RESOURCE]'; } return vasp_logger_redact_scalar($value); } /** * Decide whether a level should reach the production log sink. * * Production defaults to warnings and errors. Developers can set * VASP_LOG_LEVEL to debug, info, warning, error, or none in wp-config.php. * * @param string $level Normalized log level. * @return bool */ function vasp_logger_should_log($level) { $weights = [ 'debug' => 10, 'info' => 20, 'warning' => 30, 'error' => 40, 'none' => 100, ]; $configured = defined('VASP_LOG_LEVEL') ? strtolower((string) VASP_LOG_LEVEL) : ''; if (!isset($weights[$configured])) { $configured = defined('WP_DEBUG') && WP_DEBUG ? 'debug' : 'warning'; } return $weights[$level] >= $weights[$configured]; } /** * Log a message to the PHP error log * * @param string $message The message to log * @param string $level Log level (error, warning, info, debug) * @param array $context Optional context data to include in the log * @return bool True if message was logged, false otherwise */ function vasp_log($message, $level = 'info', $context = []) { $level = strtolower((string) $level); if (!in_array($level, ['debug', 'info', 'warning', 'error'], true)) { $level = 'info'; } if (!vasp_logger_should_log($level)) { return false; } $message = vasp_logger_redact_scalar((string) $message, 'text'); $context = vasp_logger_redact_context(is_array($context) ? $context : []); $context_str = ''; if (!empty($context)) { $encoded = function_exists('wp_json_encode') ? wp_json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $context_str = $encoded !== false ? ' ' . $encoded : ' {"context":"[UNENCODABLE]"}'; } $log_message = sprintf( '[%s] [VALUESPACE] %s%s', strtoupper($level), $message, $context_str ); try { return (bool) error_log($log_message); } catch (Throwable $error) { return false; } } /** * Log an error message * * @param string $message The error message * @param array $context Optional context data * @return bool True if logged successfully */ function vasp_log_error($message, $context = []) { return vasp_log($message, 'error', $context); } /** * Log a warning message * * @param string $message The warning message * @param array $context Optional context data * @return bool True if logged successfully */ function vasp_log_warning($message, $context = []) { return vasp_log($message, 'warning', $context); } /** * Log an info message * * @param string $message The info message * @param array $context Optional context data * @return bool True if logged successfully */ function vasp_log_info($message, $context = []) { return vasp_log($message, 'info', $context); } /** * Log a debug message * * Only logs if WP_DEBUG is enabled * * @param string $message The debug message * @param array $context Optional context data * @return bool True if logged successfully, false if debug is disabled */ function vasp_log_debug($message, $context = []) { return vasp_log($message, 'debug', $context); }