doufeixi6014 2014-04-14 11:45
浏览 36
已采纳

在日志中显示当前用户

I'm trying to create a custom monolog processor to attach the current user to an error mailer.

When declaring a service like so:

monolog.processor.mail:
        class: MyVendor\Monolog\Processor\MailProcessor
        arguments:
            - @mailer
            - @security.context
        tags:
            - { name: monolog.processor, method: processRecord }

I get a circular reference:

[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "monolog.processor.mail",
path: "router -> monolog.logger.router -> monolog.processor.mail
-> security.context -> security.authentication.manager
-> fos_user.user_provider.username_email-> fos_user.user_manager
-> doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection
-> doctrine.dbal.logger -> monolog.logger.doctrine".

What would be the best practice solution here?

A related forum thread: http://forum.symfony-project.org/viewtopic.php?t=40306&p=131081#p131143

This thread shows that:

  • Setter injection doesn't solve the issue (i tried this as well)
  • Injecting the container causes an infinitive recursion (this i have not confirmed)

Also tried this script http://pastebin.com/AuvFgTY3 to get the user from the session.

if ($this->session !== null) {
    if ($this->session->has($this->securityKey)) {
        $token = unserialize($this->session->get($this->securityKey));
        $this->currentUser = $token->getUser();
    }
}

This gave the following error:

Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in
C:\inetpub\symfony23\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler.php
on line 56

I do understand that the security.context has not yet been build for services which request the logger very early on. For my class it's not a problem since i will set the user to undefined. So ideally the security.context would be setter injected AFTER the security.context service has been created. However i can not change the priority on the logger to be constructed very late because it's needed early on. So maybe the question resolves to: how to recreate the service again after security.context has been initialized? Not sure if scope prototype would help here??

  • 写回答

1条回答 默认 最新

  • douliaotong4944 2015-09-09 07:58
    关注

    Create handler on kernel request and extract user:

    // src/AppBundle/Monolog/UserProcessor.php
    
    namespace AppBundle\Monolog;
    
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    class UserProcessor
    {
        private $tokenStorage;
        private $user;
    
        public function __construct(TokenStorageInterface $tokenStorage)
        {
            $this->tokenStorage = $tokenStorage;
        }
    
        public function __invoke(array $record)
        {
            if (null !== $this->user) {
                $record['extra']['user'] = $this->user->getUsername();
            }
    
            return $record;
        }
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            if (null === $token = $this->tokenStorage->getToken()) {
                return;
            }
    
            if (!is_object($user = $token->getUser())) {
                // e.g. anonymous authentication
                return;
            }
    
            $this->user = $user;
        }
    }
    

    Register new processor:

    # app/config/services.yml
    
    services:
        monolog.processor.user:
        class: AppBundle\Monolog\UserProcessor
        arguments:  ["@security.token_storage"]
        tags:
            - { name: monolog.processor }
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    

    Symfony Documentation has problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退