Skip to content

Commit

Permalink
feat(SFT-1714): Added Integrations Log to settings menu so users can …
Browse files Browse the repository at this point in the history
…view and clear log from CP (#1715)

* feat(SFT-1714): display integrations logs separately in settings
  • Loading branch information
gustavs-gutmanis authored Jan 16, 2025
1 parent 6fb6be8 commit f892b19
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class IntegrationLoggerProvider
{
public const LOG_FILE = 'freeform-integrations.log';

private int $level;

public function __construct(
Expand All @@ -39,7 +41,7 @@ public function getLogger(IntegrationInterface|Type $integration): LoggerInterfa
->logger
->getLogger(
$logCategory,
'freeform-integrations.log',
self::LOG_FILE,
$this->level
)
;
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/Bundles/Routing/routes/cp/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'freeform/settings/error-log' => 'freeform/logs/error',
'freeform/settings/integrations-log' => 'freeform/logs/integrations',
'freeform/logs/clear' => 'freeform/logs/clear',
];
8 changes: 4 additions & 4 deletions packages/plugin/src/Library/Logging/FreeformLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class FreeformLogger
];

private static array $levelColorMap = [
'DEBUG' => '#CCCCCC',
'INFO' => '#6c757d',
'DEBUG' => '#6c757d',
'INFO' => '#0284c7',
'NOTICE' => '#28a745',
'WARNING' => '#ffc107',
'ERROR' => '#dc3545',
Expand All @@ -97,9 +97,9 @@ public static function getInstance(string $category, ?string $fileName = null, ?
return self::$loggers[$category];
}

public static function getLogfilePath(?string $fileName = 'freeform.log'): string
public static function getLogfilePath(?string $fileName): string
{
return \Craft::$app->path->getLogPath().'/'.$fileName;
return \Craft::$app->path->getLogPath().'/'.($fileName ?? 'freeform.log');
}

public static function getColor(string $level): string
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/Resources/css/cp/logs/logs.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/plugin/src/Services/LoggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function getLogger(string $category, ?string $fileName = null, ?int $leve
return FreeformLogger::getInstance($category, $fileName, $level);
}

public function getLogReader(): LineLogReader
public function getLogReader(?string $fileName = null): LineLogReader
{
return new LineLogReader(FreeformLogger::getLogfilePath());
return new LineLogReader(FreeformLogger::getLogfilePath($fileName));
}

public function registerJsTranslations(View $view): void
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin/src/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Solspace\Freeform\Services;

use Solspace\Freeform\Bundles\Integrations\Providers\FormIntegrationsProvider;
use Solspace\Freeform\Bundles\Integrations\Providers\IntegrationLoggerProvider;
use Solspace\Freeform\Events\Freeform\RegisterSettingsNavigationEvent;
use Solspace\Freeform\Form\Form;
use Solspace\Freeform\Freeform;
Expand Down Expand Up @@ -235,6 +236,7 @@ public function getSettingsModel(): Settings
public function getSettingsNavigation(): array
{
$errorCount = Freeform::getInstance()->logger->getLogReader()->count();
$integrationsCount = Freeform::getInstance()->logger->getLogReader(IntegrationLoggerProvider::LOG_FILE)->count();

$nav = [
'general' => ['title' => Freeform::t('General Settings')],
Expand All @@ -258,8 +260,10 @@ public function getSettingsNavigation(): array
'integrations/other' => ['title' => Freeform::t('Other')],
'hdalerts' => ['heading' => Freeform::t('Reliability')],
'notices-and-alerts' => ['title' => Freeform::t('Notices & Alerts')],
'error-log' => ['title' => Freeform::t('Error Log <span class="badge">{count}</span>', ['count' => $errorCount])],
'diagnostics' => ['title' => Freeform::t('Diagnostics')],
'hdlogs' => ['heading' => Freeform::t('Logs')],
'error-log' => ['title' => Freeform::t('Errors <span class="badge">{count}</span>', ['count' => $errorCount])],
'integrations-log' => ['title' => Freeform::t('Integrations <span class="badge">{count}</span>', ['count' => $integrationsCount])],
];

if (!$this->isAllowAdminEdit()) {
Expand Down
30 changes: 17 additions & 13 deletions packages/plugin/src/controllers/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Solspace\Freeform\controllers;

use Solspace\Freeform\Bundles\Integrations\Providers\IntegrationLoggerProvider;
use Solspace\Freeform\Freeform;
use Solspace\Freeform\Library\Helpers\PermissionHelper;
use Solspace\Freeform\Resources\Bundles\LogBundle;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\Response;

class LogsController extends BaseController
Expand All @@ -27,10 +24,6 @@ public function actionIndex(): Response
);
}

/**
* @throws Exception
* @throws InvalidConfigException
*/
public function actionError(): Response
{
$logReader = $this->getLoggerService()->getLogReader();
Expand All @@ -47,11 +40,22 @@ public function actionError(): Response
);
}

/**
* @throws BadRequestHttpException
* @throws ForbiddenHttpException
* @throws Exception
*/
public function actionIntegrations(): Response
{
$logReader = $this->getLoggerService()->getLogReader(IntegrationLoggerProvider::LOG_FILE);

$this->getLoggerService()->registerJsTranslations($this->view);

\Craft::$app->view->registerAssetBundle(LogBundle::class);

return $this->renderTemplate(
'freeform/logs/error',
[
'logReader' => $logReader,
]
);
}

public function actionClear(): Response
{
$this->requirePostRequest();
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/templates/logs/error.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
{% set crumbs = [
{ label: craft.freeform.name, url: url("freeform") },
{ label: "Settings"|t("freeform"), url: url("freeform/settings") },
{ label: "Reliability"|t("freeform"), url: url("freeform/settings" ~ "/" ~ segment3) },
{ label: "Logs"|t("freeform"), url: url("freeform/settings" ~ "/" ~ segment3) },
] %}

{% set title = "Error Log"|t('freeform') %}
{% set title = segment3|title|replace({'-': " "})|t('freeform') %}

{% block actionButton %}

Expand Down
4 changes: 2 additions & 2 deletions packages/styles/src/cp/logs/logs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
font-weight: bold;

&.log-level-debug {
background-color: #CCCCCC;
background-color: #6c757d;
}
&.log-level-info {
background-color: #6c757d;
background-color: #0284c7;
}
&.log-level-notice {
background-color: #28a745;
Expand Down

0 comments on commit f892b19

Please sign in to comment.