From 8e2dab5aadaa8373336eae6e0bcce66b27729eb5 Mon Sep 17 00:00:00 2001 From: Mike van den Hoek Date: Mon, 26 Oct 2026 15:00:15 +0100 Subject: [PATCH] feat: make log directory configurable to keep log files outside the webroot --- README.md | 30 +++++++++++++++- src/PrefillGravityForms/Foundation/Plugin.php | 35 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 79628b4..c414c9f 100644 --- a/README.md +++ b/README.md @@ -113,9 +113,21 @@ Each Personal Data Row block must be configured in the block sidebar: Enable logging to monitor errors during communication with the BRP suppliers. -- Logs are written daily to `pg-log{-date}.json` in the WordPress webroot directory. +- Logs are written daily to `pg-log{-date}.json` in the parent directory of the WordPress installation (`dirname(ABSPATH)`) by default. - A rotating file handler keeps up to 7 log files by default, deleting the oldest as needed. - You can change the maximum number of log files using the filter described below. +- When the log directory does not exist or is not writable, file logging is disabled. If logging is enabled, a warning is passed to the `pg::exception/intercept` action. + +### Log directory + +When WordPress is installed in a subdirectory (e.g. a Composer/Bedrock based setup), `dirname(ABSPATH)` resolves to the public webroot, which would make the log files publicly accessible. Make sure the log files are stored outside of the webroot by defining the log directory in your `wp-config.php` (or `config/application.php` in Bedrock): + +```php +// Prefill Gravity Forms – Log directory (must exist and be writable, outside of the webroot) +define('PREFILL_GRAVITY_FORMS_LOG_DIR', '/path/outside/webroot/logs'); +``` + +The directory can also be changed using the filter described below. ## Hooks @@ -127,6 +139,22 @@ Use the following filter to alter the rotating file handler's max files setting: apply_filters('pg::logger/rotating_filer_handler_max_files', PG_LOGGER_DEFAULT_MAX_FILES) ``` +### Change the log directory + +Use the following filter to alter the directory the log files are written to. The filter receives the value of the `PREFILL_GRAVITY_FORMS_LOG_DIR` constant when defined, otherwise `dirname(ABSPATH)`: + +```php +apply_filters('pg::logger/log_directory', $directory) +``` + +Example: + +```php +add_filter('pg::logger/log_directory', function (string $directory): string { + return dirname(ABSPATH, 2) . '/logs'; +}); +``` + ### Intercept exceptions for custom handling You can intercept exceptions caught by the plugin for additional processing or custom logging using this filter: diff --git a/src/PrefillGravityForms/Foundation/Plugin.php b/src/PrefillGravityForms/Foundation/Plugin.php index b1ae1f1..992835d 100644 --- a/src/PrefillGravityForms/Foundation/Plugin.php +++ b/src/PrefillGravityForms/Foundation/Plugin.php @@ -7,6 +7,7 @@ use function DI\create; use Exception; use function OWC\PrefillGravityForms\Foundation\Helpers\resolve; +use OWC\PrefillGravityForms\GravityForms\GravityFormsSettings; /** * BasePlugin which sets all the serviceproviders. @@ -73,10 +74,18 @@ protected function buildContainer(): void 'config' => create(Config::class)->constructor($this->rootPath . '/config'), 'logger' => function () { $logger = new \Monolog\Logger('pg_log'); + $directory = $this->getLogDirectory(); + + if (null === $directory) { + $logger->pushHandler(new \Monolog\Handler\NullHandler()); + + return $logger; + } + $maxFiles = apply_filters('pg::logger/rotating_filer_handler_max_files', PG_LOGGER_DEFAULT_MAX_FILES); $handler = (new \Monolog\Handler\RotatingFileHandler( - filename: sprintf('%s/pg-log.json', dirname(ABSPATH)), + filename: sprintf('%s/pg-log.json', $directory), maxFiles: is_int($maxFiles) && 0 < $maxFiles ? $maxFiles : PG_LOGGER_DEFAULT_MAX_FILES, level: \Monolog\Level::Debug ))->setFormatter(new \Monolog\Formatter\JsonFormatter()); @@ -183,4 +192,28 @@ public function resourcePath(string $file, string $directory = ''): string return $this->rootPath . "/build/{$directory}{$file}"; } + + /** + * The default dirname(ABSPATH) resolves to the public webroot when WordPress + * is installed in a subdirectory (e.g. Bedrock), so allow overriding it. + * Returns null when the directory is invalid, in which case file logging is skipped. + * + * @since NEXT + */ + private function getLogDirectory(): ?string + { + $directory = defined('PREFILL_GRAVITY_FORMS_LOG_DIR') ? PREFILL_GRAVITY_FORMS_LOG_DIR : dirname(ABSPATH); + $directory = apply_filters('pg::logger/log_directory', $directory); + + if (! is_string($directory) || '' === $directory || ! is_dir($directory) || ! is_writable($directory)) { + if (GravityFormsSettings::make()->loggingEnabled()) { + /** This action is documented in src/PrefillGravityForms/Traits/Logger.php */ + do_action('pg::exception/intercept', new Exception(sprintf('Log directory "%s" does not exist or is not writable, file logging is disabled.', is_string($directory) ? $directory : gettype($directory)), 300), 'warning'); + } + + return null; + } + + return untrailingslashit($directory); + } }