Inject a logger

This commit is contained in:
Jeremy Dormitzer 2019-04-12 22:49:38 -04:00
parent 447cdb3481
commit 440bbfd167
5 changed files with 3220 additions and 3057 deletions

View File

@ -31,6 +31,7 @@
"doctrine/orm": "2.5.14",
"friendica/json-ld": "^1.1",
"guzzlehttp/guzzle": "^6.3",
"monolog/monolog": "^1.0",
"phpseclib/phpseclib": "^2.0",
"psr/http-message": "^1.0",
"symfony/dependency-injection": "^3.4",

6197
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,8 @@
namespace ActivityPub\Config;
use Psr\Log\LoggerInterface;
/**
* The ActivityPubConfig is a data class to hold ActivityPub configuration options
*/
@ -42,6 +44,11 @@ class ActivityPubConfig
*/
private $autoAcceptsFollows;
/**
* @var LoggerInterface
*/
private $logger;
/**
* Don't call this directly - instead, use
* ActivityPubConfig->createBuilder()->build()
@ -57,6 +64,7 @@ class ActivityPubConfig
$this->jsonLdContext = $builder->getJsonLdContext();
$this->idPathPrefix = $builder->getIdPathPrefix();
$this->autoAcceptsFollows = $builder->getAutoAcceptsFollows();
$this->logger = $builder->getLogger();
}
public static function createBuilder()
@ -120,5 +128,13 @@ class ActivityPubConfig
{
return $this->autoAcceptsFollows;
}
/**
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
}

View File

@ -5,6 +5,9 @@ namespace ActivityPub\Config;
use ActivityPub\Objects\ContextProvider;
use ActivityPub\Objects\IdProvider;
use Exception;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
/**
* The ActivityPubConfigBuilder is a builder class to create ActivityPub config data
@ -58,6 +61,11 @@ class ActivityPubConfigBuilder
*/
private $autoAcceptsFollows;
/**
* @var LoggerInterface
*/
private $logger;
/**
* Creates a new ActivityPubConfig instance with default values
*
@ -73,6 +81,7 @@ class ActivityPubConfigBuilder
$this->jsonLdContext = ContextProvider::getDefaultContext();
$this->idPathPrefix = IdProvider::DEFAULT_ID_PATH_PREFIX;
$this->autoAcceptsFollows = false;
$this->logger = $this->getDefaultLogger();
}
/**
@ -273,5 +282,40 @@ class ActivityPubConfigBuilder
$this->autoAcceptsFollows = $autoAcceptsFollows;
return $this;
}
/**
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
/**
* The `logger` is used to log all debug, info, and error messages from the ActivityPub-PHP library.
* This config parameter can be used to customize the log level and log destination via the PSR-3 LoggerInterface
* specification.
*
* Default: a Monolog Logger that prints all messages of level Info or higher to the SAPI logging handler.
* See https://github.com/Seldaek/monolog.
*
* @param LoggerInterface $logger A PSR3 LoggerInterface instance
* @return ActivityPubConfigBuilder The builder instance
*/
public function setLogger( LoggerInterface $logger )
{
$this->logger = $logger;
return $this;
}
/**
* @return Logger
*/
private function getDefaultLogger()
{
$logger = new Logger( 'ActivityPub-PHP' );
$logger->pushHandler( new ErrorLogHandler( ErrorLogHandler::SAPI ) );
return $logger;
}
}

View File

@ -35,6 +35,7 @@ use ActivityPub\Utils\SimpleDateTimeProvider;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;
@ -51,10 +52,20 @@ class ActivityPubModule
*/
private $injector;
/**
* @var ActivityPubConfig
*/
private $config;
public function __construct( ActivityPubConfig $config )
{
$this->config = $config;
$this->injector = new ContainerBuilder;
$this->injector->register( LoggerInterface::class, LoggerInterface::class )
->setFactory( array( $this, 'getLogger' ) );
$dbConfig = Setup::createAnnotationMetadataConfiguration(
array( __DIR__ . '/../Entities' ), $config->getIsDevMode()
);
@ -194,5 +205,13 @@ class ActivityPubModule
{
return $this->injector->get( $id );
}
/**
* @return LoggerInterface
*/
private function getLogger()
{
return $this->config->getLogger();
}
}