G
G
gistol2019-02-28 14:42:26
symfony
gistol, 2019-02-28 14:42:26

How to output logs to separate file in symfony 3?

Greetings.
Question for connoisseurs of Symfony 3, namely version 3.4.
I created a console command and I'm trying to output the logs to a separate file. Now the logs are displayed in the console and are automatically saved to var/logs/prod.log, but I would like to separate them, for example, to the import.log file. As
I understand it, you need to specify the desired path for writing logs in the system sweets. I tried many options from the documentation, but none of them worked. I do not understand where and how to enter the settings.
Please help anyone who knows how to do this.
The code:

<?php
namespace AppBundle\Command;

use AppBundle\Logger\MyDependency;
use AppBundle\Upload\Import;
use AppBundle\Upload\Settings;
use AppBundle\Upload\Utility;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class ImportCommand extends ContainerAwareCommand
{
   
    protected function configure()
    {
        $this
            ->setName('app:import-products')
            ->setDescription('Good morning!');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $logger = $this->getContainer()->get('logger');
        $logger->error('Your error message');
        $logger->alert('Your alert');
        $logger->debug('Your debug message', ['foo' => 'bar']); // additional context information
        $logger->log('error','Put this in the log file');
 }
}

Folder structure with configs
5c77c7b1ceb75074897331.png
Settings code
Tried in config.yml, config_prod.yml
monolog:
  handlers:
    # ...
    console:
      type:   console
      process_psr_3_messages: false
      channels: ['!event', '!doctrine', '!console']
    nested:
      type:  stream
      path:  "%kernel.logs_dir%/%kernel.environment%test.log"
      level: debug

Tried in services.yml
services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false
    AppBundle\Command\ImportCommand:
      arguments:
        $logger: '@logger'
      tags:
      - { name: monolog.logger, channel: application_logger }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2019-02-28
@sidni

type create your own handler and channel

channels: ['my_handler']
my_handler:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%_my_handler.log"
            channels: ["my_handler"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question