'Symfony 4.x Log AJAX as JSON to log file with Monolog

I try to log GET/POST to ajax to a log file. I can not figure out how to post to a specific channel :/

Any Advice?

my config/packages/dev/monolog.yaml

monolog:
  channels: [ "ajax" ]
  handlers:
    ajax:
      type: stream
      path: "%kernel.logs_dir%/%kernel.environment%/ajax.json"
      formatter: 'monolog.formatter.json'
      level: debug
      channels: ['ajax']     # Only handle ajax
    main:
      type: stream
      path: "%kernel.logs_dir%/%kernel.environment%/main.log"
      level: info
      channels: ['!ajax', '!event'] # All but ajax and event

my src/Controller/AjaxController.php

class AjaxController extends BackendController {
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private LoggerInterface $logger;

    public function __construct(

        LoggerInterface $logger

    ) {

        $this->logger = $logger;

    }

    public function saveProductName(EntityManagerInterface $em, Request $request) {

        $data = [
            'id'          => $request->request->get('id'),
            'productname' => $request->request->get('productname')
        ];

        $this->logger->debug('ajax', $context);
        
        //... more stuff
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source