https://github.com/yardinternet/logger
Replaceable PSR-3 logger
https://github.com/yardinternet/logger
Last synced: 4 months ago
JSON representation
Replaceable PSR-3 logger
- Host: GitHub
- URL: https://github.com/yardinternet/logger
- Owner: yardinternet
- License: mit
- Created: 2025-01-09T13:53:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-02-24T00:34:29.000Z (4 months ago)
- Last Synced: 2026-02-24T07:36:35.589Z (4 months ago)
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Replaceable PSR-3 Logger
[](https://github.com/yardinternet/logger/actions/workflows/format-php.yml)
[](https://github.com/yardinternet/logger/actions/workflows/phpstan.yml)
[](https://github.com/yardinternet/logger/actions/workflows/run-tests.yml)
[](https://github.com/yardinternet/logger/actions/workflows/badges.yml)
[](https://github.com/yardinternet/logger/actions/workflows/badges.yml)
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:
1. Add the following to the `repositories` section of your `composer.json`:
```json
{
"type": "vcs",
"url": "git@github.com:yardinternet/logger.git"
}
```
2. Install this package with Composer:
```sh
composer require yard/logger
```
## Usage
Simply use the static `Log` facade:
```php
Log::error('Whoops.');
```
Or get the PSR-3 logger instance from it:
```php
$logger = Log::getLogger();
```
Projects that wish to replace the logger instance can use the static `setLogger()` method, like this:
```php
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:
1. Use `do_action()` in WordPress themes to pass the desired logger to any plugins
2. 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:
```php
add_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, Log::setLogger(...));
```
An [Acorn](https://roots.io/acorn/) based WordPress theme for example could push its Laravel logger like this:
```php
do_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, app()->make('log'));
```