community/feature-request#12 - Allow named logging channels
authorTim Otten <totten@civicrm.org>
Fri, 16 Apr 2021 01:49:27 +0000 (18:49 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 16 Apr 2021 02:00:10 +0000 (19:00 -0700)
commitc213eb51ec4b39dfc581c31025318e42cdaccb7a
treef51d937cf5c7d11da730ab26b71488ef4b8a80f0
parent8cd618ab10dc5a297b977f8834367a927f44a123
community/feature-request#12 - Allow named logging channels

Overview
----------------------------------------

Make it easier to route log messages based on their topic (e.g. CiviContribute-related logs vs CiviMail-related logs).

Before
------

`Civi::log()` always returns the same instance of `LoggerInterface`, with no
clear way to differentiate logs of different business subsystems.

After
-----

`Civi::log(...)` allows you to optionally request a `LoggerInterface` for a specific theme, e.g.

```php
Civi::log('mail')->error('Failed to connect to SMTP server');
Civi::log('ipn')->warning('Transaction rejected by payment processor');
```

Technical Details
-----------------

A few things going on here:

* Extensions may start using their own logs (`Civi::log('myext')`) without any special effort.
* It is possible to replace or customize specific logs by defining a service `log.CHANNEL_NAME`.
* The `psr_log_manager` is a service. An extension like https://lab.civicrm.org/extensions/monolog/
  can replace the `psr_log_manager` and use the channel-name in its own way.

There is a limitation here in that the list of channels is open-ended.  It
will be impossible to (eg) detect that a log-user has made a typo in the
channel-name.  However, this seems like the better trade-off if the
alternative is that extensions face races during
installation/uninstallation.
Civi.php
Civi/Core/Container.php
Civi/Core/LogManager.php [new file with mode: 0644]