@@ -33,8 +33,9 @@ handling for your application. The following options are supported:
3333* ``extraFatalErrorMemory `` - int - Set to the number of megabytes to increase
3434 the memory limit by when a fatal error is encountered. This allows breathing
3535 room to complete logging or error handling.
36- * ``errorLogger `` - \C ake\E rror\E rrorLogger - The class responsible for logging
37- errors and unhandled exceptions.
36+ * ``errorLogger `` - ``Cake\Error\ErrorLoggerInterface `` - The class responsible
37+ for logging errors and unhandled exceptions. Defaults to
38+ ``Cake\Error\ErrorLogger ``.
3839
3940By default, PHP errors are displayed when ``debug `` is ``true ``, and logged
4041when debug is ``false ``. The fatal error handler will be called independent
@@ -57,19 +58,19 @@ Exception handling offers several ways to tailor how exceptions are handled. Ea
5758approach gives you different amounts of control over the exception handling
5859process.
5960
60- #. *Customize the error templates * This allows you to change the rendered view
61+ #. *Custom error templates * This allows you to change the rendered view
6162 templates as you would any other template in your application.
62- #. *Customize the ErrorController * This allows you to control how exception
63+ #. *Custom ErrorController * This allows you to control how exception
6364 pages are rendered.
64- #. *Customize the ExceptionRenderer * This allows you to control how exception
65+ #. *Custom ExceptionRenderer * This allows you to control how exception
6566 pages and logging are performed.
6667#. *Create & register your own error handler * This gives you complete
6768 control over how errors & exceptions are handled, logged and rendered.
6869
6970.. _error-views :
7071
71- Customize Error Templates
72- =========================
72+ Custom Error Templates
73+ ======================
7374
7475The default error handler renders all uncaught exceptions your application
7576raises with the help of ``Cake\Error\ExceptionRenderer ``, and your application's
@@ -92,8 +93,8 @@ data returned by ``getAttributes()`` will be exposed as view variables as well.
9293 **error500 ** templates. In debug mode, you'll see CakePHP's development
9394 error page.
9495
95- Customize the Error Page Layout
96- -------------------------------
96+ Custom Error Page Layout
97+ ------------------------
9798
9899By default error templates use **templates/layout/error.php ** for a layout.
99100You can use the ``layout `` property to pick a different layout::
@@ -108,8 +109,8 @@ Many exceptions raised by CakePHP will render specific view templates in debug
108109mode. With debug turned off all exceptions raised by CakePHP will use either
109110**error400.php ** or **error500.php ** based on their status code.
110111
111- Customize the ErrorController
112- =============================
112+ Custom ErrorController
113+ ======================
113114
114115The ``App\Controller\ErrorController `` class is used by CakePHP's exception
115116rendering to render the error page view and receives all the standard request
@@ -152,14 +153,14 @@ prefix. You could create the following class::
152153This controller would only be used when an error is encountered in a prefixed
153154controller, and allows you to define prefix specific logic/templates as needed.
154155
155- Change the ExceptionRenderer
156- ============================
156+ Custom ExceptionRenderer
157+ ========================
157158
158159If you want to control the entire exception rendering and logging process you
159160can use the ``Error.exceptionRenderer `` option in **config/app.php ** to choose
160161a class that will render exception pages. Changing the ExceptionRenderer is
161- useful when you want to provide custom error pages for application specific
162- exception classes .
162+ useful when you want to change the logic used to create an error controller,
163+ choose the error template, or control how the overall rendering process .
163164
164165Your custom exception renderer class should be placed in **src/Error **. Let's
165166assume our application uses ``App\Exception\MissingWidgetException `` to indicate
@@ -241,16 +242,10 @@ override the ``_getController()`` method in your exception renderer::
241242Creating your Own Error Handler
242243===============================
243244
244- By replacing the error handler you can customize the entire error & exception
245- handling process. By extending ``Cake\Error\BaseErrorHandler `` you can customize
246- display logic more simply. As an example, we could build a class called
247- ``AppError `` to handle our errors::
248-
249- // In config/bootstrap.php
250- use App\Error\AppError;
251-
252- $errorHandler = new AppError();
253- $errorHandler->register();
245+ By replacing the error handler you can customize how PHP errors and exceptions
246+ that are not caught by middleware are handled. By extending
247+ ``Cake\Error\BaseErrorHandler `` you can customize display logic more simply. As
248+ an example, we could build a class called ``AppError `` to handle our errors::
254249
255250 // In src/Error/AppError.php
256251 namespace App\Error;
@@ -270,6 +265,25 @@ display logic more simply. As an example, we could build a class called
270265 }
271266 }
272267
268+ Then we can register our error handler as the PHP error handler::
269+
270+ // In config/bootstrap.php
271+ use App\Error\AppError;
272+
273+ $errorHandler = new AppError();
274+ $errorHandler->register();
275+
276+ Finally, we can use our error handler in the ``ErrorHandlingMiddleware ``::
277+
278+ // in src/Application.php
279+ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
280+ {
281+ $error = new AppError(Configure::read('Error'));
282+ $middleware->add(new ErrorHandlingMiddleware($error));
283+
284+ return $middleware;
285+ }
286+
273287The ``BaseErrorHandler `` defines two abstract methods. ``_displayError() `` is
274288used when errors are triggered. The ``_displayException() `` method is called
275289when there is an uncaught exception.
@@ -296,6 +310,42 @@ standard error page, you can override it::
296310 }
297311 }
298312
313+ Custom Error Logging
314+ ====================
315+
316+ Error handlers use instances of ``Cake\Error\ErrorLoggingInterface `` to create
317+ log messages and log them to the appropriate place. You can replace the error
318+ logger using the ``Error.errorLogger `` configure value. An example error
319+ logger::
320+
321+ namespace App\Error;
322+
323+ use Cake\Error\ErrorLoggerInterface;
324+ use Psr\Http\Message\ServerRequestInterface;
325+ use Throwable;
326+
327+ /**
328+ * Log errors and unhandled exceptions to `Cake\Log\Log`
329+ */
330+ class ErrorLogger implements ErrorLoggerInterface
331+ {
332+ /**
333+ * @inheritDoc
334+ */
335+ public function logMessage($level, string $message, array $context = []): bool
336+ {
337+ // Log PHP Errors
338+ }
339+
340+ public function log(Throwable $exception, ?ServerRequestInterface $request = null): bool
341+ {
342+ // Log exceptions.
343+ }
344+ }
345+
346+ .. versionadded :: 4.1.0
347+ ErrorLoggerInterface was added.
348+
299349.. index :: application exceptions
300350
301351Creating your own Application Exceptions
0 commit comments