Implement PSR3 and PHP Monolog (#19)
* feat: install monolog composer lib * feat: implement psr3 and monolog * feat: display logs in frontend * fix: tests * build: update cicd to run tests for PR * build: add phpcs to cicd * refactor: fix phpcs problems
This commit is contained in:
@@ -2,14 +2,15 @@
|
||||
|
||||
use Bastion\ApplicationFactory;
|
||||
use Cart\User;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use Openguru\OpenCartFramework\Application;
|
||||
use Openguru\OpenCartFramework\Http\Response as HttpResponse;
|
||||
use Openguru\OpenCartFramework\ImageTool\ImageTool;
|
||||
use Openguru\OpenCartFramework\ImageTool\ImageToolInterface;
|
||||
use Openguru\OpenCartFramework\Logger\LoggerInterface;
|
||||
use Openguru\OpenCartFramework\Logger\OpenCartLogAdapter;
|
||||
use Openguru\OpenCartFramework\OpenCart\Decorators\OcRegistryDecorator;
|
||||
use Openguru\OpenCartFramework\Support\Arr;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
$sysLibPath = rtrim(DIR_SYSTEM, '/') . '/library/oc_telegram_shop';
|
||||
$basePath = rtrim(DIR_APPLICATION, '/') . '/..';
|
||||
@@ -124,17 +125,18 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$logger = $this->createLogger();
|
||||
|
||||
try {
|
||||
$this
|
||||
->createApplication()
|
||||
->createApplication($logger)
|
||||
->bootAndHandleRequest();
|
||||
} catch (Exception $e) {
|
||||
$logger = new OpenCartLogAdapter($this->log, 'TeleCart');
|
||||
$logger->logException($e);
|
||||
} catch (Throwable $e) {
|
||||
$logger->error($e->getMessage(), ['exception' => $e]);
|
||||
http_response_code(HttpResponse::HTTP_INTERNAL_SERVER_ERROR);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'error' => 'Server Error.',
|
||||
'error' => getenv('APP_DEBUG') ? $e->getMessage() : 'Server Error.',
|
||||
], JSON_THROW_ON_ERROR);
|
||||
}
|
||||
}
|
||||
@@ -237,7 +239,7 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
private function createApplication(): Application
|
||||
private function createApplication(LoggerInterface $logger): Application
|
||||
{
|
||||
$json = $this->model_setting_setting->getSetting('module_telecart');
|
||||
if (! isset($json['module_telecart_settings'])) {
|
||||
@@ -250,7 +252,7 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
'language_id' => (int) $this->config->get('config_language_id'),
|
||||
],
|
||||
'logs' => [
|
||||
'path' => DIR_LOGS,
|
||||
'path' => DIR_LOGS . '/telecart.log',
|
||||
],
|
||||
'database' => [
|
||||
'host' => DB_HOSTNAME,
|
||||
@@ -276,21 +278,32 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
$app = ApplicationFactory::create($items);
|
||||
$app->bind(OcRegistryDecorator::class, fn() => new OcRegistryDecorator($this->registry));
|
||||
$app->bind(ImageToolInterface::class, fn() => new ImageTool(DIR_IMAGE, HTTPS_SERVER));
|
||||
|
||||
$app
|
||||
->withLogger(fn() => new OpenCartLogAdapter(
|
||||
$this->log,
|
||||
'TeleCartAdmin',
|
||||
$app->getConfigValue('app.app_debug')
|
||||
? LoggerInterface::LEVEL_DEBUG
|
||||
: LoggerInterface::LEVEL_INFO,
|
||||
));
|
||||
$app->setLogger($logger);
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
private function runMaintenanceTasks(): void
|
||||
{
|
||||
$this->createApplication()->runMaintenanceTasks();
|
||||
$logger = $this->createLogger();
|
||||
|
||||
try {
|
||||
$this->createApplication($logger)->runMaintenanceTasks();
|
||||
} catch (Throwable $exception) {
|
||||
$logger->error($exception->getMessage(), ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
private function createLogger(bool $debug = false): Logger
|
||||
{
|
||||
$log = new Logger('TeleCart_Admin');
|
||||
$log->pushHandler(
|
||||
new StreamHandler(
|
||||
DIR_LOGS . '/telecart.log',
|
||||
$debug ? Logger::DEBUG : Logger::INFO,
|
||||
)
|
||||
);
|
||||
|
||||
return $log;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
{{ header }}{{ column_left }}
|
||||
<div id="content">
|
||||
<div class="page-header">
|
||||
<div class="container-fluid">
|
||||
<h1>{{ heading_title }}</h1>
|
||||
<ul class="breadcrumb">
|
||||
{% for breadcrumb in breadcrumbs %}
|
||||
<li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="container-fluid">
|
||||
<div class="container-fluid" style="margin-top: 10px;">
|
||||
{% if telecart_error_warning %}
|
||||
<div class="alert alert-danger alert-dismissible"><i
|
||||
class="fa fa-exclamation-circle"></i> {{ telecart_error_warning }}
|
||||
|
||||
Reference in New Issue
Block a user