これが古い投稿であることは知っていますが、symfony/monolog-bundle 2.1.x を使用して同様のニーズに遭遇しました。他のスレッドで必要なものを正確に見つけることができなかったので、カスタム チャネルを使用するロガー コンテナーを作成するという解決策をここに文書化します。
config.yml で
monolog:
handlers:
user_actions:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%-user-actions.log"
level: info
channels: [user_actions]
私のバンドルの services.yml で
acme.logger.user_actions:
class: Acme\MyBundle\Monolog\UserActionsLogger
arguments: ['@logger']
tags:
- { name: monolog.logger, channel: user_actions }
src/Acme/MyBundle/Monolog/UserActionsLogger.php 内
<?php
namespace Acme\MyBundle\Monolog;
class UserActionsLogger
{
public $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
}
次に、ロガー コンテナーを別のサービスに挿入できます。
acme.user.authenticationhandler:
class: %acme.user.authenticationhandler.class%
public: false
arguments: ['@router', '@security.context', '@acme.logger.user_actions']
または、任意のコントローラーでロガー コンテナーをサービスとして選択的に使用することもできます。
$userActionsLogger = $this->get('acme.logger.user_actions');
次に、次の方法で実際のロガーにアクセスできます。
$userActionsLogger->logger->info('A thing happened!')