Skip to content

Commit 7f6485f

Browse files
author
Tim Helfensdörfer
authored
Merge pull request #53 from FabianTe/feat/psr-logger-interface
Introduce PSR 3 for logging
2 parents 06394df + d93d655 commit 7f6485f

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php": ">=7.2.0",
1717
"desarrolla2/cache": "^3.0",
1818
"monolog/monolog": "^2.1",
19-
"composer/semver": "^3.0"
19+
"composer/semver": "^3.0",
20+
"psr/log": "1.1.4"
2021
},
2122
"require-dev": {
2223
"roave/security-advisories": "dev-master",

example/client/update/index.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
// Replace with your server update directory
1010
$update->setUpdateUrl('http://127.0.0.1:8090/example/server');
1111

12-
// Log handler and cache are optional
13-
$update->addLogHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/update.log'));
12+
// Custom logger (optional)
13+
$logger = new \Monolog\Logger("default");
14+
$logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/update.log'));
15+
$update->setLogger($logger);
1416

17+
// Cache (optional but recommended)
1518
$cache = new Desarrolla2\Cache\File(__DIR__ . '/cache');
1619
$update->setCache($cache, 3600);
1720

src/AutoUpdate.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
use Composer\Semver\Comparator;
1010
use Desarrolla2\Cache\CacheInterface;
1111
use Desarrolla2\Cache\NotCache;
12-
use Monolog\Handler\HandlerInterface;
1312
use Monolog\Logger;
14-
use Monolog\Handler\NullHandler;
13+
use Psr\Log\LoggerInterface;
1514
use Psr\SimpleCache\InvalidArgumentException;
1615
use VisualAppeal\Exceptions\DownloadException;
1716
use VisualAppeal\Exceptions\ParserException;
@@ -44,7 +43,7 @@ class AutoUpdate {
4443
/**
4544
* Logger instance.
4645
*
47-
* @var Logger
46+
* @var LoggerInterface
4847
*/
4948
private $log;
5049

@@ -199,7 +198,6 @@ public function __construct(?string $tempDir = null, ?string $installDir = null,
199198
{
200199
// Init logger
201200
$this->log = new Logger('auto-update');
202-
$this->log->pushHandler(new NullHandler());
203201

204202
$this->setTempDir($tempDir ?? (__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR));
205203
$this->setInstallDir($installDir ?? (__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR));
@@ -364,14 +362,14 @@ private function useBasicAuth()
364362
}
365363

366364
/**
367-
* Add a new logging handler.
365+
* Replace the logger internally used by the given logger instance.
368366
*
369-
* @param HandlerInterface $handler See https://github.com/Seldaek/monolog
367+
* @param LoggerInterface $logger
370368
* @return AutoUpdate
371369
*/
372-
public function addLogHandler(HandlerInterface $handler): AutoUpdate
370+
public function setLogger(LoggerInterface $logger): AutoUpdate
373371
{
374-
$this->log->pushHandler($handler);
372+
$this->log = $logger;
375373

376374
return $this;
377375
}

tests/AutoUpdateTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ protected function setUp(): void
2525
$this->_update = new AutoUpdate(__DIR__ . DIRECTORY_SEPARATOR . 'temp', __DIR__ . DIRECTORY_SEPARATOR . 'install');
2626
$this->_update->setCurrentVersion('0.1.0');
2727
$this->_update->setUpdateUrl(__DIR__ . DIRECTORY_SEPARATOR . 'fixtures');
28-
$this->_update->addLogHandler(new Monolog\Handler\StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'update.log'));
28+
$logger = new Monolog\Logger("default");
29+
$logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'update.log'));
30+
$this->_update->setLogger($logger);
2931
}
3032

3133
/**

0 commit comments

Comments
 (0)