Symfony / Monolog
config/services.yaml
-
# Show request info in log (URL, IP, referer, etc) monolog.processor.web: class: Monolog\Processor\WebProcessor tags: - { name: monolog.processor } # Include stack traces monolog.formatter.line: class: Monolog\Formatter\LineFormatter calls: - [includeStacktraces]
config/packages/prod/monolog.yaml
-
monolog: handlers: # log rotation: https://symfony.com/doc/current/logging.html#how-to-rotate-your-log-files # mailing: https://symfony.com/doc/current/logging/monolog_email.html # levels: https://stackify.com/php-monolog-tutorial/ main: # if *one* log entry is error or higher, pass *all* to file_log type: fingers_crossed #action_level: warning action_level: error # practically silences the log #action_level: alert excluded_http_codes: [404, 405] buffer_size: 100 # How many messages should be saved? Prevent memory leaks handler: grouped # That does not eliminate deprecation warnings #channels: [ "!deprecation" ] grouped: type: group # members: [file_log, deduplicated] members: [file_log] file_log: #type: stream type: rotating_file # max number of log files to keep # defaults to zero, which means infinite files max_files: 14 #path: php://stderr path: "%kernel.logs_dir%/%kernel.environment%.log" #level: debug #level: warning level: error # practically silences the log #level: alert #formatter: monolog.formatter.json formatter: monolog.formatter.line # deduplicated: # type: deduplication # handler: symfony_mailer # symfony_mailer: # type: symfony_mailer # from_email: 'log-errors@example.com' # to_email: 'me@example.com' # subject: 'An Error Occurred! %%message%%' # level: debug # formatter: monolog.formatter.html # content_type: text/html # phperrorlog: # type: stream # path: php://stderr # level: notice console: type: console process_psr_3_messages: false channels: ["!event", "!doctrine"]
Disable logging:
- /config/packages/dev/monolog.yaml
-
monolog: handlers: main: # Quotes are important, otherwise interpreted as null "empty" type: 'null'
-
Custom Log
E.g. "i18n"
- config/packages/dev/monolog.yaml
-
monolog: channels: - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists - i18n # Internationalization logs are logged in the dedicated "internationalization" channel when it exists handlers: i18n: type: stream path: "%kernel.logs_dir%/i18n.log" level: debug channels: ["i18n"]
-
- src/Service/I18nLoggerService.php
-
<?php namespace App\Service; use Psr\Log\LoggerInterface; // tail -f var/log/i18n.log // Used to log internationalization events class I18nLoggerService { public function __construct(private LoggerInterface $logger) { } public function log(string $message) { $this->logger->info($message); } }
-
- config/services.yaml
-
services: # Custom internationalization logger service into var/log/i18n.log App\Service\I18nLoggerService: arguments: $logger: '@monolog.logger.i18n'
-
- Use it with dependency injection
-
public function __construct(private I18nLoggerService $logger){ $logger->log('Foo'); }
-