Simple and lightweight PHP Logger for actions and activity.
MIT License
AcLog is a simple, zero-dependency PHP package to log activity to files.
AcLog can be used to easily log actions such as cronjobs, emails, user activity, details about errors, etc.
🧡 Sponsored by Datsi.app - Your personal database.
Add this to your existing project:
composer require xy2z/aclog
use xy2z\AcLog\AcLog;
$aclog = new AcLog(__DIR__ . '/logs');
$aclog->log($var); // can be any type: object, array, string, int, etc.
$aclog->log($var, $foo, $bar, $etc); // as many arguments you want.
$aclog = new AcLog(
log_dir: __DIR__ . '/logs',
log_date_format: false,
include_trace: false,
output_method: AcLog::VAR_DUMP,
line_breaks_between_header: 4,
// etc.
);
For more options see the constructor method of the AcLog.php file.
It is also possible to use it as a static class, if you prefer.
use xy2z\AcLog\AcLogStatic;
AcLogStatic::setup(__DIR__ . '/logs');
AcLogStatic::log($var);
If you want to set options in the static class, you need to set them as an array.
AcLogStatic::setup([
'log_dir' => __DIR__ . '/logs',
'log_date_format' => false,
'include_trace' => false,
'output_method' => AcLog::VAR_DUMP,
'line_breaks_between_header' => 4,
// etc.
]);
Other than that, it should behave exactly the same as the AcLog class, and all public methods and properties are also available.
If you want a shorter name for the static class, you can alias it.
use xy2z\AcLog\AcLogStatic as acl;
acl::setup(__DIR__ . '/logs');
acl::log($var);
You can add callbacks that will be appended to each log()
call, examples for this can be user information, request headers, etc. You can add as many callbacks you want.
$aclog = new AcLog($this->logdir);
$aclog->add_log_append_callback(function () {
return 'callback-1.';
});
$aclog->add_log_append_callback(array('MyClass', 'myCallbackMethod'));
Pull Requests are welcome, just make sure your code is tested, analysed and fixed - see below.
Remember to make tests for both classes: AcLog
and AcLogStatic
.
# Fix Coding Standards (php-cs-fixer)
composer fix
# Analyse code (phpstan)
composer analyse
# Test code (phpunit)
composer test