에러 로그용 클래스 설정

This commit is contained in:
2018-07-23 22:50:22 +09:00
parent 9c666bf7a9
commit 30793e68df
3 changed files with 30 additions and 24 deletions
+28 -21
View File
@@ -49,40 +49,47 @@ function getFriendlyErrorType($type)
return "{$type}";
}
function logError(string $err, string $errstr, array $trace){
$fdb = FileDB::db(ROOT.'/d_log/err_log.sqlite3', ROOT.'/f_install/sql/err_log.sql');
$date = date("Ymd_His");
$trace = array_map(function(string $text){
return str_replace(ROOT, '{ROOT}', $text);
}, $trace);
$fdb->insert('err_log', [
'date'=>$date,
'err'=>getFriendlyErrorType($errno),
'errstr'=>$errstr,
'trace'=>Json::encode($trace)
]);
}
function logErrorByCustomHandler(int $errno, string $errstr, string $errfile, int $errline, array $errcontext){
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
$date = date("Ymd_His");
$e = new \Exception();
$data = Json::encode([
'date'=>$date,
'err'=>getFriendlyErrorType($errno),
'errstr'=>$errstr,
'trace'=>explode("\n", $e->getTraceAsString())
], Json::PRETTY);
file_put_contents(ROOT.'/d_log/err_log.txt',"$data\n", FILE_APPEND);
logError(
getFriendlyErrorType($errno),
$errstr,
explode("\n", $e->getTraceAsString())
);
}
set_error_handler("\sammo\logErrorByCustomHandler");
function logExceptionByCustomHandler(\Throwable $e){
$date = date("Ymd_His");
$data = Json::encode([
'date'=>$date,
'err'=>get_class($e),
'errstr'=>$e->getMessage(),
'trace'=>explode("\n", $e->getTraceAsString())
], Json::PRETTY);
file_put_contents(ROOT.'/d_log/err_log.txt',"$data\n", FILE_APPEND);
logError(
get_class($e),
$e->getMessage(),
explode("\n", $e->getTraceAsString())
);
echo $e->getTraceAsString();
throw $e;
}
-2
View File
@@ -1,4 +1,3 @@
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `err_log` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`date` TEXT NOT NULL,
@@ -9,4 +8,3 @@ CREATE TABLE IF NOT EXISTS `err_log` (
CREATE INDEX IF NOT EXISTS `date` ON `err_log` (
`date` DESC
);
COMMIT;
+2 -1
View File
@@ -5,7 +5,7 @@ namespace sammo;
use \Medoo\Medoo;
class FileDB{
public static function DB(string $path, ?string $schemaPath = null) : Medoo{
public static function db(string $path, ?string $schemaPath = null) : Medoo{
//Note: reference count 적용. MySQL의 것과 다르게 동작함.
$db = new Medoo([
'database_type' => 'sqlite',
@@ -14,6 +14,7 @@ class FileDB{
if($schemaPath){
$db->query(\file_get_contents($schemaPath));
}
return $db;
}
}