use Friendica\Core\Logger\Capabilities\LogChannel;
use Friendica\Core\Logger\Exception\LoggerArgumentException;
use Friendica\Core\Logger\Exception\LoggerException;
+use Friendica\Core\Logger\Exception\LogLevelException;
use Friendica\Core\Logger\Type\StreamLogger as StreamLoggerClass;
use Friendica\Core\Logger\Util\FileSystem;
use Psr\Log\LoggerInterface;
-use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
/**
$fileSystem = new FileSystem();
$logfile = $logfile ?? $config->get('system', 'logfile');
- if ((@file_exists($logfile) && !@is_writable($logfile)) && !@is_writable(basename($logfile))) {
+ if (!@file_exists($logfile) || !@is_writable($logfile)) {
throw new LoggerArgumentException(sprintf('%s is not a valid logfile', $logfile));
}
if (array_key_exists($loglevel, StreamLoggerClass::levelToInt)) {
$loglevel = StreamLoggerClass::levelToInt[$loglevel];
} else {
- $loglevel = StreamLoggerClass::levelToInt[LogLevel::NOTICE];
+ throw new LogLevelException(sprintf('The level "%s" is not valid.', $loglevel));
}
$stream = $fileSystem->createStream($logfile);
namespace Friendica\Core\Logger\Util;
+use Friendica\Core\Logger\Exception\LoggerUnusableException;
+
/**
* Util class for filesystem manipulation for Logger classes
*/
* @param string $file The file
*
* @return string The directory name (empty if no directory is found, like urls)
+ *
+ * @throws LoggerUnusableException
*/
public function createDir(string $file): string
{
restore_error_handler();
if (!$status && !is_dir($dirname)) {
- throw new \UnexpectedValueException(sprintf('Directory "%s" cannot get created: ' . $this->errorMessage, $dirname));
+ throw new LoggerUnusableException(sprintf('Directory "%s" cannot get created: ' . $this->errorMessage, $dirname));
}
return $dirname;
*
* @return resource the open stream resource
*
- * @throws \UnexpectedValueException
+ * @throws LoggerUnusableException
*/
public function createStream(string $url)
{
restore_error_handler();
if (!is_resource($stream)) {
- throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $url));
+ throw new LoggerUnusableException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $url));
}
return $stream;
public function dataHooks(): array
{
return [
- 'normal' => [
+ 'normal' => [
'structure' => $this->structure,
'enabled' => $this->addons,
'files' => [
],
],
],
- 'double' => [
+ 'double' => [
'structure' => $this->structure,
'enabled' => $this->addons,
'files' => [
],
],
],
- 'wrongName' => [
+ 'wrongName' => [
'structure' => $this->structure,
'enabled' => $this->addons,
'files' => [
\Psr\Log\NullLogger::class => [''],
],
],
- \Friendica\Core\Hooks\Capabilities\BehavioralHookType::DECORATOR => [
- \Psr\Log\LoggerInterface::class => [
- \Psr\Log\NullLogger::class,
- ],
- ],
];
EOF,
'addonsArray' => [],
'assertStrategies' => [
[LoggerInterface::class, NullLogger::class, ''],
],
- 'assertDecorators' => [
- [LoggerInterface::class, NullLogger::class],
- ],
],
'normalWithString' => [
'content' => <<<EOF
\Psr\Log\NullLogger::class => '',
],
],
- \Friendica\Core\Hooks\Capabilities\BehavioralHookType::DECORATOR => [
- \Psr\Log\LoggerInterface::class => \Psr\Log\NullLogger::class,
- ],
];
EOF,
'addonsArray' => [],
'assertStrategies' => [
[LoggerInterface::class, NullLogger::class, ''],
],
- 'assertDecorators' => [
- [LoggerInterface::class, NullLogger::class],
- ],
],
'withAddons' => [
'content' => <<<EOF
[LoggerInterface::class, NullLogger::class, ''],
[LoggerInterface::class, NullLogger::class, 'null'],
],
- 'assertDecorators' => [],
],
'withAddonsWithString' => [
'content' => <<<EOF
[LoggerInterface::class, NullLogger::class, ''],
[LoggerInterface::class, NullLogger::class, 'null'],
],
- 'assertDecorators' => [],
],
// This should work because unique name convention is part of the instance manager logic, not of the file-infrastructure layer
'withAddonsDoubleNamed' => [
[LoggerInterface::class, NullLogger::class, ''],
[LoggerInterface::class, NullLogger::class, ''],
],
- 'assertDecorators' => [],
],
'withWrongContentButAddons' => [
'content' => <<<EOF
'assertStrategies' => [
[LoggerInterface::class, NullLogger::class, ''],
],
- 'assertDecorators' => [],
],
];
}
/**
* @dataProvider dataHooks
*/
- public function testSetupHooks(string $content, array $addonsArray, array $assertStrategies, array $assertDecorators)
+ public function testSetupHooks(string $content, array $addonsArray, array $assertStrategies)
{
vfsStream::newFile('static/hooks.config.php')
->withContent($content)
$instanceManager->shouldReceive('registerStrategy')->withArgs($assertStrategy)->once();
}
- foreach ($assertDecorators as $assertDecorator) {
- $instanceManager->shouldReceive('registerDecorator')->withArgs($assertDecorator)->once();
- }
-
$hookFileManager->setupHooks($instanceManager);
self::expectNotToPerformAssertions();
namespace Friendica\Test\src\Core\Logger;
use Friendica\Core\Logger\Exception\LoggerArgumentException;
-use Friendica\Core\Logger\Exception\LoggerException;
use Friendica\Core\Logger\Exception\LogLevelException;
-use Friendica\Core\Logger\Util\FileSystem;
use Friendica\Test\Util\VFSTrait;
use Friendica\Core\Logger\Type\StreamLogger;
use org\bovigo\vfs\vfsStream;
*/
private $logfile;
- /**
- * @var \Friendica\Core\Logger\Util\Filesystem
- */
- private $fileSystem;
-
protected function setUp(): void
{
parent::setUp();
$this->setUpVfsDir();
-
- $this->fileSystem = new FileSystem();
}
/**
* {@@inheritdoc}
*/
- protected function getInstance($level = LogLevel::DEBUG)
+ protected function getInstance($level = LogLevel::DEBUG, $logfile = 'friendica.log')
{
- $this->logfile = vfsStream::newFile('friendica.log')
+ $this->logfile = vfsStream::newFile($logfile)
->at($this->root);
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
+ $this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level)->once();
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, $level);
-
- return $logger;
+ $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
+ return $loggerFactory->create($this->config);
}
/**
public function testNoUrl()
{
$this->expectException(LoggerArgumentException::class);
- $this->expectExceptionMessage("Missing stream URL.");
+ $this->expectExceptionMessage(' is not a valid logfile');
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
+ $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
+ $logger = $loggerFactory->create($this->config);
$logger->emergency('not working');
}
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
+ $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
+ $logger = $loggerFactory->create($this->config);
$logger->emergency('not working');
}
static::markTestIncomplete('We need a platform independent way to set directory to readonly');
- $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
+ $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
+ $logger = $loggerFactory->create($this->config);
$logger->emergency('not working');
}
$this->expectException(LogLevelException::class);
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
- $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('file.text')->once();
-
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, 'NOPE');
+ $logger = $this->getInstance('NOPE');
}
/**
$this->expectException(LogLevelException::class);
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
- $logfile = vfsStream::newFile('friendica.log')
- ->at($this->root);
-
- $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
-
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
+ $logger = $this->getInstance('NOPE');
$logger->log('NOPE', 'a test');
}
- /**
- * Test when the file is null
- */
- public function testWrongFile()
- {
- $this->expectException(LoggerArgumentException::class);
- $this->expectExceptionMessage("A stream must either be a resource or a string.");
-
- $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn(null)->once();
-
- $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
- }
-
/**
* Test a relative path
* @doesNotPerformAssertions