I have a Symfony2 web application.
Everything works well in local. But when moving to production environment, there are some things that don't work the same as local.
Now, to figure out what is going on on Production I am trying to write some logs.
For some reason, I am not getting to write anything to the production log. Could anyone tell me what I am doing wrong? This is my code in the Controller in (nothing too sophisticated, by the way):
$logger = $this -> get("logger");
$logger -> err("My Trace --> " . $my_inspect_variable);
The ./logs/prod.log is being writing properly by Symfony. But when I try to write something by myself, it just ignores me.
My config_prod.yml file:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
Related
We recently moved our infra to K8s, and since I have issue to get Traces of my Exceptions
We're on Symfony 3.4, and my Exception traces only gaves me web/app.php, but not root cause
Error: Call to a member function send() on null
#0 web/app.php(19): null
app.php is classic Symfony file, nothing relevent inside:
$kernel = new AppKernel('prod', false);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
I tried to connect my localhost to my production DB, to see if I had the same issue, but no. I have correct full trace of my Exception
If I try in production with "app_dev.php" (which I enabled only for this test), I have same issue with no trace
Do you know what could cause this issue?
A PHP configuration? I checked differences between php -i on previous infra & new one, but I don't see any relevant difference
Edit:
Monolog in config.yml:
monolog:
handlers:
main:
type: rotating_file
max_files: 10
path: '%kernel.logs_dir%/%kernel.environment%.general.log'
level: INFO
formatter: myapp.monolog.formatter.json_message
Edit 2:
We bring part of old infra alive. And here the difference :
Old infra, for Fatal Errors, we have the full trace:
On new infra, same fatal error is displayed without any more detail
The issue seems to comes from datadog agent that we added on our new infra.
Edit: Issue has been fixed in https://github.com/DataDog/dd-trace-php/releases/tag/0.79.0
I got an error 500 on production env so I spent a lot of time to search where error was displaying. I found that symfony log errors using logger who send output to stderr by default so apache's error log is used in my case. I want to send messages to symfony's env.log (e.g var/log/dev.log and var/log/prod.log)
Documentation says :
The minimum log level, the default output and the log format can also be changed by passing the appropriate arguments to the constructor of Logger. To do so, override the "logger" service definition.
But I can't figure out how I can change logger's output, for testing purpose I changed logger construct in vendor/symfony/http-kernel/Log/Logger.php as following with success:
public function __construct(string $minLevel = null, $output = 'abspath/to/project/var/log/dev.log', callable $formatter = null)
but I can't edit files in vendor dir.
So How I can override the "logger" service definition ?
Do not edit the vendor files. Those are included by composer, and overwriten on each install/update.
If you want to change the log path you can do this with using monolog and config
composer require symfony/monolog-bundle
monolog:
handlers:
# this "file_log" key could be anything
file_log:
type: stream
# log to var/log/(environment).log
path: "%kernel.logs_dir%/%kernel.environment%.log" #change path here
# log *all* messages (debug is lowest level)
level: debug
syslog_handler:
type: syslog
# log error-level messages and higher
level: error
coming from Java development, I learned to appreciate LogLevel, how to set it in Logback or Log4j.
Monolog is used in my symphony 4.2 project.
I would like to see that from a certain controller/namespace the log entries with level info can also be seen in the production log file without all the other log entries with the info level filling the log file.
Unfortunately I didn't find any explanations.
After a little more research, I configured my monolog.yaml for dev and also prod this way
monolog:
channels: ['appinfo']
handlers:
custom:
channels: ['appinfo']
level: info
max_files: 30
path: "%kernel.logs_dir%/appinfo.log"
type: rotating_file
The important thing is the channel, appinfo in my case.
The handler, custom in my case, can be named any way you like.
Then in the services.yaml one has to define the "type" of the injected logger.
App\Controller\DefaultController:
arguments:
$logger: '#monolog.logger.appinfo'
This works with controllers as with services
I'd like to set the Symfony 4 profiler to log only exceptions. The application works in dev environment. Having default config everything works as expected - the profiler logs each request (including exceptions).
I found a setting concerning resolving my case: only_exceptions and according to the documentation (https://symfony.com/doc/current/reference/configuration/framework.html#only-exceptions), this should do the trick. But it doesn't, after changing this to true, the profiler logs entirely nothing, even though I deliberately throw an exception.
My config/packages/dev/web_profiler.yaml:
web_profiler:
toolbar: true
intercept_redirects: false
framework:
profiler: { only_exceptions: true }
I've tried this setting for both Symfony versions (3 & 4), in neither of them it seems to work. Is there anybody able to help me? Thanks in advance!
Could you suggest how to print any debug data into browser console in Symfony 2?
Especially, is it possible to implement with Symfony VarDumper Component?
In Zend Framework you can use tool Zend_Log_Writer_Firebug to do so which is very helpful. Does Symfony has something like this from the box?
Monolog, the logger used by Symfony, has a built-in support for FirePHP and ChromePHP.
In Symfony Standard Edition you can configure monolog handlers in your application configuration.
FirePHP and ChromePHP handlers are even present in config_dev.yml, but are commented out:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: [!event]
console:
type: console
channels: [!event, !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
All you need to do to see your logs in the browser is to uncomment the needed handler.
Currently, the VarDumper component doesn't support dumping anything to the browser's console. However, you can see the dumped values in the web debug toolbar (or in html if you don't use the toolbar).