Skip to content

Instantly share code, notes, and snippets.

@Boorj
Created February 16, 2019 08:11
Show Gist options
  • Save Boorj/f364671c9adc4ad93b9b2f1d868b5b0b to your computer and use it in GitHub Desktop.
Save Boorj/f364671c9adc4ad93b9b2f1d868b5b0b to your computer and use it in GitHub Desktop.
debug_log - utility for printing to debug output
<?php
/**
* debug_log
* @param {*} $data
* @param {string} $d = "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough"
* */
function debug_log($data, $type = ''){
$patterns = [
"black" => "str",
"red" => "str",
"green" => "str",
"yellow" => "str",
"blue" => "str",
"magenta" => "str",
"cyan" => "str",
"white" => "str",
"gray" => "str",
"bold" => "str",
"dim" => "str",
"italic" => "str",
"underline" => "str",
"inverse" => "str",
"hidden" => "str",
"strikethrough" => "str",
];
if (!is_string($data) && !is_numeric($data)) {
$data = json_encode($data, JSON_PRETTY_PRINT);
}
if ($type && isset($patterns[$type])) {
$pattern = $patterns[$type];
$data = str_replace('str', $data, $pattern);
}
file_put_contents('php://stderr', $data);
file_put_contents('php://stderr', "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment