Skip to content
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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
35 changes: 34 additions & 1 deletion src/PrefillGravityForms/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}