Symfony - монологический специфический обработчик - PullRequest
0 голосов
/ 15 октября 2018

Я создаю приложение для электронной коммерции под symfony 4.0.Приложение становится сложным, и я хочу войти в процесс.Я хотел бы зарегистрировать его в определенном файле журнала.

Для этого я создал новый обработчик монолога: monolog.yaml:

monolog:
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event"]
        youshoes:
            level: info
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
            channels: [youshoes]
    channels: ["main", "youshoes"]

Как я могу использовать этот журнал сейчас.например:

 $logger->info('Client has validated is cart');
 $logger->info('Payment is successful');
 $logger->info('Client is requesting for deliveries');
etc ....

Быть в нужном лог-файле ..

Большое спасибо за вашу помощь.

Пьер.

1 Ответ

0 голосов
/ 16 октября 2018

Я нашел решение ...

monolog.yaml

monolog:
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event", "!youshoes"]
        youshoes:
            level: info
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
            channels: ["youshoes"]

        # uncomment to get logging in your browser
        # you may have to allow bigger header sizes in your Web server configuration
        #firephp:
        #    type: firephp
        #    level: info
        #chromephp:
        #    type: chromephp
        #    level: info
        console:
            type:   console
            process_psr_3_messages: false
            channels: ["!event", "!doctrine", "!console"]

    channels: ["youshoes"]

services.yaml

...
    # saw in https://symfony.com/doc/current/service_container.html#services-wire-specific-service
    App\Service\YoushoesLog:
        arguments:
            # the '@' symbol is important: that's what tells the container
            # you want to pass the *service* whose id is 'monolog.logger.request',
            # and not just the *string* 'monolog.logger.request'
            $logger: '@monolog.logger.youshoes'

Service / YoushoesLog.php

<?php

namespace App\Service;
use Psr\Log\LoggerInterface;

class YoushoesLog

{

    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function info($message)
    {
        if ($this->logger->info($message)) return true;
        return false;
    }
}

Любой контроллер:

use App\Service\YoushoesLog;

class TestController extends Controller{
public function test(YoushoesLog $logger){
    $logger->info("Client has sent order");
}

}
...