Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit 6d102b9

Browse files
authored
Merge pull request #15 from SebKay/logging
Logging
2 parents e4c1a79 + 4ac87ce commit 6d102b9

11 files changed

+379
-260
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ vendor/
44
coverage/
55
.cache/
66
public/assets/
7+
logs/
78

89
# Files
910
.phpunit.result.cache

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"twig/twig": "^3.1",
2929
"slim/csrf": "^1.0",
3030
"illuminate/database": "^8.27",
31-
"fakerphp/faker": "^1.13"
31+
"fakerphp/faker": "^1.13",
32+
"monolog/monolog": "^2.2"
3233
},
3334
"require-dev": {
3435
"squizlabs/php_codesniffer": "^3.5",

composer.lock

+97-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

+13-38
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
namespace App;
44

5-
use App\Container\Container;
65
use App\Container\ContainerCreator;
76
use App\Database\Database;
8-
use App\Middleware\ExampleMiddleware;
9-
use App\Handlers\HttpErrorHandler;
10-
use App\Handlers\ShutdownHandler;
7+
use App\Dependencies\Logger;
118
use Psr\Http\Message\ResponseInterface;
129
use Psr\Http\Message\ServerRequestInterface;
1310
use Slim\App as SlimApp;
1411
use DI\Bridge\Slim\Bridge as AppFactory;
15-
use Slim\Factory\ServerRequestCreatorFactory;
1612

1713
class App
1814
{
@@ -32,9 +28,9 @@ class App
3228
protected $slim;
3329

3430
/**
35-
* @var HttpErrorHandler
31+
* @var Logger
3632
*/
37-
protected $error_handler;
33+
protected $logger;
3834

3935
/**
4036
* @var Database
@@ -52,6 +48,9 @@ public function __construct()
5248
$this->slim = AppFactory::create($this->container());
5349

5450
$this->container_creator->setup();
51+
52+
$this->logger = $this->container()->get('logger');
53+
5554
$this->setupSlim();
5655

5756
$this->database = new Database();
@@ -94,12 +93,14 @@ protected function addMiddleware(): void
9493
{
9594
$this->slim()->addRoutingMiddleware();
9695

97-
$this->slim()
98-
->addErrorMiddleware($this->dev_mode, false, false)
99-
->setDefaultErrorHandler($this->error_handler);
100-
10196
$this->slim()->add($this->container()->get('csrf'));
102-
// $this->slim()->add(ExampleMiddleware::class);
97+
98+
$this->slim()->addErrorMiddleware(
99+
$this->isDevelopmentMode(),
100+
true,
101+
true,
102+
$this->logger
103+
);
103104
}
104105

105106
/**
@@ -110,37 +111,11 @@ protected function addRoutes(): void
110111
require_once __DIR__ . '/../inc/routes/web.php';
111112
}
112113

113-
/**
114-
* Add error handler
115-
*/
116-
protected function addErrorHandler(): void
117-
{
118-
$this->error_handler = new HttpErrorHandler(
119-
$this->container(),
120-
$this->slim()->getCallableResolver(),
121-
$this->slim()->getResponseFactory()
122-
);
123-
}
124-
125-
/**
126-
* Add shutdown (fatal error) handler
127-
*/
128-
protected function addShutdownHandler(): void
129-
{
130-
$serverRequestCreator = ServerRequestCreatorFactory::create();
131-
$request = $serverRequestCreator->createServerRequestFromGlobals();
132-
$shutdownHandler = new ShutdownHandler($request, $this->error_handler, $this->dev_mode);
133-
134-
\register_shutdown_function($shutdownHandler);
135-
}
136-
137114
/**
138115
* Setup the app (called in the constructor)
139116
*/
140117
protected function setupSlim(): void
141118
{
142-
$this->addErrorHandler();
143-
$this->addShutdownHandler();
144119
$this->addMiddleware();
145120
$this->addRoutes();
146121
}

src/Container/ContainerCreator.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@ public function __construct(App $app)
2626
protected function services(): array
2727
{
2828
return [
29-
new CSRFService($this->container, $this->app),
30-
new ViewService($this->container, $this->app),
29+
LoggerService::class,
30+
CSRFService::class,
31+
ViewService::class
3132
];
3233
}
3334

3435
protected function addServices(): void
3536
{
36-
foreach ($this->services() as $service) {
37-
$this->container->set(
38-
$service->name(),
39-
$service->config()
40-
);
37+
foreach ($this->services() as $service_class) {
38+
$service = new $service_class($this->container(), $this->app);
39+
40+
if ($service instanceof Service) {
41+
$this->container->set(
42+
$service->name(),
43+
$service->config()
44+
);
45+
}
4146
}
4247
}
4348

src/Container/LoggerService.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use App\Dependencies\Logger;
6+
7+
class LoggerService extends Service
8+
{
9+
public static function name(): string
10+
{
11+
return 'logger';
12+
}
13+
14+
public function config(): Logger
15+
{
16+
return new Logger();
17+
}
18+
}

src/Dependencies/Logger.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Dependencies;
4+
5+
use Monolog\Handler\StreamHandler;
6+
use Monolog\Logger as MonologLogger;
7+
use Psr\Log\LoggerInterface;
8+
9+
class Logger implements LoggerInterface
10+
{
11+
/**
12+
* @var MonologLogger
13+
*/
14+
protected $logger;
15+
16+
public function __construct(string $log_path = null)
17+
{
18+
$log_path = ($log_path ?: __DIR__ . '/../../logs/app.log');
19+
20+
$this->logger = new MonologLogger('App');
21+
22+
$this->logger->pushHandler(
23+
new StreamHandler($log_path)
24+
);
25+
}
26+
27+
public function log($level, $message, array $context = [])
28+
{
29+
$this->logger->addRecord($level, $message, $context);
30+
}
31+
32+
public function debug($message, array $context = [])
33+
{
34+
$this->logger->debug($message, $context);
35+
}
36+
37+
public function info($message, array $context = [])
38+
{
39+
$this->logger->info($message, $context);
40+
}
41+
42+
public function notice($message, array $context = [])
43+
{
44+
$this->logger->notice($message, $context);
45+
}
46+
47+
public function warning($message, array $context = [])
48+
{
49+
$this->logger->warning($message, $context);
50+
}
51+
52+
public function error($message, array $context = [])
53+
{
54+
$this->logger->error($message, $context);
55+
}
56+
57+
public function critical($message, array $context = [])
58+
{
59+
$this->logger->critical($message, $context);
60+
}
61+
62+
public function alert($message, array $context = [])
63+
{
64+
$this->logger->alert($message, $context);
65+
}
66+
67+
public function emergency($message, array $context = [])
68+
{
69+
$this->logger->emergency($message, $context);
70+
}
71+
}

0 commit comments

Comments
 (0)