diff --git a/f_config/config.php b/f_config/config.php index cb0f282c..2bee441d 100644 --- a/f_config/config.php +++ b/f_config/config.php @@ -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; } diff --git a/f_install/sql/err_log.sql b/f_install/sql/err_log.sql index cecd3a44..c12c2f89 100644 --- a/f_install/sql/err_log.sql +++ b/f_install/sql/err_log.sql @@ -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; diff --git a/src/sammo/FileDB.php b/src/sammo/FileDB.php index 6ebbd92e..378f507f 100644 --- a/src/sammo/FileDB.php +++ b/src/sammo/FileDB.php @@ -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; } } \ No newline at end of file