Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SFT-1714]: Add "Integrations Log" to settings menu so users can view and clear log from CP #1715

Merged
merged 3 commits into from
Jan 16, 2025
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
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