yard / logger
Basic replaceable PSR-3 logger
Installs: 244
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7.4
- psr/log: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pestphp/pest: ^1.0
- phpstan/phpstan: ^2.0
- yard/php-cs-fixer-rules: ^1.0
README
Composer package that provides a very basic PSR-3 logger. This logger is just a wrapper around PHP function error_log()
.
As such it does not have any requirements other than psr/log
. It is meant to be used in PHP environments that might not
feature a DI container, like WordPress.
The provided logger is actually meant as a temporary stub. The most prominent feature of this package is that the logger can be replaced by any another PSR-3 logger. This allows projects to push a single logger to its dependencies and achieve consistent log handling.
Installation
To install this package using Composer, follow these steps:
-
Add the following to the
repositories
section of yourcomposer.json
:{ "type": "vcs", "url": "git@github.com:yardinternet/logger.git" }
-
Install this package with Composer:
composer require yard/logger
Usage
Simply use the static Log
facade:
Log::error('Whoops.');
Or get the PSR-3 logger instance from it:
$logger = Log::getLogger();
Projects that wish to replace the logger instance can use the static setLogger()
method, like this:
Log::setLogger(app()->make('log'));
WordPress
When using the logger in WordPress themes, the above replacement method is not recommended, due to the practise of vendor prefixing. The recommended method to replace the logger from a WordPress theme is to:
- Use
do_action()
in WordPress themes to pass the desired logger to any plugins - Use
add_action()
in WordPress plugins to receive a logger instance and set it.
The Log
class provides the WP_ACTION_SET_LOGGER
constant, which contains the name of the WordPress action that should be used. Hooking into the action should look like:
add_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, Log::setLogger(...));
An Acorn based WordPress theme for example could push its Laravel logger like this:
do_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, app()->make('log'));