Last active
December 15, 2015 06:39
-
-
Save jedgalbraith/5217588 to your computer and use it in GitHub Desktop.
example pagoda worker error handler
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
# First set the custom error handler at the beginning of the script: | |
# php docs: http://php.net/manual/en/function.set-error-handler.php | |
set_error_handler('workerErrorHandler'); | |
# Next define the workerErrorHandler function: | |
function workerErrorHandler($errno, $errstr, $errfile, $errline) | |
{ | |
if (!(error_reporting() & $errno)) { | |
// This error code is not included in error_reporting | |
return; | |
} | |
$output = "ERROR: [$errno] $errstr\n"; | |
$output .= " FILE: $errfile:$errline\n"; | |
$this->log($output, 'error'); | |
/* Don't execute PHP internal error handler */ | |
return true; | |
} | |
# In my case, I preferred to direct those messages into the same log that my worker was logging to which is defined as: | |
public function log($data, $type) | |
{ | |
$log_levels = $this->config['global']['log_level']; | |
$log_file = $this->config['global']['log_file']; | |
if (in_array($type, $log_levels)) { | |
$log_dir = BASE_DIR."/var/log/pim/" . date('l'); | |
if (!is_dir($log_dir)) { | |
mkdir($log_dir, 0777, true); | |
} | |
$log = "$log_dir/$log_file"; | |
// use existing log file if it's less than a day old | |
if (file_exists($log) && (time() - filemtime($log) < 2000)) { | |
$mode = 'a'; | |
} else { | |
$mode = 'w'; | |
} | |
$fh = fopen($log, $mode) or die("Can't open file at: $log"); | |
$output = "[" . strftime('%T %Y-%m-%d') . "][Mem:" . round((memory_get_usage()/1000000), 0) . " MB] $type: $data\n"; | |
fwrite($fh, $output); | |
fclose($fh); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment