symfony2 logger in profiler

2.6k views Asked by At

I looked for this info in this forum and many others but I can't understand how to log messages in the profiler.

I attempt to log messages from my controller.

CalendarController.php :

<?php

namespace DJU\CalendarBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Psr\Log\LoggerInterface;



class CalendarController extends Controller
{
    protected $logger;
    public function __construct(LoggerInterface $logger) {
        $this->logger = $logger;
    }

    public function getweekAction() {
        $this->logger->info("this is a test message");
    }
}

According to what I've read in the symfony doc, I've also modified the config.yml

app/config/config.yml

monolog:
    handlers:
        main:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.log"
            level:  debug
        console:
            type:   console
            bubble: false
            verbosity_levels:
                VERBOSITY_VERBOSE: INFO
                VERBOSITY_VERY_VERBOSE: DEBUG
            channels: ["!doctrine"]
        console_very_verbose:
            type:   console
            bubble: false
            verbosity_levels:
                VERBOSITY_VERBOSE: NOTICE
                VERBOSITY_VERY_VERBOSE: NOTICE
                VERBOSITY_DEBUG: DEBUG
            channels: ["doctrine"]

This is a simple copy paste from the config_dev.yml.

No log appears in the profiler.

Any suggestion?

Thank you

2

There are 2 answers

1
Derick F On

what version of symfony? if you're on symfony 2.6 or higher you can just do dump('some log'); and it will appear in the profiler.

http://symfony.com/blog/new-in-symfony-2-6-vardumper-component

3
Artem  Zhuravlev On

If you're using constructor in controller class then you should use controller as service to inject logger there. Normally if you want to call logger service from controller you can simply get it from container:

class CalendarController extends Controller
{ 
    public function getweekAction() {
        $this->get('logger')->info("this is a test message");
    }
}