PHPackages                             fotografde/logging - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. fotografde/logging

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

fotografde/logging
==================

Integrate a common way for log handling.

4.0.3(1y ago)026.0k↑11.5%MITPHPPHP ^8.2CI failing

Since Jul 7Pushed 1y agoCompare

[ Source](https://github.com/fotografde/logging)[ Packagist](https://packagist.org/packages/fotografde/logging)[ RSS](/packages/fotografde-logging/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (40)Used By (0)

Gotphoto logging
================

[](#gotphoto-logging)

Example log format:

```
{
  "@timestamp": "2022-07-06T12:28:54.571699+00:00",
  "@version": 1,
  "timestamp": 1657110534571,
  "host": "maually/automatic defined host",
  "environment": "prod",
  "app": "ServiceName",
  "message": "Something happened",
  "context": {
    "hello": "people"
  },
  "extra": {
    "system-id": "48446546"
  },
  "level": 200,
  "level_name": "INFO",
  "channel": "security",
  "entity.name": "newrelic_defined",
  "entity.type": "newrelic_defined",
  "hostname": "newrelic_defined",
  "trace.id": "newrelic_defined",
  "span.id": "newrelic_defined"
}
```

Exception context
=================

[](#exception-context)

Sometimes we want to log additional data from or when an exception appear. In many case you add additional dependency which is the Logger. We can automize it with Exception context. You can create your custom error and rules how to extract the context from them. See examples in folder ExceptionContext.

And here Guzzle example

```
class GuzzleRequestExceptionContext implements ExceptionContext
{
    /**
     * @return array{message?: string}
     */
    public function __invoke(RequestException $exception): array
    {
        if ($exception->getResponse() !== null && $exception->getResponse()->getBody() !== null) {
            return ['message' => $exception->getResponse()->getBody()->getContents()];
        }
        return [];
    }
}
```

!WARNING: it is working automatically with Symfony integration only now. If you want you always can add integrations for another frameworks.

Configuration
=============

[](#configuration)

Laravel
-------

[](#laravel)

add in `config/logging.php` in `channels` section:

```
        'gotphoto' => [
            'driver' => 'custom',
            'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating,
            'app_name' => 'ServiceName',
            'channel' => 'app'(security/reauest/order),
            'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL
            'level' => Monolog\Logger::INFO, //OPTIONAL
            'stream_to' => 'php://stderr', //OPTIONAL
        ]
        'security' => [
            'driver' => 'custom',
            'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating,
            'app_name' => 'ServiceName',
            'channel' => 'security',
            'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL
            'exceptionContexts' => [ //OPTIONAL
                RequestException::class => [new GuzzleRequestExceptionContext()],
                AwsException::class => [new AwsExceptionContext()],
            ], //OPTIONAL
            'level' => Monolog\Logger::INFO, //OPTIONAL
            'stream_to' => 'php://stderr', //OPTIONAL
        ],
```

Do not forget to set one of them as default one in the same file : `'default' => env('LOG_CHANNEL', 'gotphoto'),`

Symfony
-------

[](#symfony)

Add bundle `Gotphoto\Logging\Symfony\SymfonyLoggingBundle::class => ['all' => true],`;

Add config `gotphoto_logging.yaml`

```
symfony_logging:
 app_name: ServiceName
```

make monolog configuration looks like this

```
