Add logger to Game, along with some basic log messages

This commit is contained in:
Austen McDonald
2016-07-13 10:14:51 -07:00
parent 50879ec6f4
commit 6083e5c170
6 changed files with 50 additions and 14 deletions
+2
View File
@@ -1,6 +1,8 @@
### Project related
vendor/
logs/*
dbtest.sqlite
.sqlite
+2
View File
@@ -0,0 +1,2 @@
*
!.gitignore
+1
View File
@@ -4,6 +4,7 @@
<env name="DB_USER" value="root" />
<env name="DB_PASSWORD" value="" />
<env name="DB_NAME" value="daenerys" />
<env name="LOG_PATH" value="./logs" />
</php>
<testsuites>
<testsuite name="All">
+24 -6
View File
@@ -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);
}
}
+13 -1
View File
@@ -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;
}
}
+8 -7
View File
@@ -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());