0

I am building a e-commerce application under symfony 4.0. The application is getting complex and I want to log the process. I would like to log it in a specific log file.

For that I have create a new monolog handler : 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"]

How can I use this log now. for example :

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

To be in the right log file..

Thanks a lot for your help.

Pierre.

123pierre
  • 305
  • 1
  • 4
  • 18

1 Answers1

2

I found a solution...

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;
    }
}

Any Controller :

use App\Service\YoushoesLog;

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

}
123pierre
  • 305
  • 1
  • 4
  • 18