Skip to content

Commit

Permalink
Create Logger.php
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisnikoy committed Feb 21, 2024
1 parent 8bab14c commit 0daa858
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Athos\Foundation;

/**
* Logger
* Log REST requests and responses
*
* @package athos-foundation
* @author Jannis Nikoy <[email protected]>
* @license MIT
* @link https://github.com/jannisnikoy/athos-foundation
*/

class Logger {
/**
* Log data to exm_logs and print output in JSON
*
* @param int $statusCode HTTP status code
* @param mixed $response Optional response payload
*/
public static function printOutput($statusCode, $response) {
global $db;

http_response_code($statusCode);
if(isset($response)) {
echo json_encode($response, isset($_GET['prettify']) && $_GET['prettify'] == 'true' ? JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT : JSON_NUMERIC_CHECK);
}
$db->query("INSERT INTO exm_logs(status_code, method, ipaddress, path, headers, request, response) VALUES(?, ?, ?, ?, ?, ?, ?)", $statusCode, $_SERVER['REQUEST_METHOD'], $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], json_encode(getallheaders()), file_get_contents('php://input'), json_encode($response));
}
}
?>

0 comments on commit 0daa858

Please sign in to comment.