symfony 3.4で述べたように、MonologBundle
と他のすべてのサービスによって提供されるlogger
サービスは、デフォルトでプライベートに設定されています。問題を回避するには[sic]
、推奨される方法は、依存性の注入を使用することです。 http://symfony.com/doc/3.4/logging.html
namespace AppBundle/Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction(LoggerInterface $logger)
{
$logger->info('Your Message');
}
}
ソースコードのリファレンス:autowire
が有効になっている場合に依存性の注入が可能ですhttps://github.com/symfony/monolog-bundle/blob/v3.1.0/Resources/config/monolog.xml#L17
サービスの定義について。 [sic]
#app/config/services.yml
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
#optionally declare individual service as public
#AppBundle\Service\MyService:
# public: true
#alternatively declare the namespace explicitly as public
#AppBundle\Service\:
# resource: '../../src/AppBundle/Service/*'
# public: true
は、その後のサービスに依存関係を注入するために、あなたは、コンストラクタの引数の型ヒントを追加します。
namespace AppBundle\Service;
use Psr\Log\LoggerInterface;
class MyService
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
autowire
が無効になっている場合は、手動でロガーエイリアスを注入するために、あなたのサービスを定義することができます。また
#app/config/services.yml
services:
AppBundle\Service\MyService:
arguments: ['@logger']
public: true
は、コンテナから公的にアクセスできるようにロガーエイリアスを強制するために、あなたのアプリケーションサービスの設定でサービス別名を再宣言することができます。
#app/config/services.yml
services:
#...
logger:
alias: 'monolog.logger'
public: true
あなたのコードなし特にお手伝いできないものの、エラーメッセージが間違っていることを伝えています。 Symfonyサービスと依存性注入は、3.2と3.4の間で大幅に変化しました。https://symfony.com/doc/current/service_container/3.3-di-changes.html – MEmerson
ああ、申し訳ありませんがコードで私はロガーサービスとコール情報メソッドを取得 –
そうしないでください。ロガーをどこに引っ張っているのかを示す少しのコードで質問を更新することを検討してください。そしておそらく私たちは変化を提案することができます。 – Cerad