<?php

if (!defined('ABSPATH')) {
    exit;
}

// realtime logging to browser console. use as the last resort. blocking rendering and redirect sometimes
function debug_to_console($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

// logging to error.log with formatting. only one function instead using monolog or other libraries
function logger($message, array $data, $logFile = "error.log"){
    foreach ($data as $key => $val) {
      $message = str_replace("%{$key}%", $val, $message);
    }
    $message .= PHP_EOL;
    return file_put_contents($logFile, $message, FILE_APPEND);
}

function info_logger($message){
    logger("%time% %file% %level% %message%", [
        "time" => current_time('mysql'),
        "level" => "info", 
        "message" => $message, 
        "file" => __FILE__
    ]);
}

function error_logger($message){
    logger("%time% %file% %level% %message%", [
        "time" => current_time('mysql'),
        "level" => "error", 
        "message" => $message, 
        "file" => __FILE__
    ]);
}