Fix logger classes and tests

This commit is contained in:
Philipp 2023-07-17 01:06:11 +02:00
parent 903ecc2a76
commit bca6abf4ab
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
5 changed files with 28 additions and 72 deletions

View file

@ -25,10 +25,10 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
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;
/**
@ -55,7 +55,7 @@ class StreamLogger extends AbstractLoggerTypeFactory
$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));
}
@ -64,7 +64,7 @@ class StreamLogger extends AbstractLoggerTypeFactory
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);

View file

@ -21,6 +21,8 @@
namespace Friendica\Core\Logger\Util;
use Friendica\Core\Logger\Exception\LoggerUnusableException;
/**
* Util class for filesystem manipulation for Logger classes
*/
@ -37,6 +39,8 @@ class FileSystem
* @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
{
@ -57,7 +61,7 @@ class FileSystem
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;
@ -75,7 +79,7 @@ class FileSystem
*
* @return resource the open stream resource
*
* @throws \UnexpectedValueException
* @throws LoggerUnusableException
*/
public function createStream(string $url)
{
@ -89,7 +93,7 @@ class FileSystem
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;