Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created August 4, 2016 16:14
Show Gist options
  • Save ukautz/b7794d7761d9b5092bea4d7950788d48 to your computer and use it in GitHub Desktop.
Save ukautz/b7794d7761d9b5092bea4d7950788d48 to your computer and use it in GitHub Desktop.
CodeIgniter 3 - Delegate exceptions and errors to Logentries
<?php
// application/config/hooks.php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| https://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook['pre_system'] = function() {
require_once __DIR__ . '/../../vendor/autoload.php';
$logentriesToken = '{{your-logentries-token}}';
$monolog = new \Monolog\Logger("MyApp");
$monolog->pushHandler(new \Logentries\Handler\LogentriesHandler($logentriesToken));
set_error_handler(function($no, $str, $file, $line, $context) use ($monolog) {
// delegate to logentries
try {
switch ($no) {
case E_USER_NOTICE:
$monolog->info("$str [file: $file, line: $line]");
break;
default:
$monolog->error("$str [file: $file, line: $line]");
break;
}
} catch (\Exception $e) {
error_log("FAILED to delegate error to logentries: {$e->getMessage()}");
}
// call the original error handler
_error_handler($no, $str, $file, $line, $context);
});
set_exception_handler(function($exception) use ($monolog) {
// delegate to logentries
try {
$monolog->error(get_class($exception). ": ". $exception->getMessage());
} catch (\Exception $e) {
error_log("FAILED to delegate exception to logentries: {$e->getMessage()}");
}
// call original exception handler
_exception_handler($exception);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment