<?php
// includes/logging.php
defined('ABSPATH') || exit;

if (!function_exists('blog_maker_log')) {
    function blog_maker_log($message, $context = null) {
        // Use BLOG_MAKER_LOG constant (set in constants.php) or fallback
        $log_file = defined('BLOG_MAKER_LOG') ? BLOG_MAKER_LOG : (BLOG_MAKER_DIR . '/blog-maker.log');

        $line = '[' . date('Y-m-d H:i:s') . '] ' . (string)$message;

        if ($context !== null) {
            if (is_scalar($context)) {
                $line .= ' | context=' . (string)$context;
            } else {
                $json = function_exists('wp_json_encode') ? wp_json_encode($context) : json_encode($context);
                $line .= ' | context=' . substr((string)$json, 0, 2000);
            }
        }

        $line .= PHP_EOL;
        @file_put_contents($log_file, $line, FILE_APPEND);
    }
}
