PHPackages                             brezgalov/yii2-external-api-logger - 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. brezgalov/yii2-external-api-logger

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

brezgalov/yii2-external-api-logger
==================================

logger for api requests

045PHP

Since Oct 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Brezgalov/yii2-external-api-logger)[ Packagist](https://packagist.org/packages/brezgalov/yii2-external-api-logger)[ RSS](/packages/brezgalov-yii2-external-api-logger/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Description
-----------

[](#description)

This component helps you log external api interaction

Installation
------------

[](#installation)

> composer require brezgalov/yii2-external-api-logger

Usage
-----

[](#usage)

Specify logger as an application component at your config file:

```
[
    bootstrap' => [
        'logger',
    ],
    'components' => [
        'logger' => [
            'class' => LoggerComponent::class,
            'logsStorage' => LogsStorageDb::class,
        ],
    ],
],

```

> LogsStorageDb class requires applying migration from *src/LogsStorageDb/Migrations*

I prefer using [yiisoft/yii2-httpclient](https://github.com/yiisoft/yii2-httpclient)as client and [brezgalov/activity-id](https://github.com/Brezgalov/activity-id) as ActivityId helper

Feel free to use any client you want. ActivityId is obligatory constructor parameter tho. It serves as a Primary Key for db logs storage.

You could use `uniqid()` instead of activity-id lib, or any unique identifier you prefer.

```
// create activityId to bind logs all the way through
$activityId = \Yii::createObject(ActivityId::class, ['name' => Auth::ACTIVITY_ID_LOGIN]) ;

// bind request data
$eventRequestSent = \Yii::createObject(EventExternalApiRequestSent::class, ['activityId' => (string)$activityId,]);

$eventRequestSent->method = $request->getMethod();
$eventRequestSent->url = $request->getFullUrl();
$eventRequestSent->input = $request->getData();
$eventRequestSent->requestGroup = Auth::REQUEST_GROUP;
$eventRequestSent->requestId = Auth::REQUEST_ID_LOGIN;

// trigger event before sending request

\Yii::$app->trigger(LoggerComponent::EVENT_EXTERNAL_API_REQUEST_SENT, $eventRequestSent);

// send request

$response = $request->send();

// bind response params

$eventResponseReceived = \Yii::createObject(EventExternalApiResponseReceived::class, ['activityId' => (string)$activityId]);
$eventResponseReceived->statusCode = $response->statusCode;
$eventResponseReceived->response = $response->getContent();

// trigger event after response is received

\Yii::$app->trigger(LoggerComponent::EVENT_EXTERNAL_API_RESPONSE_RECEIVED, $eventResponseReceived);

// some usefull example code further

if ($response->isOk) {
    ...
}

```

Usage inside SQL Transaction
----------------------------

[](#usage-inside-sql-transaction)

While you log api request inside transaction - any error could ruin yor log info. **LogApiRequestDelayedBehavior** is an option to escape such issue.

Replace your component setup with:

```
[
    bootstrap' => [
        'logger',
    ],
    'components' => [
        'logger' => [
            'class' => DelayedLoggerComponent::class,
            'logsStorage' => LogsStorageDb::class,
            'fireStorageEvent' => \yii\base\Application::EVENT_AFTER_ACTION,
        ],
    ],
],

```

Then replace previous logger events trigger code with:

```
    // create activityId to bind logs all the way through
    $activityId = (string)\Yii::createObject(ActivityId::class, ['name' => Auth::ACTIVITY_ID_SEND_SMS_CODE]);

    $request = // build \yii\httpclient\Request;

    // fill up request fields
    $requestLogDto = \Yii::createObject(ApiLogFullDto::class);
    $requestLogDto->activityId = $activityId;
    $requestLogDto->requestTime = time();
    $requestLogDto->method = $request->getMethod();
    $requestLogDto->url = $request->getFullUrl();
    $requestLogDto->input = $request->getData();
    $requestLogDto->requestGroup = Auth::REQUEST_GROUP;
    $requestLogDto->requestId = Auth::REQUEST_ID_SEND_SMS;

    $response = $request->send();

    // fill up response fields
    $requestLogDto->responseTime = time();
    $requestLogDto->statusCode = $response->statusCode;
    $requestLogDto->responseContent = $response->getContent();

    // delay event

    /** @var DelayedLoggerComponent $loggerComponent */
    $loggerComponent = \Yii::$app->get('logger');
    $loggerComponent->delayLogDto($requestLogDto);

```

Next fire configured "fireStorageEvent" event outside of transaction block. I suggest to use EVENT\_AFTER\_ACTION event.

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba5437e2f11c8cf8548822ad3c8e80eb9f9e84d266daf340e72681195aa7810d?d=identicon)[Brezgalov](/maintainers/Brezgalov)

---

Top Contributors

[![Brezgalov](https://avatars.githubusercontent.com/u/30072225?v=4)](https://github.com/Brezgalov "Brezgalov (17 commits)")

### Embed Badge

![Health badge](/badges/brezgalov-yii2-external-api-logger/health.svg)

```
[![Health](https://phpackages.com/badges/brezgalov-yii2-external-api-logger/health.svg)](https://phpackages.com/packages/brezgalov-yii2-external-api-logger)
```

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M138](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[datadog/php-datadogstatsd

An extremely simple PHP datadogstatsd client

19124.6M15](/packages/datadog-php-datadogstatsd)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
