编程语言
首页 > 编程语言> > php – 如何编写用户名登录Symfony2

php – 如何编写用户名登录Symfony2

作者:互联网

我想将像extra field这样的用户名写入生产日志.
我该怎么做?

我在config.yml中有这个配置:

services:
    monolog.formatter.logprocessor:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%datetime%%] [%%username%%] %%channel%%.%%level_name%%: %%message%%\n"

monolog.processor.logprocessor:
    class: Acme\CoreBundle\Monolog\LogProcessor
    tags:
        - { name: monolog.processor, method: processRecord }
monolog:
    handlers:
        main:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: warning
            formatter: monolog.formatter.logprocessor

当我尝试将security.context注入LogProcessor时,我收到错误:

[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]         
Circular reference detected for service "monolog.processor.logprocessor", path: "router ->  
monolog.logger.router -> monolog.processor.logprocessor -> security.context -> security.a  
uthorization_checker -> security.authentication.manager -> security.user.provider.concrete  
.chain_provider -> security.user.provider.concrete.main ->  doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> monolog.logger.doctrine".    

解决方法:

这是配置

# Service Configuration
services:          
    monolog.formatter.html:
        class: Monolog\Formatter\HtmlFormatter

    monolog.processor.web_processor:
        class: Monolog\Processor\WebProcessor
        tags:
            - { name: monolog.processor, method: __invoke } 

    monolog.processor.user:
        class: Company\ToolBoxBundle\Services\Monolog\ExtraProcessor
        arguments:  ["@security.token_storage"]
        tags:
            - { name: monolog.processor }
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }    

和服务代码
    

namespace Company\ToolBoxBundle\Services\Monolog;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class ExtraProcessor
{
    private $tokenStorage = null;
    private $user = null;
    private $postParams = null;    

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function __invoke(array $record)
    {
        if (null !== $this->user) {
            $record['extra']['user'] = $this->user->getUserDetails();
        }

        if (null !== $this->postParams) {
            $record['extra']['postParams'] = $this->postParams;
        }

        return $record;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $postParams = $event->getRequest()->request->all();
        if(false === empty($postParams)){
            $this->postParams = serialize($postParams);
        }  

        if (null === $token = $this->tokenStorage->getToken()) {
            return;
        }

        if (!is_object($user = $token->getUser())) {
            // e.g. anonymous authentication
            return;
        }

        $this->user = $user;
    }
}

标签:monolog,php,symfony
来源: https://codeday.me/bug/20190728/1560094.html