From 6083e5c170037e265bb8dacc7a9fcff6782f3971 Mon Sep 17 00:00:00 2001 From: Austen McDonald Date: Wed, 13 Jul 2016 10:14:51 -0700 Subject: [PATCH] Add logger to Game, along with some basic log messages --- .gitignore | 2 ++ logs/.gitignore | 2 ++ phpunit.xml | 1 + src/Bootstrap.php | 30 ++++++++++++++++++++++++------ src/Game.php | 14 +++++++++++++- tests/BootstrapTest.php | 15 ++++++++------- 6 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 logs/.gitignore diff --git a/.gitignore b/.gitignore index 0ef9a9b..a74c59c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ ### Project related vendor/ +logs/* + dbtest.sqlite .sqlite diff --git a/logs/.gitignore b/logs/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/logs/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/phpunit.xml b/phpunit.xml index b0dd2cb..033df62 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,6 +4,7 @@ + diff --git a/src/Bootstrap.php b/src/Bootstrap.php index ba8c83a..2851749 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -15,16 +15,16 @@ use LotGD\Core\Exceptions\InvalidConfigurationException; class Bootstrap { private static $annotationMetaDataDirectories = []; - + public static function registerAnnotationMetaDataDirectory(string $directory) { if (is_dir($directory) === false) { throw new ArgumentException("{$directory} needs to be a valdid directory"); } - + self::$annotationMetaDataDirectories[] = $directory; } - + /** * Create a new Game object, with all the necessary configuration. * @throws InvalidConfigurationException @@ -32,17 +32,35 @@ class Bootstrap */ public static function createGame(): Game { + $logPath = getenv('LOG_PATH'); + if ($logPath === false || strlen($logPath) == 0 || is_dir($logPath) === false) { + throw new InvalidConfigurationException("Invalid or missing log directory: '{$logPath}'"); + } + $cleanedLogPath = realpath($logPath); + $logger = new \Monolog\Logger('lotgd'); + // Add lotgd as the prefix for the log filenames. + $logger->pushHandler(new \Monolog\Handler\RotatingFileHandler($cleanedLogPath . DIRECTORY_SEPARATOR . 'lotgd', 14)); + + $v = Game::getVersion(); + $logger->info("Bootstrap constructing game (Daenerys 🐲 {$v})."); + $dsn = getenv('DB_DSN'); $user = getenv('DB_USER'); $passwd = getenv('DB_PASSWORD'); if ($dsn === false || strlen($dsn) == 0) { - throw new InvalidConfigurationException("Invalid or missing data source name: '{$dsn}'"); + $m = "Invalid or missing data source name: '{$dsn}'"; + $logger->critical($m); + throw new InvalidConfigurationException($m); } if ($user === false || strlen($user) == 0) { + $m = "Invalid or missing database user: '{$user}'"; + $logger->critical($m); throw new InvalidConfigurationException("Invalid or missing database user: '{$user}'"); } if ($passwd === false) { + $m = "Invalid or missing database password: '{$passwd}'"; + $logger->critical($m); throw new InvalidConfigurationException("Invalid or missing database password: '{$passwd}'"); } @@ -54,7 +72,7 @@ class Bootstrap self::$annotationMetaDataDirectories ); $configuration = Setup::createAnnotationMetadataConfiguration($annotationMetaDataDirectories, true); - + // Set a quote $configuration->setQuoteStrategy(new AnsiQuoteStrategy()); @@ -67,6 +85,6 @@ class Bootstrap $eventManager = new EventManager($entityManager); - return new Game($entityManager, $eventManager); + return new Game($entityManager, $eventManager, $logger); } } diff --git a/src/Game.php b/src/Game.php index 1151ad5..33f55aa 100644 --- a/src/Game.php +++ b/src/Game.php @@ -13,13 +13,16 @@ class Game private $eventManager; private $composerManager; private $moduleManager; + private $logger; public function __construct( EntityManagerInterface $entityManager, - EventManager $eventManager) + EventManager $eventManager, + \Monolog\Logger $logger) { $this->entityManager = $entityManager; $this->eventManager = $eventManager; + $this->logger = $logger; } /** @@ -90,4 +93,13 @@ class Game { return $this->character; } + + /** + * Returns the logger instance to write logs. + * @return \Monolog\Logger + */ + public function getLogger(): \Monolog\Logger + { + return $this->logger; + } } diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index 7afcc62..c8cc1ff 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -13,26 +13,27 @@ class BootstrapTest extends \PHPUnit_Framework_TestCase $g = Bootstrap::createGame(); $this->assertNotNull($g->getEntityManager()); $this->assertNotNull($g->getEventManager()); + $this->assertNotNull($g->getLogger()); } - + public function testDoctrineReadsAnnotationsFromAdditionalMetaDataDirectory() { Bootstrap::registerAnnotationMetaDataDirectory(__DIR__ . "/AdditionalEntities"); - + $g = Bootstrap::createGame(); - + $user = new UserEntity(); $user->setName("Monthy"); - + $g->getEntityManager()->persist($user); $g->getEntityManager()->flush(); - + $id = $user->getId(); $this->assertInternalType("int", $id); - + $g->getEntityManager()->clear(); $user = $g->getEntityManager()->getRepository(UserEntity::class)->find($id); - + $this->assertInternalType("int", $user->getId()); $this->assertInternalType("string", $user->getName()); $this->assertSame("Monthy", $user->getName());