Compare commits
20 Commits
b253e4958e
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| bd2fe57682 | |||
| b67436c44f | |||
| 18deba3b66 | |||
| 713ba001e5 | |||
| 3872996fa7 | |||
| fecc73408a | |||
| 03a338b98e | |||
| 6bb1d8fc8f | |||
| aacb6a5462 | |||
| a8d0667c50 | |||
| 23b22556e7 | |||
| e00189b590 | |||
| ee140d7e6d | |||
| 28fa24f67e | |||
| 0fe00bf368 | |||
| e1aef5107c | |||
| 09dd5847ec | |||
| 3fd4ed527d | |||
| f8e45defbb | |||
| 56a855298a |
@@ -0,0 +1,3 @@
|
||||
composer.lock
|
||||
/nbproject/
|
||||
/vendor/
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "potter/logger",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Potter\\Logger\\": "src/Potter/Logger/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jay Potter",
|
||||
"email": "j@ypotter.ca"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"graham-campbell/result-type": ">=1.1.4",
|
||||
"monolog/monolog": ">=3.10.0",
|
||||
"phpoption/phpoption": ">=1.9.5",
|
||||
"potter/console": ">=0.0.1",
|
||||
"potter/container": ">=0.0.1",
|
||||
"potter/event": ">=0.0.1",
|
||||
"psr/log": ">=3.0.2",
|
||||
"symfony/polyfill-ctype": ">=1.37.0",
|
||||
"symfony/polyfill-mbstring": ">=1.38.2",
|
||||
"symfony/polyfill-php80": ">=1.37.0",
|
||||
"vlucas/phpdotenv": ">=5.6.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Potter\Logger;
|
||||
|
||||
use \Potter\Container;
|
||||
use \Potter\Event\Event;
|
||||
|
||||
trait Behaviour
|
||||
{
|
||||
private static function addLogger(Container $container): void
|
||||
{
|
||||
$container->addShared(Logger::class)
|
||||
->addArgument(static::class)
|
||||
->addArgument(self::getLoggerHandlers($container));
|
||||
}
|
||||
|
||||
private static function addLoggerBehaviour(Container $container): void
|
||||
{
|
||||
self::addLoggerEnvironment($container);
|
||||
self::addLoggerHandler($container);
|
||||
self::addLogger($container);
|
||||
self::addLoggerSubscription($container);
|
||||
}
|
||||
|
||||
private static function addLoggerEnvironment(Container $container): void
|
||||
{
|
||||
static::getEnvironment($container)->required('APPLICATION_NAME');
|
||||
}
|
||||
|
||||
private static function addLoggerHandler(Container $container): void
|
||||
{
|
||||
$container->addShared(Handler::class)
|
||||
->addArgument(\Potter\Console\Logger::class);
|
||||
}
|
||||
|
||||
private static function addLoggerSubscription(Container $container): void
|
||||
{
|
||||
$dispatcher = static::getDispatcher($container);
|
||||
|
||||
$dispatcher->subscribeTo('applicationStart',
|
||||
function (Event $event) {
|
||||
self::getLogger(
|
||||
$event->getArgument('container'))
|
||||
->notice('Application start: ' . $_ENV['APPLICATION_NAME']); });
|
||||
|
||||
$dispatcher->subscribeTo('applicationStop',
|
||||
function (Event $event) {
|
||||
self::getLogger(
|
||||
$event->getArgument('container'))
|
||||
->notice('Application stop: ' . $_ENV['APPLICATION_NAME']); });
|
||||
}
|
||||
|
||||
abstract private static function getDispatcher(Container $container): \Potter\Event\Dispatcher;
|
||||
|
||||
abstract private static function getEnvironment(Container $container): \Dotenv\Dotenv;
|
||||
|
||||
private static function getLogger(Container $container): Logger
|
||||
{
|
||||
return $container->get(Logger::class);
|
||||
}
|
||||
|
||||
private static function getLoggerHandlers(Container $container): array
|
||||
{
|
||||
return [$container->get(Handler::class)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Potter\Logger;
|
||||
|
||||
final class Handler extends \Monolog\Handler\PsrHandler
|
||||
{ }
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Potter\Logger;
|
||||
|
||||
final class Logger extends \Monolog\Logger
|
||||
{ }
|
||||
Reference in New Issue
Block a user