Skip to content

Instantly share code, notes, and snippets.

@M1TKO
Created June 11, 2021 15:05
Show Gist options
  • Save M1TKO/11d1e1a03714669dabfe7152de8993e1 to your computer and use it in GitHub Desktop.
Save M1TKO/11d1e1a03714669dabfe7152de8993e1 to your computer and use it in GitHub Desktop.
Simple logger for PHP
<?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