Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions en/appendices/5-1-upgrade-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Http
events when requests are sent. You can use these events to perform logging,
caching or collect telemetry.

TestSuite
---------

- ``LogTestTrait`` was added. This new trait makes it easy to capture logs in
your tests and make assertions on the presence or absence of log messages.

Validation
----------

Expand Down
53 changes: 51 additions & 2 deletions en/core-libraries/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ following keys:
used. See ``syslog`` documentation for more options

Creating Log Engines
=====================
====================

Log engines can be part of your application, or part of
plugins. If for example you had a database logger called
Expand Down Expand Up @@ -347,7 +347,7 @@ interface as it only requires you to implement the ``log()`` method.
.. _logging-formatters:

Logging Formatters
------------------
==================

Logging formatters allow you to control how log messages are formatted
independent of the storage engine. Each core provided logging engine comes with
Expand Down Expand Up @@ -378,6 +378,55 @@ To implement your own logging formatter you need to extend
method you need to implement is ``format($level, $message, $context)`` which is
responsible for formatting log messages.

.. _log-testing:

Testing Logs
============

To test logging, add ``Cake\TestSuite\LogTestTrait`` to your test case. The
``LogTestTrait`` uses PHPUnit hooks to attach log engines that intercept the log
messages your application is making. Once you have captured logs you can perform
assertions on log messages your application is emitting. For example::

namespace App\Test\TestCase\Controller;

use Cake\TestSuite\LogTestTrait;
use Cake\TestSuite\TestCase;

class UsersControllerTest extends TestCase
{
use LogTestTrait;

public function setUp(): void
{
parent::setUp();
$this->setupLog([
'error' => ['scopes' => ['app.security']]
]);
}

public function testResetPassword()
{
$this->post('/users/resetpassword', ['email' => '[email protected]']);
$this->assertLogMessageContains('info', '[email protected] reset password', 'app.security');
}
}

You use ``setupLog()`` to define the log messages you wish to capture and
perform assertions on. After logs have been emitted you can make assertions on
the contents of logs, or the absence of them:

* ``assertLogMessage(string $level, string $expectedMessage, ?string $scope
= null, string $failMsg = '')`` Assert that a log message was found.
* ``assertLogMessageContains(string $level, string $expectedMessage, ?string
$scope = null, string $failMsg = '')`` Assert that a log message contains the
substring.
* ``assertLogAbsent(string $level, ?string $failMsg = '')`` Assert that no log
messages of the given level were captured.

The ``LogTestTrait`` will automatically clean up any loggers that were
configured.

Log API
=======

Expand Down
5 changes: 5 additions & 0 deletions en/development/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,11 @@ Testing Email

See :ref:`email-testing` for information on testing email.

Testing Logging
===============

See :ref:`log-testing` for information on testing log messages.

Creating Test Suites
====================

Expand Down