Created
June 11, 2021 15:05
-
-
Save M1TKO/11d1e1a03714669dabfe7152de8993e1 to your computer and use it in GitHub Desktop.
Simple logger for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Put this function in the main directory of your project | |
# Logs the messages in the php error log file | |
function log_write($logMsg, $printR = false, $useNewLines = true) { | |
$debug = debug_backtrace(); | |
$file = str_replace(dirname(__FILE__), '', $debug[0]['file'] ?? ''); | |
$line = $debug[0]['line'] ?? 'NULL'; | |
$addition = ''; | |
if (!is_string($logMsg)) { | |
if (is_array($logMsg) && $printR) { | |
$logMsg = print_r($logMsg, true); | |
} else { | |
$addition = '| JSON ENCODED'; | |
$logMsg = json_encode($logMsg); | |
} | |
} | |
$newLine = $useNewLines ? PHP_EOL : ''; | |
error_log( | |
"DEBUG LOG: line $line in $file $addition $newLine" . $logMsg, | |
0 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment