maplephp / log
PHP PSR-3 Logger library, standardized approach to logging messages.
v1.0.1
2023-12-11 20:45 UTC
Requires
- php: >=8.0
- maplephp/http: ^1.0
- maplephp/query: ^1.0
README
PHP PSR-3 Logger library – your reliable companion for efficient logging in PHP applications. This library adheres to the PSR-3 standard, providing a seamless and standardized approach to logging messages across different components of your application.
Log-levels
- emergency: System is unusable
- alert: Action must be taken immediately
- critical: Critical conditions
- error: Runtime errors that do not require immediate action but usually logged and monitored.
- warning: Exceptional occurrences that are not errors.
- notice: Normal but significant events.
- info: Interesting events (User logs in, SQL logs.)
- debug: Detailed debug information.
- log: Logs with an arbitrary level.
Stream/file handler
Add namespaces
use MaplePHP\Log\Logger; use MaplePHP\Log\Handlers\StreamHandler;
Create simple stream logger
$log = new Logger(new StreamHandler("/path/to/logger.log")); $log->warning("The user {firstname} has been added.", ["firstname" => "John", "lastname" => "Doe"]);
Rotatable log files
Create simple stream rotatables loggers. Will create a new log file if size is more than MAX_SIZE (5000 KB) and remove log files if total file count is more than MAX_COUNT 10.
$log = new Logger(new StreamHandler("/path/to/logger.log", StreamHandler::MAX_SIZE, StreamHandler::MAX_COUNT)); $log->warning("The user {firstname} has been added.", ["firstname" => "John", "lastname" => "Doe"]);
Database handler
Add namespaces
use MaplePHP\Log\Logger; use MaplePHP\Log\Handlers\DBHandler;
1. Connect to the database.
2. Create database table
Execute bellow once to create the database table. Then remove the script again!
$dbHandler = new DBHandler(); $error = $dbHandler->create(); if (count($error) > 0) { echo "<pre>"; print_r($error); echo "</pre>"; } else { echo "<p>The Logger database table has been installed!</p>"; }
3. Write to database log
$log = new Logger(new DBHandler()); $log->warning("The user {firstname} has been added.", ["user_id" => 4, "firstname" => "Daniel"]);
PHP error log handler (error_log())
You can (not required) specify a log file location in ErrorLogHandler. If argument is empty, then server default location.
Add namespaces
use MaplePHP\Log\Logger; use MaplePHP\Log\Handlers\ErrorLogHandler;
$log = new Logger(new ErrorLogHandler("/path/to/logger.log")); $log->warning("The user {firstname} has been added.", ["firstname" => "John", "lastname" => "Doe", "data" => ["city" => "Stockholm", "coor" => "122,1212"]]);