Skip to content

Commit

Permalink
Allow psr/log v1-v3 usage
Browse files Browse the repository at this point in the history
For that, remove the development dependency on wa72/simplelogger. Also
add a psr/log dependency which was previously only indirectly given by
the dependency on carddavclient.
  • Loading branch information
mstilkerich committed Oct 22, 2022
1 parent 8625235 commit 62413aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -25,14 +25,14 @@
"ext-pdo": "*",
"sabre/vobject": "^3.3.5 || ^4.0.0",
"roundcube/plugin-installer": "^0.3.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"mstilkerich/carddavclient": "dev-master"
},
"require-dev": {
"vimeo/psalm": ">= 3.11",
"phpcompatibility/php-compatibility": "*",
"phpunit/phpunit": "~9",
"phpunit/phpcov": "*",
"wa72/simplelogger": "^1.1",
"dealerdirect/phpcodesniffer-composer-installer": ">= 0.7.0",
"aodto/phasher": "dev-master",
"psalm/plugin-phpunit": "^0.15.0"
Expand Down
2 changes: 1 addition & 1 deletion src/RoundcubeLogger.php
Expand Up @@ -103,7 +103,7 @@ public function setLogLevel(string $loglevel): void
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, $message, array $context = array()): void
{
if (is_string($level) && isset(self::LOGLEVELS[$level])) {
if (self::LOGLEVELS[$level] >= $this->loglevel) {
Expand Down
23 changes: 14 additions & 9 deletions tests/TestLogger.php
Expand Up @@ -59,20 +59,22 @@ class TestLogger extends AbstractLogger
LogLevel::EMERGENCY => "EMG"
];

/** @var LoggerInterface Logger object used to store log messages produced during the tests */
private $fileLogger;
/** @var resource $logh File handle to the log file */
private $logh;

/** @var string[][] In-Memory buffer of log messages to assert log messages */
private $logBuffer = [];

public function __construct()
public function __construct(string $logFileName = 'test.log')
{
$logfile = "testreports/{$GLOBALS['TEST_TESTRUN']}/test.log";
if (file_exists($logfile)) {
unlink($logfile);
$logfile = "testreports/{$GLOBALS['TEST_TESTRUN']}/$logFileName";
$logh = fopen($logfile, 'w');

if ($logh === false) {
throw new \Exception("could not open log file: $logfile");
}

$this->fileLogger = new \Wa72\SimpleLogger\FileLogger($logfile, \Psr\Log\LogLevel::DEBUG);
$this->logh = $logh;
}

/**
Expand All @@ -86,6 +88,7 @@ public function __construct()
public function __destruct()
{
$this->reset();
fclose($this->logh);
}

/**
Expand All @@ -96,14 +99,16 @@ public function __destruct()
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, $message, array $context = array()): void
{
TestCase::assertIsString($level);
TestCase::assertNotNull(self::LOGLEVELS[$level]);

$levelNumeric = self::LOGLEVELS[$level];
$levelShort = self::LOGLEVELS_SHORT[$level];
$this->fileLogger->log($level, "[$levelNumeric $levelShort] $message", $context);

// interpolation of context placeholders is not implemented
fprintf($this->logh, "[%s]: %s\n", date('Y-m-d H:i:s'), "[$levelNumeric $levelShort] $message");

// only warnings or more critical messages are interesting for testing
if (self::LOGLEVELS[$level] >= self::LOGLEVELS[LogLevel::WARNING]) {
Expand Down

0 comments on commit 62413aa

Please sign in to comment.