Skip to content

Commit

Permalink
Merge pull request #27 from jdkfx/dev/#25
Browse files Browse the repository at this point in the history
✨ ログ出力機能の追加
  • Loading branch information
jdkfx authored Feb 15, 2022
2 parents 63ed8d7 + e69c0f7 commit 981d07d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
.log
18 changes: 18 additions & 0 deletions src/app/Logs/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Logs;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class Log
{
public function __construct(){}

public function writeLog($name, $message) {
$log = new Logger($name);

$log->pushHandler(new StreamHandler(__DIR__ . '/../../log/' . $name . '.log', Logger::INFO));
$log->info($message);
}
}
10 changes: 5 additions & 5 deletions src/app/Routers/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Routers;

use App\Templates\View;
use App\Logs\Log;

class Router
{
Expand All @@ -24,18 +25,17 @@ public function response($request)
$controller->$controllerAction();
} else if ($this->routes[$request]['method'] === 'POST') {
$controller->$controllerAction($_POST);
} else {
throw new \Exception('error!');
}

} else {
$view = new View();
header("HTTP/1.1 404 Not Found");
$view->pages('error');
exit;

throw new \Exception('error!');
}
} catch (\Exception $e) {
error_log($e->getFile() . $e->getLine() . $e->getMessage());
$logger = new Log();
$logger->writeLog('error', $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage());
}
}
}
1 change: 1 addition & 0 deletions src/log/error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2022-02-15T10:58:52.083263+00:00] error.INFO: /var/www/app/Routers/Router.php 34 error! [] []

0 comments on commit 981d07d

Please sign in to comment.