From febc802d13d088487022c8544f5856854d6c037f Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Fri, 24 Apr 2020 12:13:58 +0200 Subject: [PATCH 1/7] Document how to add stacktraces to monolog IMHO this should be in the main section because it is a major DX issue. --- logging.rst | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/logging.rst b/logging.rst index 4cfb46581ad..714332fdbce 100644 --- a/logging.rst +++ b/logging.rst @@ -159,6 +159,7 @@ to write logs using the :phpfunction:`syslog` function: This defines a *stack* of handlers and each handler is called in the order that it's defined. + Handlers that Modify Log Entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -357,6 +358,106 @@ 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: + + +```php +$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) ----------------------------------------------------------- From 120322471a5db42244768a223c37e42da92c83ed Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:50:48 +0200 Subject: [PATCH 2/7] Update logging.rst Co-Authored-By: Oskar Stark --- logging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging.rst b/logging.rst index 714332fdbce..c3dbff030be 100644 --- a/logging.rst +++ b/logging.rst @@ -358,7 +358,7 @@ 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 +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: From 86ed5eaa54dfd1e14ae82f104fcc1a318cbef6b9 Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:51:14 +0200 Subject: [PATCH 3/7] Improve formating Co-Authored-By: Oskar Stark --- logging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging.rst b/logging.rst index c3dbff030be..5b194f86f1d 100644 --- a/logging.rst +++ b/logging.rst @@ -361,7 +361,7 @@ or use the ``monolog.logger`` tag with the ``channel`` property as explained in 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: +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:: ```php From 82711d41ecd361563c98942321c16a38cb8cc7c2 Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:51:21 +0200 Subject: [PATCH 4/7] Update logging.rst Co-Authored-By: Oskar Stark --- logging.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/logging.rst b/logging.rst index 5b194f86f1d..53032733135 100644 --- a/logging.rst +++ b/logging.rst @@ -368,7 +368,6 @@ To include stacktraces to your logs, set the ``include_stacktraces`` option on t $logger->error($exception->getMessage(), ['exception' => $exception]); ``` - .. configuration-block:: .. code-block:: yaml From 221d1aa866df06ebe44d5c6384472d59cef4393f Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:51:29 +0200 Subject: [PATCH 5/7] Update logging.rst Co-Authored-By: Oskar Stark --- logging.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/logging.rst b/logging.rst index 53032733135..799aa516b38 100644 --- a/logging.rst +++ b/logging.rst @@ -364,9 +364,7 @@ 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:: -```php -$logger->error($exception->getMessage(), ['exception' => $exception]); -``` + $logger->error($exception->getMessage(), ['exception' => $exception]); .. configuration-block:: From 75fd2dbb1a8dd0ac294b906d758eeeefaf9a3198 Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:51:38 +0200 Subject: [PATCH 6/7] Update logging.rst Co-Authored-By: Oskar Stark --- logging.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/logging.rst b/logging.rst index 799aa516b38..193eb61f40e 100644 --- a/logging.rst +++ b/logging.rst @@ -454,7 +454,6 @@ To include stacktraces to your logs, set the ``include_stacktraces`` option on t ], ]); - Adding extra Data to each Log (e.g. a unique request token) ----------------------------------------------------------- From b33071251f49d22eb54096ef3969c75062222e9e Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Thu, 30 Apr 2020 08:51:48 +0200 Subject: [PATCH 7/7] Update logging.rst Co-Authored-By: Oskar Stark --- logging.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/logging.rst b/logging.rst index 193eb61f40e..cb2e43e5c89 100644 --- a/logging.rst +++ b/logging.rst @@ -159,7 +159,6 @@ to write logs using the :phpfunction:`syslog` function: This defines a *stack* of handlers and each handler is called in the order that it's defined. - Handlers that Modify Log Entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~