Skip to content

Commit

Permalink
Extend Config::init() with an options array parameter
Browse files Browse the repository at this point in the history
This is extensible for the future.
  • Loading branch information
mstilkerich committed Nov 1, 2021
1 parent 13fc8d3 commit 669f0ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,8 +2,8 @@

## Unreleased (to 1.2.1)

- Option to set loglevel for HTTP requests in Config. The prior verbose log format will only be used if the level is set
to debug. A shorted version without the full requests/responses is used for other loglevels.
- Config::init() now accepts an options array as third parameter, which currently allows to customize the log format for
the HTTP logs. It is meant to be extended with further options in the future.

## Version 1.2.1 (to 1.2.0)

Expand Down
47 changes: 41 additions & 6 deletions src/Config.php
Expand Up @@ -18,30 +18,65 @@
*
* @package Public\Infrastructure
*
* @psalm-type Loglevel = 'emergency'|'alert'|'critical'|'error'|'warning'|'notice'|'info'|'debug'
* @psalm-type LibOptionsInput = array {
* guzzle_logformat?: string,
* }
*
* @psalm-type LibOptions = array {
* guzzle_logformat: string,
* }
*/
class Config
{
public const GUZZLE_LOGFMT_DEBUG =
'"{method} {target} HTTP/{version}" {code}' . "\n>>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}";

public const GUZZLE_LOGFMT_SHORT =
'"{method} {target} HTTP/{version}" {code} {res_header_Content-Length}';

/** @var LoggerInterface */
public static $logger;

/** @var LoggerInterface */
public static $httplogger;

/** @var Loglevel $httpLoglevel The loglevel to use for the HTTP logger. Affects the amount of data logged. */
public static $httpLoglevel = 'info';
/**
* Configuration options of the library.
* @var LibOptions
* @psalm-readonly-allow-private-mutation
*/
public static $options = [
'guzzle_logformat' => self::GUZZLE_LOGFMT_DEBUG,
];

/**
* @psalm-param Loglevel $httpLoglevel The loglevel to use for the HTTP logger. Affects the amount of data logged.
* Initialize the library.
*
* The functions accepts two logger objects complying with the standard PSR-3 Logger interface, one of which is used
* by the carddavclient lib itself, the other is passed to the HTTP client library to log the HTTP traffic. Pass
* null for any logger to disable the corresponding logging.
*
* The $options parameter allows to override the default behavior of the library in various options. It is an
* associative array that may have the following keys and values::
* - guzzle_logformat(string):
* Value is a string defining the HTTP log format when Guzzle is used as HTTP client library. See the
* documentation of the Guzzle MessageFormatter class for the available placeholders. This class offers two
* constants that can be used as template:
* - {@see Config::GUZZLE_LOGFMT_SHORT}: A compact log format with only request type/URI and the response status
* and content length
* - {@see Config::GUZZLE_LOGFMT_DEBUG}: The default, which logs the full HTTP traffic including request bodies
* and response bodies.
*
* @psalm-param LibOptionsInput $options Options to override defaults.
*/
public static function init(
LoggerInterface $logger = null,
LoggerInterface $httplogger = null,
string $httpLoglevel = 'info'
array $options = []
): void {
self::$logger = $logger ?? new NullLogger();
self::$httplogger = $httplogger ?? new NullLogger();
self::$httpLoglevel = $httpLoglevel;
self::$options = $options + self::$options;
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/HttpClientAdapterGuzzle.php
Expand Up @@ -16,6 +16,7 @@
use Psr\Http\Message\ResponseInterface as Psr7Response;
use Psr\Http\Message\UriInterface as Psr7Uri;
use Psr\Http\Client\ClientInterface as Psr18ClientInterface;
use Psr\Log\NullLogger;
use MStilkerich\CardDavClient\Exception\{ClientException, NetworkException};

/**
Expand Down Expand Up @@ -119,14 +120,12 @@ public function __construct(string $base_uri, array $credentials)

$stack = HandlerStack::create();

$msgFormat = (Config::$httpLoglevel == "debug")
? "\"{method} {target} HTTP/{version}\" {code}\n" . MessageFormatter::DEBUG
: "\"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}";

$stack->push(Middleware::log(
Config::$httplogger,
new MessageFormatter($msgFormat)
));
if (!(Config::$httplogger instanceof NullLogger)) {
$stack->push(Middleware::log(
Config::$httplogger,
new MessageFormatter(Config::$options['guzzle_logformat'])
));
}

$guzzleOptions = $this->prepareGuzzleOptions();
$guzzleOptions['handler'] = $stack;
Expand Down

0 comments on commit 669f0ff

Please sign in to comment.