use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
* Formats log messages using variable substitutions for requests, responses,
* and other transactional data.
* The following variable substitutions are supported:
* - {request}: Full HTTP request message
* - {response}: Full HTTP response message
* - {ts}: ISO 8601 date in GMT
* - {date_iso_8601} ISO 8601 date in GMT
* - {date_common_log} Apache common log date using the configured timezone.
* - {host}: Host of the request
* - {method}: Method of the request
* - {uri}: URI of the request
* - {version}: Protocol version
* - {target}: Request target of the request (path + query + fragment)
* - {hostname}: Hostname of the machine that sent the request
* - {code}: Status code of the response (if available)
* - {phrase}: Reason phrase of the response (if available)
* - {error}: Any error messages (if available)
* - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message
* - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message
* - {req_headers}: Request headers
* - {res_headers}: Response headers
* - {req_body}: Request body
* - {res_body}: Response body
* Apache Common Log Format.
* @link http://httpd.apache.org/docs/2.4/logs.html#common
const CLF = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}";
const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}";
const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}';
/** @var string Template used to format log messages */
* @param string $template Log message template
public function __construct($template = self::CLF)
$this->template = $template ?: self::CLF;
* Returns a formatted message string.
* @param RequestInterface $request Request that was sent
* @param ResponseInterface $response Response that was received
* @param \Exception $error Exception that was received
RequestInterface $request,
ResponseInterface $response = null,
return preg_replace_callback(
'/{\s*([A-Za-z_\-\.0-9]+)\s*}/',
function (array $matches) use ($request, $response, $error, &$cache) {
if (isset($cache[$matches[1]])) {
return $cache[$matches[1]];
$result = Psr7\str($request);
$result = $response ? Psr7\str($response) : '';
$result = trim($request->getMethod()
. ' ' . $request->getRequestTarget())
. ' HTTP/' . $request->getProtocolVersion() . "\r\n"
. $this->headers($request);
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
) . "\r\n" . $this->headers($response)
$result = $request->getBody();
$result = $response ? $response->getBody() : 'NULL';
$result = date('d/M/Y:H:i:s O');
$result = $request->getMethod();
$result = $request->getProtocolVersion();
$result = $request->getUri();
$result = $request->getRequestTarget();
$result = $request->getProtocolVersion();
? $response->getProtocolVersion()
$result = $request->getHeaderLine('Host');
$result = $response ? $response->getStatusCode() : 'NULL';
$result = $response ? $response->getReasonPhrase() : 'NULL';
$result = $error ? $error->getMessage() : 'NULL';
// handle prefixed dynamic headers
if (strpos($matches[1], 'req_header_') === 0) {
$result = $request->getHeaderLine(substr($matches[1], 11));
} elseif (strpos($matches[1], 'res_header_') === 0) {
? $response->getHeaderLine(substr($matches[1], 11))
$cache[$matches[1]] = $result;
* Get headers from message as string
private function headers(MessageInterface $message)
foreach ($message->getHeaders() as $name => $values) {
$result .= $name . ': ' . implode(', ', $values) . "\r\n";