assimtech/dislog-bundle

Dislog symfony 扩展包

3.2.3 2022-08-02 04:30 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Scrutinizer Code Quality Code Coverage

Dislog Bundle 为 assimtech/dislog 提供了 symfony ^3|^4|^5 集成,它是一个 API 调用记录器。

安装

使用 composer 安装

composer require assimtech/dislog-bundle

启用扩展包 Assimtech\DislogBundle\AssimtechDislogBundle.

配置处理器

assimtech_dislog:
    handler:
        stream:
            resource: '/tmp/my.log'

开始记录您的 API 调用

$request = '<request />';

/** @var \Assimtech\Dislog\ApiCallLoggerInterface $apiCallLogger */
$apiCall = $apiCallLogger->logRequest($request, $endpoint, $appMethod, $reference);

$response = $api->transmit($request);

$this->apiCallLogger->logResponse($apiCall, $response);

有关更高级的用法,请参阅 assimtech/dislog

删除旧的 API 调用

bin/console assimtech:dislog:remove

要记录来自 PSR-18 客户端的 HTTP 请求,您可以使用服务 assimtech_dislog.logging_http_client

/**
 * @var \Psr\Http\Message\RequestInterface $request
 * @var \Assimtech\Dislog\LoggingHttpClientInterface $httpClient
 * @var \Psr\Http\Message\ResponseInterface $response
 */
$response = $httpClient->sendRequest(
    $request,
    $appMethod,
    $reference,
    $requestProcessors,
    $responseProcessors,
    $deferredLogging
);

处理器配置

Doctrine 对象管理器

该扩展包中包含的 doctrine 映射定义有意放置在非默认路径中,以防止意外加载到错误的对象管理器中。

例如,如果您有一个应用程序,它同时使用 Doctrine\ORM(用于您的正常应用程序实体)以及 Doctrine\ODM(用于 Dislog),我们不希望 Doctrine\ORM 从 DislogBundle 的 ApiCall.orm.xml 中检测并加载映射信息。如果它做到了,那么如果您还使用了 doctrine:schema:update 或 Doctrine 迁移,您可能最终会创建一个表。

这意味着 Dislog 的映射信息必须手动加载。

警告:建议不要使用应用程序的默认实体管理器作为 flush(),因为 dislog 的 flush() 可能会干扰您的应用程序。

Doctrine ODM

使用 DoctrineMongoDBBundle 添加映射信息的示例

doctrine_mongodb:
    document_managers:
        default:
            # Your main application document manager config
        dislog:
            connection: default
            mappings:
                AssimtechDislogBundle:
                    type: xml
                    prefix: Assimtech\Dislog\Model
                    dir: Resources/config/doctrine/mongodb

assimtech_dislog:
    handler:
        doctrine_document_manager:
            document_manager: doctrine_mongodb.odm.dislog_document_manager

有关更高级的设置,请参阅 DoctrineMongoDBBundle 配置

Doctrine ORM

使用 DoctrineBundle 添加映射信息的示例

doctrine:
    orm:
        entity_managers:
            default:
                # Your main application entity manager config
            dislog:
                connection: default
                mappings:
                    AssimtechDislogBundle:
                        type: xml
                        prefix: Assimtech\Dislog\Model
                        dir: Resources/config/doctrine/orm

assimtech_dislog:
    handler:
        doctrine_entity_manager:
            entity_manager: doctrine.orm.dislog_entity_manager

有关更高级的设置,请参阅 DoctrineBundle 配置

服务

您可以使用自己的实现了 Assimtech\Dislog\ApiCallLoggerInterface 的 logger 服务。

assimtech_dislog:
    handler:
        service:
            name: App\Dislog\ApiLogger

注意:您负责在持久化之前通过任何处理器传递请求/响应。实现自定义 logger 的最简单方法是通过扩展默认的 logger。

namespace App\Dislog;

use Assimtech\Dislog\Model\ApiCallInterface;
use Assimtech\Dislog\ApiCallLogger;

class ApiLogger extends ApiCallLogger
{
    public function logRequest(
        ?string $request,
        ?string $endpoint,
        ?string $appMethod,
        string $reference = null,
        /* callable[]|callable */ $processors = []
    ): ApiCallInterface {
        $processedRequest = $this->processPayload($processors, $request);

        $apiCall = $this->apiCallFactory->create();
        $apiCall
            ->setRequest($processedRequest)
            ->setEndpoint($endpoint)
            ->setMethod($appMethod)
            ->setReference($reference)
            ->setRequestTime(microtime(true))
        ;

        // Persist $apiCall somewhere

        return $apiCall;
    }

    public function logResponse(
        ApiCallInterface $apiCall,
        string $response = null,
        /* callable[]|callable */ $processors = []
    ): void {
        $duration = microtime(true) - $apiCall->getRequestTime();

        $processedResponse = $this->processPayload($processors, $response);

        $apiCall
            ->setResponse($processedResponse)
            ->setDuration($duration)
        ;

        // Update the persisted $apiCall
    }
}

assimtech_dislog:
    handler:
        stream:
            identity_generator: Assimtech\Dislog\Identity\UniqueIdGenerator
            resource: /tmp/dis.log
            serializer: Assimtech\Dislog\Serializer\StringSerializer

配置参考

assimtech_dislog:
    api_call_factory: assimtech_dislog.api_call.factory # Api Call Factory service name

    max_age: 2592000 # seconds to keep logs for

    handler:
        # *One* of the following sections must be configured, none are enable by default

        doctrine_document_manager:
            document_manager: ~ # document manager service name

        doctrine_entity_manager:
            entity_manager: ~ # entity manager service name

        service:
            name: ~ # Your custom handler service name

        stream:
            resource: ~ # Either a stream path ("/tmp/my.log", "php://stdout") or a stream resource (see fopen)
            identity_generator: Assimtech\Dislog\Identity\UniqueIdGenerator # Identity Generator service name
            serializer: Assimtech\Dislog\Serializer\StringSerializer # Serializer service name

    preferences:
        suppress_handler_exceptions: false # By default, api call logging exceptions are suppressed (they still get emitted as warnings to the psr_logger if any)
        endpoint_max_length: null # By default, limits endpoint max length. Recommended 255 if using a VARCHAR 255 in the storage layer
        method_max_length: null # By default, limits endpoint max length. Recommended 255 if using a VARCHAR 255 in the storage layer
        reference_max_length: null # By default, limits endpoint max length. Recommended 255 if using a VARCHAR 255 in the storage layer

    psr_logger: logger # (Optional) Psr-3 logger service name