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
2 changes: 2 additions & 0 deletions app/Dtos/PostRequestDto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Dtos;

use Infra\Interfaces\RequestDtoInterface;
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "clebson/pure-php-oop",
"type": "project",
"autoload": {
"files": [
"infra/helper.php"
],
"psr-4": {
"App\\": "app/",
"Infra\\": "infra/",
Expand Down
2 changes: 2 additions & 0 deletions infra/Interfaces/RequestDtoInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Infra\Interfaces;

interface RequestDtoInterface
Expand Down
95 changes: 95 additions & 0 deletions infra/Support/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Infra\Support;

use RuntimeException;

class Log
{
const LEVEL_INFO = 'INFO';

const LEVEL_DEBUG = 'DEBUG';

const LEVEL_WARN = 'WARN';

const LEVEL_ERROR = 'ERROR';

private static ?self $instance = null;

private string $filePath;

/** @var resource */
private $fileHandle;

private function __construct()
{
$logsDirectory = storage_path().'/logs';
if (! file_exists($logsDirectory)) {
// @codeCoverageIgnoreStart
mkdir($logsDirectory, 0755, true);
// @codeCoverageIgnoreEnd
}

$today = date('Ymd');
$this->filePath = $logsDirectory."/app-{$today}.log";

/** @todo delete old log files */
$fileHandle = fopen($this->filePath, 'a');

if (! $fileHandle) {
// @codeCoverageIgnoreStart
throw new RuntimeException("Unable to open log file: $this->filePath");
// @codeCoverageIgnoreEnd
}

$this->fileHandle = $fileHandle;
}

public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self;
}

return self::$instance;
}

public function info(string $message): void
{
fwrite($this->fileHandle, $this->buildMessage($message, self::LEVEL_INFO));
}

public function debug(string $message): void
{
fwrite($this->fileHandle, $this->buildMessage($message, self::LEVEL_DEBUG));
}

public function warn(string $message): void
{
fwrite($this->fileHandle, $this->buildMessage($message, self::LEVEL_WARN));
}

public function error(string $message): void
{
fwrite($this->fileHandle, $this->buildMessage($message, self::LEVEL_ERROR));
}

private function buildMessage(string $message, string $level): string
{
/** @todo php timezone is set to UTC, find a way to make it configurable */
$timestamp = '['.date('Y-m-d H:i:s', time())."] $level: ";

return $timestamp.$message.PHP_EOL;
}

public function __destruct()
{
// @codeCoverageIgnoreStart
if ($this->fileHandle !== null) {
fclose($this->fileHandle);
}
// @codeCoverageIgnoreEnd
}
}
41 changes: 41 additions & 0 deletions infra/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

use Infra\Support\Log;

function root_path(): string
{
return dirname(__DIR__);
}

function storage_path(): string
{
return root_path().'/storage';
}

function info(string $message): void
{
$logger = Log::getInstance();

$logger->info($message);
}

function debug(string $message): void
{
$logger = Log::getInstance();

$logger->debug($message);
}
function error(string $message): void
{
$logger = Log::getInstance();

$logger->error($message);
}
function warn(string $message): void
{
$logger = Log::getInstance();

$logger->warn($message);
}
3 changes: 1 addition & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
} catch (Throwable $e) {
// Case the application crash
// Log the actual error for the developer
// @todo Create proper Application logger
error_log($e->getMessage());
error((string) $e);

// Show a generic error to the user
http_response_code(500);
Expand Down
2 changes: 2 additions & 0 deletions storage/logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
53 changes: 53 additions & 0 deletions tests/Feature/infra/Support/LogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

describe('Log', function () {
it('put info message in log file', function () {
$message = 'test info message';

info($message);

$today = date('Ymd');
$logFilePath = storage_path()."/logs/app-$today.log";
$logFile = file_get_contents($logFilePath);

expect(str_contains($logFile, $message))->toBeTrue();
});

it('put debug message in log file', function () {
$message = 'test debug message';

debug($message);

$today = date('Ymd');
$logFilePath = storage_path()."/logs/app-$today.log";
$logFile = file_get_contents($logFilePath);

expect(str_contains($logFile, $message))->toBeTrue();
});

it('put warn message in log file', function () {
$message = 'test warn message';

warn($message);

$today = date('Ymd');
$logFilePath = storage_path()."/logs/app-$today.log";
$logFile = file_get_contents($logFilePath);

expect(str_contains($logFile, $message))->toBeTrue();
});

it('put error message in log file', function () {
$message = 'test error message';

error($message);

$today = date('Ymd');
$logFilePath = storage_path()."/logs/app-$today.log";
$logFile = file_get_contents($logFilePath);

expect(str_contains($logFile, $message))->toBeTrue();
});
});