Buy

We have two environments! That's super flexible... or I think it will be, just as soon as we figure out how the heck we can configure each environment.

Compare app.php and app_dev.php. What are the differences? Ok, ignore that big if block in app_dev.php. The important difference is a single line: the one that starts with $kernel = new AppKernel(). Hey, that's the class that lives in the app/ directory!

The first argument to AppKernel is prod in app.php:

33 lines web/app.php
... lines 1 - 22
$kernel = new AppKernel('prod', false);
... lines 24 - 33

And dev in app_dev.php:

33 lines web/app_dev.php
... lines 1 - 26
$kernel = new AppKernel('dev', true);
... lines 28 - 33

This defines your environment. The second argument - true or false - is a debug flag and basically controls whether or not errors should be shown. That's less important.

config_dev.yml versus config_prod.yml

Now, what do these dev and prod strings do? Here's the secret: when Symfony boots, it loads only one configuration file for the entire system. And no, it's not config.yml - I was lying to you. Sorry about that. No, the dev environment loads only config_dev.yml:

46 lines app/config/config_dev.yml
imports:
- { resource: config.yml }
framework:
router:
resource: "%kernel.root_dir%/config/routing_dev.yml"
strict_requirements: true
profiler: { only_exceptions: false }
web_profiler:
toolbar: true
intercept_redirects: false
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"]
# 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
#swiftmailer:
# delivery_address: me@example.com

And in prod, the only file it loads is config_prod.yml:

28 lines app/config/config_prod.yml
imports:
- { resource: config.yml }
#framework:
# validation:
# cache: validator.mapping.cache.apc
# serializer:
# cache: serializer.mapping.cache.apc
#doctrine:
# orm:
# metadata_cache_driver: apc
# result_cache_driver: apc
# query_cache_driver: apc
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console

Ok fellow deep sea explorers, this is where things get cool! Look at the first line of config_dev.yml:

46 lines app/config/config_dev.yml
imports:
- { resource: config.yml }
... lines 3 - 46

What does it do? It imports the main config.yml: the main shared configuration. Then, it overrides any configuration that's special for the dev environment.

Check this out! Under monolog - which is the bundle that gives us the logger service - it configures extra logging for the dev environment:

46 lines app/config/config_dev.yml
... lines 1 - 13
monolog:
handlers:
main:
... lines 17 - 18
level: debug
... lines 20 - 46

By setting level to debug, we're saying "log everything no matter its priority!"

So what about config_prod.yml? No surprise: it does the exact same thing: it loads the main config.yml file and then overrides things:

28 lines app/config/config_prod.yml
imports:
- { resource: config.yml }
... lines 3 - 28

This file has a similar setup for the logger, but now it says action_level: error:

28 lines app/config/config_prod.yml
... lines 1 - 15
monolog:
handlers:
main:
... line 19
action_level: error
... lines 21 - 28

This only logs messages that are at or above the error level. So only messages when things break.

Experimenting with config_dev.yml

Let's play around a bit with the dev environment! Under monolog, uncomment the firephp line:

46 lines app/config/config_dev.yml
... lines 1 - 13
monolog:
handlers:
... lines 16 - 34
# 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
... lines 40 - 46

This is a cool handler that will show you log messages right in your browser.

Head over to it and run "Inspect Element". Make sure that the URL will access the dev environment and then refresh. And check this out: a bunch of messages telling us what route was matched. Heck, we can even see what route was matched for our ajax call. To see this working, you'll need a FirePHP extension installed in your browser. In your app, Monolog is attaching these messages to your response headers, and the extension is reading those. We don't want this to happen on production, so we only enabled this in the dev environment.

Environments are awesome! So how could we use them only cache our markdown string in the prod environment?

