https://github.com/nostadt/php-psr3-log-context
Structured PSR-3 Logging
https://github.com/nostadt/php-psr3-log-context
logging php psr-3
Last synced: about 1 month ago
JSON representation
Structured PSR-3 Logging
- Host: GitHub
- URL: https://github.com/nostadt/php-psr3-log-context
- Owner: nostadt
- Created: 2023-07-27T10:54:53.000Z (almost 3 years ago)
- Default Branch: development
- Last Pushed: 2024-01-06T19:20:51.000Z (over 2 years ago)
- Last Synced: 2025-03-21T07:16:10.440Z (over 1 year ago)
- Topics: logging, php, psr-3
- Language: PHP
- Homepage:
- Size: 3.84 MB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PSR-3 Log-Context - Structured PSR-3 Logging
[](https://github.com/nostadt/php-psr3-log-context/actions/workflows/default.yml)
[](https://packagist.org/packages/nostadt/psr3-log-context)
## Preamble
Especially for larger projects logging is essential. You do want to have all the data you need when looking in the past.
Nothing is worse than having a serious issue on the production system but lacking of helpful context information.
This small package makes it easy to log in a structured manner without cluttering your code.
## Available classes and interface
| Class | Description |
|--------------------------------------|--------------------------------------------------------------------------------|
| `LogData` | Key=>Value combo to forbid bad values |
| `LogContext` | Storage for `LogData` and `toArray` method that will be passed as 2nd argument |
| `LogContextConvertibleInterface` | Make any custom class LogContext compatible. |
**Further link/s**
- https://www.php-fig.org/psr/psr-3/
## Examples
**Use LogContext::createFromException**
This is the recommended way when dealing with Exceptions, unless they implement `LogContextConvertibleInterface`.
```php
logger->warning(
$exception->getMessage(),
LogContext::createFromException($exception)->toArray()
);
}
```
**Use LogContextConvertibleInterface**
This is the recommend way, because it truly simplifies creating the log-context array.
```php
id),
new \Nostadt\Psr3LogContext\ValueObject\LogData('user_activated', $this->activated ? 'true' : 'false'),
new \Nostadt\Psr3LogContext\ValueObject\LogData('user_name', $this->name),
);
}
}
$user = new User();
$user->id = 1;
$user->activated = true;
$user->name = 'John Doe';
$logger->warning('My Message', $user->asLogContext()->toArray());
```
**Merge multiple LogContexts**
With previous `User`-class in mind we can merge LogContext-objects.
```php
logger->warning(
$exception->getMessage(),
LogContext::createFromException($exception)->mergeLogContext($user->asLogContext())->toArray()
);
}
```
**Create a LogContext from the scratch**
This can be used in situations in which `LogContext` is not available.
```php
warning('My Message', $logContext->toArray());
```
## Development
**Requirements:**
- [Docker](https://www.docker.com/)
- [Make](https://www.selflinux.org/selflinux/html/make01.html)
If you start fresh, execute:
```bash
make init
make start
```
If you want to run code-quality checks, execute:
```bash
make test
make lint
```
If you are done working, execute:
```bash
make stop
```
If you want to continue working, execute:
```bash
make start
```
If you want to clean up the system, execute:
```bash
make clean
```