~thirdplace/components

737d1c017f3d9213a782663baa34332ba60e8793 — Dag a month ago 6929ab4
feat: add mailgun logger handler
2 files changed, 12 insertions(+), 4 deletions(-)

M src/http/CurlHttpClient.php
M src/logger/StreamHandler.php
M src/http/CurlHttpClient.php => src/http/CurlHttpClient.php +9 -1
@@ 13,7 13,8 @@ final class CurlHttpClient implements HttpClient
        'max_redirs'        => 5,
        'headers'           => [],
        'body'              => null,
        'max_file_size'     => 5 * 1024**2 // 5MiB
        'max_file_size'     => 5 * 1024**2, // 5MiB,
        'auth'              => [],
    ];

    private $ch;


@@ 22,6 23,7 @@ final class CurlHttpClient implements HttpClient
    public function __construct(array $config = [])
    {
        $this->ch = curl_init();
        // todo: add check for invalid config keys
        $this->config = array_merge(self::CONFIG, $config);
    }



@@ 32,6 34,7 @@ final class CurlHttpClient implements HttpClient

        curl_reset($this->ch);

        // todo: disallow non-http urls
        curl_setopt($this->ch, CURLOPT_URL,                     $url);
        curl_setopt($this->ch, CURLOPT_HEADER,                  false);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,          true);


@@ 61,6 64,11 @@ final class CurlHttpClient implements HttpClient
        }
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $requestHeaders);

        if ($config['auth'] !== []) {
            curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($this->ch, CURLOPT_USERPWD, sprintf('%s:%s', $config['auth']['user'], $config['auth']['pass']));
        }

        $responseHeaders = [];
        curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($ch, $rawHeader) use (&$responseHeaders) {
            $len = strlen($rawHeader);

M src/logger/StreamHandler.php => src/logger/StreamHandler.php +3 -3
@@ 17,7 17,6 @@ final class StreamHandler
        if (isset($record['context']['e'])) {
            $record['context']['e'] = create_sane_stacktrace($record['context']['e']);
        }

        if ($record['context'] === []) {
            $record['context'] = '';
        } else {


@@ 25,11 24,12 @@ final class StreamHandler
            $record['context'] = $json;
        }

        $result = sprintf(
        $text = sprintf(
            "[%s] %s.%s %s %s\n",
            $record['created_at']->format('Y-m-d H:i:s'),
            $record['name'],
            $record['level_name'],
            // todo: replace non-visible characters too such as null byte etc.
            str_replace(["\n", "\r"], '\n', $record['message']),
            $record['context']
        );


@@ 38,7 38,7 @@ final class StreamHandler
            $this->stream = fopen($this->stream, 'a');
        }

        if (!fwrite($this->stream, $result)) {
        if (!fwrite($this->stream, $text)) {
            error_log('Unable to write log record');
        }
    }