mctroubleshooter
mctroubleshooter

Reputation: 11

symfony2 logger in profiler

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

Upvotes: 1

Views: 2706

Answers (2)

Artem  Zhuravlev
Artem Zhuravlev

Reputation: 786

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

Upvotes: 2

Derick F
Derick F

Reputation: 2769

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

Upvotes: 0

Related Questions