A simple package to write entries to a log file.
composer require jeffreyvanrossum/wp-log
You can setup your log with:
$log = new \Jeffreyvr\WPLog\Log('Your log');
You can define a custom file path, if you don't, the default is the wp-content/uploads/
folder with the file name being a sanitized version of your log name.
$log->setFilePath(wp_upload_dir()['basedir'] . '/logs/your-log-filename.log');
Writing to your log can be done like so:
$log->write('Your log message');
$log->write(['foo' => 'bar']);
You may clear your log with:
$log->clear();
The interface
method will render an interface, which you can use to display the log somewhere in the admin area.
If you want to display the log page in the admin menu, you can call:
$log->interface()->inAdminMenu(slug: 'optional-slug', parent: 'tools.php');
To set a custom capability use:
$log->interface->setCapability('manage_options');
Or if you want to add it as a plugin link instead:
$log->interface()->asPluginLink(basename: plugin_basename(__FILE__), slug: 'optional-slug');
You can instead also call $log->interface()->render()
to render it somewhere you want.
To prevent your log from becoming very large, the default limit is set to 1000 items. You can overwrite this:
$log->setClearLimit(100);
// If you don't want to limit your log, you can pass 0.
$log->setClearLimit(0);
MIT. Please see the License File for more information.