Leave a comment!

  • 2017-09-21 Victor Bocharsky

    Hey Rich,

    Haha, thank you for this excellent feedback and all the kind words in our direction!

    Cheers!

  • 2017-09-20 Rich Wilx

    I'm sitting here still, watching these vids. And, with every step forward, I'm just thinking "Symfony is <expletive> brilliant!"
    The developers have done an awesome job.

    And "by Poseidon's beard" your videos are great: Well scripted, no muffled voices, no noisy keyboard presses.

  • 2017-08-12 Patryk Węgrzynek

    Thanks Jeroen Noten. Really good tip :)

  • 2017-08-02 weaverryan

    Thanks for sharing Jeroen Noten!

  • 2017-08-01 Jeroen Noten

    I struggled with making the firephp logging getting to work in Chrome as well, and I managed to found the solution:
    1. instead of uncommenting the firephp config section, uncomment the chromephp config section
    2. install the 'Chrome Logger' extension and enable it by clicking the black icon in the top right.

    I hope this helps others!

  • 2017-07-14 weaverryan

    Hey Juan!

    > but it does not show me the path name

    Are you referring to this message in the console: "XHR finished loading: GET "http://localhost:8000/app_dev.php/genus/octopus/notes"?

    If so, *this* actually doesn't come from the logger :). It's a browser setting to show AJAX requests (XHR requests) in the console. You can enable it by right-clicking on the console and selecting "Log XMLHtttpRequests": http://imgur.com/3rp0sTc

    But if you're referring to something else, let me know!

    Cheers!

  • 2017-07-13 Juan Nicolás

    Hello, I did everything and it works, I installed ChromePHP and I enabled it, but it does not show me the path name: http://imgur.com/a/jsjqz

    What I did wrong? Thanks!

  • 2017-03-29 Diego Aguiar

    Hey Richie Hamburg
    Who actually does the job of adding those headers is the extension FirePHP or ChromePHP (symfony has support for both) you need to install it and then enable it in the config.yml file as shown in the video

    After enabling it, you can use monolog as always, accesing it through container or injecting into a service


    $logger = $this->get('logger');
    $logger->info('I just got the logger');
    $logger->error('An error occurred');

    You can read more about monolog here:
    http://symfony.com/doc/curr...

    Have a nice day!

  • 2017-03-29 Richie Hamburg

    Can you point to where and how monolog is used to add those headers you mention above please.

  • 2017-03-08 weaverryan

    Hey Richie!

    Ha! Of course that would be the solution :). About the headers, they might be there - you're just not quite looking in the correct spot. In the network tab (which is what your screenshot is looking at) scroll all the way to the top of the list. The first item on the list will be the request for this actual page - will be something like /genus/octopus. Click on that item. That should open up a big section on the right with a bunch of tabs: Headers, Preview, Response, etc. It'll look like my screenshot: http://i.imgur.com/0d85kfK.png. Make sure you're on the Headers tab, then scroll down to where you see the "Response Headers". Check for the header there.

    tl;dr; The response headers don't show up on the full list of requests - but just click on one of the individual requests to see them! You should also see the headers if you click on any of the AJAX requests (i.e. any of the "notes" rows in the network list).

    Cheers!

  • 2017-03-08 Richie Hamburg

    Aha. All sorted. Thanks.

  • 2017-03-08 Richie Hamburg

    The magic of computers - I have my error logs now - uninstalling and reinstalling the extension seems to have triggered it. But I'm not seeing headers in the network tab of chrome tools for some reason. Maybe I'm being blind - it's a long day. http://imgur.com/KYSDWQi

  • 2017-03-08 weaverryan

    Hey Richie!

    Hmm..... So, here's how this works. When you activate the firephp handler in Monolog (there is also a chromephp handler - you could try that instead), it ultimately triggers this class (and its base class): https://github.com/symfony/.... Specifically, the onKernelResponse() method is called. As you can see, this looks to see if the Request has some headers in it. Basically, it's checking to make sure that the plugin is installed in your browser: when the plugin is installed, it should add a header to the request to "advertise" itself. If that header is not present, then the firephp handler does nothing. I would look in the Network tools in your browser to see what the Request headers are of your main request! You could also temporarily add some debugging code to this function to see if Symfony sees that header.

    If Symfony is doing its job, then you should be able to see an extra header on your Response (again, look in the network tools). It will look something like this: http://i.imgur.com/0d85kfK.png. If you see this, Symfony is doing its job! But for some reason, your plugin is not picking it up. If you do NOT see this, Symfony is not doing its job, either because something is misconfigured, or because your plugin isn't sending the necessary header to "advertise" its presence.

    Phew! I hope that at least gives you some ideas for debugging! Some of these plugins are better maintained than others, which could be part of the problem!

    Let me know what you find out!

  • 2017-03-08 Richie Hamburg

    No logging here either. 100% using the _dev environment (with and without the app_dev.php) and nothing is appearing in the chrome console. tried two different firePHPs and also ChromePHP just to be sure. 100% uncommented the firephp paragraph in config_dev.yml too. Suggestions?

  • 2016-07-01 Ilya

    Hey
    No, it's ok. Thank you

  • 2016-07-01 Victor Bocharsky

    Hey, Ilya!

    Have you installed PHP Annotation plugin already? Can I help you with installation?

  • 2016-06-30 Ilya

    Thanks, Victor
    Becouse I've got some troubles earlier whith PHP Storm (I didn't install PHP Annotation plugin.)

  • 2016-06-30 Victor Bocharsky

    Hey, Ilya!

    It's not required for other screencasts, just a usage example for this chapter. But feel free to use this if it helps you, of course.

  • 2016-06-30 Ilya

    Is it necessarily for the future tutorials\lessons Fire PHP or it just for this lesson to see messages are coming?

  • 2016-03-21 Andrew Grudin

    I catch it , thanks for your time and invaluable help!

  • 2016-03-20 weaverryan

    If it's working, don't worry about those headers. But if you're curious, open the "inspect element" on your browser (Chrome), click the network tab and then refresh. You should see a bunch of requests in this list, for the main page, CSS files etc. Select the first one (for the main page) and you should see something like what I've shown: these are the response headers sent back by Symfony. The headers are how Symfony communicates with the browser plugin :)

  • 2016-03-19 Andrew Grudin

    I have installed FirePHP4Chrome and it is working ... The fact is FirePHP for Chrome doesn't work (which was installed first by me).

    I see now in Inspect: request Object {message: "Matched route "genus_show_notes".", context: Object}

    Just one thing, I can't find in my Inspect something like request you are talking about... and line ( Headers Preview Response Cookies Timing ) as in your http://i.imgur.com/0d85kfK.png.
    Where should I look that stuff for?
    Best regards!

  • 2016-03-18 weaverryan

    Hey Andrew!

    Did you also make the change in config_dev.yml to activate this on the Symfony side? One other way to check (to see if things are configured correctly on the Symfony side) is to open your inspect, refresh, then find the request for the full page that just finished. In the first tab, you can see the "response" headers. There should be a BUNCH of weird response headers - like this: http://i.imgur.com/0d85kfK.png. If you can see that, you're setup on the Symfony side - but something isn't right with the extension.

    Cheers!

  • 2016-03-18 Andrew Grudin

    I have installed FirePHP extension in my Chrome but don't see any message telling me what route was matched when inspecting element on localhost:8000/genus/octopus (((