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
Related
datadog.conf
log_level: warn
log_file: /var/log/datadog/agent.log
log_to_syslog: no
dd_url: https://app.datadoghq.com
api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
process_config:
enabled: "true"
logs_enabled: true
tags:
- env:stage
- nginx
- webserver
- frontend
- php-fpm
- php
use_dogstatsd: true
dogstatsd_port: 8125
I needed to enable flag logs_enabled: true in order to collect PHP logs but with this, I also collect Nginx logs (access and error logs)
If logs_enabled is set to false then I do not collect even PHP logs.
How can I exclude Nginx logs from being collected?
# php.d/conf.yaml
init_config:
instances:
logs:
- type: file
path: "/var/www/{{ env }}/var/log/app-log.json"
service: php
source: php
sourcecategory: sourcecode
The solution was very simple.
Data dog agent configuration is related to enabling/disabling collecting logs from a webserver.
In order to turn off/on specific service logging, it must be done from the DataDog webservice.
Visit the next URL https://app.datadoghq.com/logs/pipelines/indexes
And then add the exclusion rule.
Simple as that :)
I would like to setup a staging environment with the same configuration as the prod environment.
According to the docs, I proceed as follow:
I create a staging symlink that points on prod
configure the env in .env: APP_ENV=staging
clear the cache: php bin/console cache:clear
ask for an URL that does not exist to trigger a 404 error: http://localhost:8080/an-url-that-does-not-exists
When the APP_ENV=prod, my custom error page is render properly, but when APP_ENV=staging, the debug message NotFoundHttpException is rendered?
The profiler is not displayed.
What am I missing?
tldr;
Crate an .env.staging file and use it to set APP_DEBUG to 0. Debug mode and environment are set independently.
By default, unless you set it explicitly debug mode (APP_DEBUG) is set from the environment automagically.
This happens in the following steps:
In your front-controller (index.php, usually) you would find this line:
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
And on DotEnv::bootEnv() you'll find this:
$debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey], $this->prodEnvs, true);
This will compare your APP_ENV with an array of "environments" that DotEnv, considers "production-like". By default, this array includes only prod.
You could modify the instance of the DotEnv by calling setProdEnvs():
(new Dotenv())
->setProdEnvs(['prod', 'staging'])
->bootEnv(dirname(__DIR__).'/.env');
... but generally simply disabling debug mode on your .env file would be enough.
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 need to add some action, when application runs one of Monolog\Logger methods (info, error, warning etc.) and do some custom code.
for example:
$this->logger->error('Some error');
should do error output - basic action for Monolog\Logger, but after that send error text via API ...
Please read the Symfony Monolog documentation and check out if you find any network or server handler from the list of included handlers and their configuration options.
If there is no suitable handler you should create a custom handler class using the service handler type, e.g. src/AppBundle/Monolog/YourApiHandler.php which needs to implement at least the HandlerInterface , but you could also see if another class you could inherit from is more appropriate for your task, e.g. AbstractProcesssingHandler .
Once you have implemented your handler just define a service for it
# app/config/services.yml
services:
my_handler:
class: AppBundle\Monolog\YourApiHandler
and add it to the monolog configuration:
# app/config/config.yml
monolog:
handlers:
my_handler:
type: service
id: my_handler
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).