diff --git a/logging.rst b/logging.rst index 4cfb46581ad..cb2e43e5c89 100644 --- a/logging.rst +++ b/logging.rst @@ -357,6 +357,102 @@ specific channel (``app`` by default), you can either :ref:`autowire monolog cha or use the ``monolog.logger`` tag with the ``channel`` property as explained in the :ref:`Dependency Injection reference `. +Adding Stacktraces from Exceptions +---------------------------------- + +To include stacktraces to your logs, set the ``include_stacktraces`` option on the "stream" handler to true and include the exception key in your logging statement:: + + + $logger->error($exception->getMessage(), ['exception' => $exception]); + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/prod/monolog.yaml + monolog: + handlers: + filter_for_errors: + type: fingers_crossed + # if *one* log is error or higher, pass *all* to file_log + action_level: error + handler: file_log + + # now passed *all* logs, but only if one log is error or higher + file_log: + type: stream + include_stacktraces: true + path: "%kernel.logs_dir%/%kernel.environment%.log" + + # still passed *all* logs, and still only logs error or higher + syslog_handler: + type: syslog + level: error + + .. code-block:: xml + + + + + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/prod/monolog.php + $container->loadFromExtension('monolog', [ + 'handlers' => [ + 'filter_for_errors' => [ + 'type' => 'fingers_crossed', + // if *one* log is error or higher, pass *all* to file_log + 'action_level' => 'error', + 'handler' => 'file_log', + ], + + // now passed *all* logs, but only if one log is error or higher + 'file_log' => [ + 'type' => 'stream', + 'path' => '%kernel.logs_dir%/%kernel.environment%.log', + 'level' => 'debug', + 'include_stacktraces' => true + ], + + // still passed *all* logs, and still only logs error or higher + 'syslog_handler' => [ + 'type' => 'syslog', + 'level' => 'error', + ], + ], + ]); + Adding extra Data to each Log (e.g. a unique request token) -----------------------------------------------------------