TIP게시판

제목 에러 핸들러
글쓴이 카카메론바 작성시각 2017/01/26 13:31:02
댓글 : 1 추천 : 0 스크랩 : 1 조회수 : 10356   RSS

packagist.org 에 등록된 훌륭한 에러 핸들링이 있음에도 불구하고

굳이 에러 핸들링을 별도로 만드셔야 하거나, 필요한 기능만을 담고 싶으실때 소스를 참고하시면 될듯 합니다.

필요한곳이 있어서 단순하게 작성하였으니, 적용하실분들은 입맛에 맞게 뚝딱뚝딱 하셔서 사용하시면 될것 같아요^^

모두 즐거운 설명절 되세요~~~

<?php

error_reporting(0);
set_error_handler('errorHandler', E_ALL & ~E_NOTICE);
set_exception_handler('exceptionHandler');
register_shutdown_function('fatalErrorHandler');

function sendError($msg) {
    $_eh_mode = 'file';
    $_eh_logfile = "./logs/error_log";
    file_put_contents($_eh_logfile, $msg, FILE_APPEND);
    // 저는 이곳에 메신저로 에러 로그를 보내도록 했어요;; 그게 필요해서 ㅠ
}
function generateErrorLog($msg, $file = NULL, $line = NULL) {
    $content .= "[UID]\t".md5(uniqid().time().$_SERVER['REMOTE_ADDR'])."\n";
    $content .= "[날짜]\t" . date('Y-m-d H:i:s') . "\n";
    $content .= "[URL]\t{$_SERVER['REQUEST_URI']}\n";
    $content .= "[IP]\t{$_SERVER['REMOTE_ADDR']}\n";
    if (!is_null($file)) {
        $content .= "[파일]\t{$file}\n";
    }
    if (!is_null($line)) {
        $content .= "[라인]\t{$line}\n";
    }
    $content .= "[내용]\t{$msg}\n\n---\n";
    return $content;
}
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext = null) {
    $content = generateErrorLog($errstr, $errfile, $errline);
    sendError($content);
}
function exceptionHandler($e) {
    $content = generateErrorLog($e->getMessage(), $e->getFile(), $e->getLine());
    sendError($content);
}
function fatalErrorHandler() {
    $error = error_get_last();
    if ($error['type'] === E_ERROR) {
        $str = explode('Stack trace:', $error['message']);
        errorHandler($error['type'], $str[0], $error['file'], $error['line']);
        http_response_code(500);
    }
}

 

 다음글 Codeigniter와 Mailgun 쉽..게? 연동하... (1)
 이전글 CI 3.1.3 버전에서 HMVC 에러 (1)

댓글

변종원(웅파) / 2017/01/27 13:10:15 / 추천 0
즐거운 명절 되세요. ^^