에러 로그용 클래스 설정
This commit is contained in:
+28
-21
@@ -49,40 +49,47 @@ function getFriendlyErrorType($type)
|
|||||||
return "{$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){
|
function logErrorByCustomHandler(int $errno, string $errstr, string $errfile, int $errline, array $errcontext){
|
||||||
if (!(error_reporting() & $errno)) {
|
if (!(error_reporting() & $errno)) {
|
||||||
// This error code is not included in error_reporting, so let it fall
|
// This error code is not included in error_reporting, so let it fall
|
||||||
// through to the standard PHP error handler
|
// through to the standard PHP error handler
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$date = date("Ymd_His");
|
|
||||||
$e = new \Exception();
|
$e = new \Exception();
|
||||||
|
|
||||||
$data = Json::encode([
|
logError(
|
||||||
'date'=>$date,
|
getFriendlyErrorType($errno),
|
||||||
'err'=>getFriendlyErrorType($errno),
|
$errstr,
|
||||||
'errstr'=>$errstr,
|
explode("\n", $e->getTraceAsString())
|
||||||
'trace'=>explode("\n", $e->getTraceAsString())
|
);
|
||||||
], Json::PRETTY);
|
|
||||||
|
|
||||||
file_put_contents(ROOT.'/d_log/err_log.txt',"$data\n", FILE_APPEND);
|
|
||||||
}
|
}
|
||||||
set_error_handler("\sammo\logErrorByCustomHandler");
|
set_error_handler("\sammo\logErrorByCustomHandler");
|
||||||
|
|
||||||
|
|
||||||
function logExceptionByCustomHandler(\Throwable $e){
|
function logExceptionByCustomHandler(\Throwable $e){
|
||||||
|
logError(
|
||||||
$date = date("Ymd_His");
|
get_class($e),
|
||||||
|
$e->getMessage(),
|
||||||
$data = Json::encode([
|
explode("\n", $e->getTraceAsString())
|
||||||
'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);
|
|
||||||
echo $e->getTraceAsString();
|
echo $e->getTraceAsString();
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
BEGIN TRANSACTION;
|
|
||||||
CREATE TABLE IF NOT EXISTS `err_log` (
|
CREATE TABLE IF NOT EXISTS `err_log` (
|
||||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
`date` TEXT NOT NULL,
|
`date` TEXT NOT NULL,
|
||||||
@@ -9,4 +8,3 @@ CREATE TABLE IF NOT EXISTS `err_log` (
|
|||||||
CREATE INDEX IF NOT EXISTS `date` ON `err_log` (
|
CREATE INDEX IF NOT EXISTS `date` ON `err_log` (
|
||||||
`date` DESC
|
`date` DESC
|
||||||
);
|
);
|
||||||
COMMIT;
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace sammo;
|
|||||||
use \Medoo\Medoo;
|
use \Medoo\Medoo;
|
||||||
|
|
||||||
class FileDB{
|
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의 것과 다르게 동작함.
|
//Note: reference count 적용. MySQL의 것과 다르게 동작함.
|
||||||
$db = new Medoo([
|
$db = new Medoo([
|
||||||
'database_type' => 'sqlite',
|
'database_type' => 'sqlite',
|
||||||
@@ -14,6 +14,7 @@ class FileDB{
|
|||||||
if($schemaPath){
|
if($schemaPath){
|
||||||
$db->query(\file_get_contents($schemaPath));
|
$db->query(\file_get_contents($schemaPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user