Skip to content

Commit

Permalink
refactor: data type has been modified for the logger helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleon4 committed Apr 29, 2024
1 parent 25355b3 commit 180d35f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 8 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use Lion\Route\Route;
use Dotenv\Dotenv;
use Lion\Bundle\Enums\LogTypeEnum;
use Lion\Bundle\Helpers\Commands\Schedule\TaskQueue;
use Lion\Bundle\Helpers\ExceptionCore;
use Lion\Bundle\Helpers\Http\Routes;
use Lion\Database\Driver;
Expand Down Expand Up @@ -113,11 +115,16 @@
Route::addMiddleware(Routes::getMiddleware());
// -----------------------------------------------------------------------------
Route::get('/', function () {
// TaskQueue::push('send:email:verify', json([
// 'email' => 'sleon@dev.com',
// 'template' => '<h1>Tasks Test</h1>'
// ]));

return info('[index]');
});

Route::get('logger', function () {
logger('test-logger', 'info', ['user' => 'Sleon'], true);
logger('test-logger', LogTypeEnum::INFO, ['user' => 'Sleon'], true);

return success();
});
Expand Down
23 changes: 19 additions & 4 deletions src/LionBundle/Helpers/Bundle/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,34 @@ function vd(mixed $response): void
*
* @return void
*/
function logger(string $message, LogTypeEnum $logType = LogTypeEnum::INFO, array $data = [], bool $index = true): void
{
function logger(
string $message,
LogTypeEnum $logType = LogTypeEnum::INFO,
array $data = [],
bool $index = true
): void {
$logTypeValue = $logType->value;

$path = storage_path('logs/monolog/', $index);

$fileName = "{$path}lion-" . Carbon::now()->format('Y-m-d') . '.log';

(new Store())->folder($path);

$logger = new Logger('log');

$logger->pushHandler(new StreamHandler($fileName, Level::Info));

if (!isset($_SERVER['REQUEST_URI'])) {
$logger->$logType->value(json_encode($message), $data);
$logger->$logTypeValue(json_encode($message), $data);
} else {
$logger->$logType->value(json_encode(['uri' => $_SERVER['REQUEST_URI'], 'data' => json_decode($message)]), $data);
$logger->$logTypeValue(
json_encode([
'uri' => $_SERVER['REQUEST_URI'],
'data' => json_decode($message)
]),
$data
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Helpers/Bundle/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testLogger(): void

$fileName = "{$path}lion-" . Carbon::now()->format('Y-m-d') . '.log';

logger(self::LOGGER_CONTENT, LogTypeEnum::INFO->value, ['user' => 'Sleon'], false);
logger(self::LOGGER_CONTENT, LogTypeEnum::INFO, ['user' => 'Sleon'], false);

$this->assertFileExists($fileName);
}
Expand Down

0 comments on commit 180d35f

Please sign in to comment.