Did you find something wrong?
Be sure to let us know about it with an issue.
Aplus Framework Log Library.
The installation of this library can be done with Composer:
composer require aplus/log
The Log Library allows you to save logs in several ways.
In the constructor of the Logger class it is possible to set the destination, the level and configurations through the config parameter.
Let's see an example saving logs with CRITICAL level (5) or higher in the app.log file:
use Framework\Log\Loggers\FileLogger; use Framework\Log\LogLevel; $destination = __DIR__ . '/app.log'; $level = LogLevel::CRITICAL; $logger = new FileLogger($destination, $level); $logger->logDebug('Debug message'); // bool $logger->logCritical('Critical message'); // bool $logger->logAlert('Alert message'); // bool
Note that logs with a level lower than CRITICAL (5) are not saved in the destination.
Logs can be defined at eight different levels:
Level 0: Detailed debug information.
Level 1: Interesting events.
Example: User logs in, SQL logs.
Level 2: Normal but significant events.
Level 3: Exceptional occurrences that are not errors.
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Level 4: Runtime errors that do not require immediate action but should typically be logged and monitored.
Level 5: Critical conditions.
Example: Application component unavailable, unexpected exception.
Level 6: Action must be taken immediately.
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Level 7: System is unusable.
The Log Library has five different loggers:
The message is sent by email to the address in the destination parameter.
use Framework\Log\Loggers\EmailLogger; $destination = '[email protected]'; $logger = new EmailLogger($destination);
In the third parameter of the constructor, config, you can set custom headers for the message:
$logger = new EmailLogger($destination, config: [ 'headers' => [ 'From' => '[email protected]', 'Subject' => 'System Log', ], ]);
The message is appended to the file destination.
use Framework\Log\Loggers\FileLogger; $destination = __DIR__ . '/app.log'; $logger = new FileLogger($destination);
Multi File Logger
The message is appended to a file with the date in the filename with a directory as destination.
use Framework\Log\Loggers\MultiFileLogger; $destination = __DIR__ . '/logs'; $logger = new MultiFileLogger($destination);
The filename has the following format:
The message is sent directly to the SAPI logging handler.
use Framework\Log\Loggers\SAPILogger; $logger = new SAPILogger();
The message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file.
use Framework\Log\Loggers\SysLogger; $logger = new SysLogger();
Aplus Log Library is an easy-to-use tool for, beginners and experienced, PHP developers.
It is perfect for saving logs with different destinations.
The more you use it, the more you will learn.
Did you find something wrong?