From a244f0d0361b9efc137fc06da89ee2f6848edbb3 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 17 Feb 2023 12:27:45 +0100 Subject: [PATCH] Fix #50: Forward transport logs to the Yii Logger --- .github/workflows/build.yml | 7 +++++-- CHANGELOG.md | 3 ++- composer.json | 3 +++ src/Mailer.php | 10 +++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e8c25a3..1cd306b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -32,10 +32,13 @@ jobs: uses: actions/cache@v1 with: path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- + key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer-${{ matrix.php }}- - name: Install dependencies run: composer update $DEFAULT_COMPOSER_FLAGS + - name: Install logging proxy + if: ${{ matrix.php != '7.4' }} + run: composer require "yiisoft/yii2-psr-log-source" - name: Run unit tests with coverage run: vendor/bin/phpunit --coverage-text - name: Static analysis (Psalm) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a0d23b..12c3648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ Yii Framework 2 Symfony mailer extension Change Log ================================================ -3.0.1 under development +3.1.0 under development ----------------------- - Enh #45: Added option to create transport from Dsn object (Swanty) +- Enh #50: Forward transport logs to the Yii Logger (sammousa) 3.0.0 December 05, 2022 diff --git a/composer.json b/composer.json index f54ece8..8997137 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,9 @@ "captainhook/captainhook": "^5.10", "captainhook/plugin-composer": "^5.3" }, + "suggest": { + "yiisoft/yii2-psr-log-source": "Allows routing transport logs to your Yii2 logger" + }, "repositories": [ { "type": "composer", diff --git a/src/Mailer.php b/src/Mailer.php index 80f35de..1e23542 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -8,6 +8,7 @@ namespace yii\symfonymailer; +use Psr\Log\LoggerInterface; use Symfony\Component\Mailer\Mailer as SymfonyMailer; use Symfony\Component\Mailer\Transport; use Symfony\Component\Mailer\Transport\Dsn; @@ -16,6 +17,8 @@ use yii\base\InvalidArgumentException; use yii\base\InvalidConfigException; use yii\mail\BaseMailer; +use yii\psr\DynamicLogger; +use yii\psr\Logger; /** * @psalm-suppress PropertyNotSetInConstructor @@ -93,7 +96,12 @@ private function getTransportFactory(): Transport if (isset($this->transportFactory)) { return $this->transportFactory; } - $defaultFactories = Transport::getDefaultFactories(); + /** @var LoggerInterface|null $logger */ + $logger = class_exists(DynamicLogger::class) ? new DynamicLogger() : null; + /** + * @psalm-suppress TooManyArguments On PHP 7.4 symfony/mailer 5.4 is uses which uses func_get_args instead of real args + */ + $defaultFactories = Transport::getDefaultFactories(null, null, $logger); /** @psalm-suppress InvalidArgument Symfony's type annotation is wrong */ return new Transport($defaultFactories); }