Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
</div>
<br>
<p align="center">
<a href="https://github.com/berkanumutlu/php-logger/releases/tag/v1.0.0" target="_blank" rel="nofollow"><img src="https://img.shields.io/github/v/release/berkanumutlu/php-logger?logo=github" alt="PHP Logger Release"></a>
<a href="https://github.com/berkanumutlu/php-logger/releases/tag/v1.0.1" target="_blank" rel="nofollow"><img src="https://img.shields.io/github/v/release/berkanumutlu/php-logger?logo=github" alt="PHP Logger Release"></a>
<a href="https://github.com/berkanumutlu/php-logger/stargazers" rel="nofollow"><img src="https://img.shields.io/github/stars/berkanumutlu/php-logger?style=flat&logo=github" alt="PHP Logger Repo stars"></a>
<a href="https://github.com/berkanumutlu/php-logger/blob/master/LICENSE" target="_blank" rel="nofollow"><img src="https://img.shields.io/github/license/berkanumutlu/laravel-example-app" alt="License"></a>
<a href="https://www.php.net/releases/5_6_0.php" target="_blank" rel="nofollow"><img src="https://img.shields.io/badge/PHP->=v5.6-777BB4?logo=php&logoColor=white&labelColor=777BB4" alt="PHP Version"></a>
<a href="https://github.com/php-fig/log" target="_blank" rel="nofollow"><img src="https://img.shields.io/badge/PHP%20for%20psr/log->=v8.0.0-777BB4?logo=php&logoColor=white&labelColor=777BB4" alt="psr/log Version"></a>
</p>

# PHP Request Logger
Expand Down
39 changes: 37 additions & 2 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use App\Library\FileLogger;

require_once '../vendor/autoload.php';
require_once 'config/db.php';
$date = date('Y-m-d H:i:s');
Expand Down Expand Up @@ -57,7 +60,19 @@ function logRequestToDatabase($pdo, $data)
]);
}

try {
/**
* @return void
*/
function logFileWithPsrLog($data)
{
$logger = new FileLogger();
$logger->debug('Log from index.php', $data);
}

/**
* Request logged with manually
*/
/*try {
header("Content-Type: application/json; charset=UTF-8");
$response = new \App\Library\Response();
$db = \App\Config\DB\getDatabaseConnection();
Expand All @@ -66,7 +81,27 @@ function logRequestToDatabase($pdo, $data)
logRequestToDatabase($db, $requestData);
$response->setStatus(true);
$response->setStatusCode(200);
$response->setMessage("Request logged.");
$response->setMessage("Request logged with manually.");
$response->setDate($date);
echo $response->toJson();
exit();
} catch (\Exception $e) {
throw new \Exception($e->getMessage(), (int) $e->getCode());
}*/

/**
* Request logged with psr/log package
*/
try {
header("Content-Type: application/json; charset=UTF-8");
$response = new \App\Library\Response();
$db = \App\Config\DB\getDatabaseConnection();
$requestData = getRequestData();
logFileWithPsrLog($requestData);
logRequestToDatabase($db, $requestData);
$response->setStatus(true);
$response->setStatusCode(200);
$response->setMessage("Request logged with psr/log package.");
$response->setDate($date);
echo $response->toJson();
exit();
Expand Down
79 changes: 79 additions & 0 deletions src/lib/FileLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Library;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class FileLogger implements LoggerInterface
{
public function emergency($message, array $context = []): void
{
// Use the level from LogLevel class
$this->log(LogLevel::EMERGENCY, $message, $context);
}

public function alert($message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}

public function critical($message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

public function error($message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}

public function warning($message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}

public function notice($message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}

public function info($message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}

public function debug($message, array $context = []): void
{
$this->log(LogLevel::DEBUG, $message, $context);
}

public function log($level, $message, array $context = []): void
{
// Current date in 1970-12-01 23:59:59 format
$dateFormatted = date('Y-m-d H:i:s');

// Build the message with the current date, log level,
// and the string from the arguments
$contextString = json_encode($context);
$message = sprintf(
'[%s] %s: %s %s%s',
$dateFormatted,
$level,
$message,
$contextString,
PHP_EOL // Line break
);

// Writing to the file
$logDir = './logs';
if (!file_exists($logDir)) {
mkdir($logDir, 0777, true);
}
$logFileName = date('Y-m-d').'.log';
$logFile = $logDir.'/'.$logFileName;
file_put_contents($logFile, $message, FILE_APPEND);
// FILE_APPEND flag prevents flushing the file content on each call
// and simply adds a new string to it
}
}