kuran / slogger
A simple logger file.
Requires
- php: >=8.1
- psr/log: ^3.0
README
A Simple Logger class for PhP.
About
This is a simple Logger class meant to be easy to implement so that you don't have to worry about to much configuration.
This class follow a PSR-3 standard.
Basic Usage
The minimum setup needed to use the Logger class.
<?php require_once("vendor/Autoloader.php"); use Kuran\SLOGGER\{Logger, ErrorLevel, Managers\FileManager}; /* Instanciate the Logger. * Add a Manager to the logger. The manager has a default Formater. */ $log = new Logger( array( new FileManager() ) ); /* Simple log. * Needs a message body, and an array for context. */ $log->alert( "Test message for the file {:file}", array( ":file" => __FILE__, ":extras" => array( ["name" => "Admin", "username" => "admin"], ["name" => "Root", "username" => "root"] ) ) );
Logger class options
- Constructor
__construct(array $managers = array())
Same as creating a logger without argument then using the setManagers()
function
- setManagers method
This method is used to set an array of managers to the logger.
This will replace the Logger's managers list.
setManagers(array $managers);
Many managers can be used to log messages to different files, or using different methods (Database...). Or to log messages with different Error Levels.
- addManager method
This method is used to add a simgle manager to the managers stack.
If you want to add another Manager
later in your code.
addManager(ManagerInterface $manager);
FileManager options
- Constructor
all arguments are optional.
__construct( string $filePath = 'app.log', ErrorLevel $level = ErrorLevel::ERROR, FormaterInterface $formater = null)
- setFormater
used to replace the Formater already in place.
setFormater(FormaterInterface $formater)
Usage
$manager = new FileManager( filePath: "path/to/logfile.log", level: ErrorLevel::INFO, // Set the minimum Error level for this manager. formater: new LineFormater() ) // Setting multiple managers with different Error Levels $log = new Logger(); $errorManager = new FileManager( filePath: "error.log", level: ErrorLevel::ERROR ); $debugManager = new FileManager( filePath: "debug.log", level: ErrorLevel::DEBUG ); //set the Manager list to $errorManager and $debugManager $log->setManager(array($errorManager, $debugManager)); // add $manager to the list of managers $log->addManager($manager);
Error Levels
The Error levels are defined as follow in the ErrorLevel
enum
enum ErrorLevel: int { case EMERGENCY = 800; //ErrorLevel::EMERGENCY case ALERT = 700; //ErrorLevel::ALERT case CRITICAL = 600; //ErrorLevel::CRITIAL case ERROR = 500; //ErrorLevel::ERROR case WARNING = 400; //ErrorLevel::WARNING case NOTICE = 300; //ErrorLevel::NOTICE case INFO = 200; //ErrorLevel::INFO case DEBUG = 100; //ErrorLevel::DEBUG }
LineFormater options
- Constructor
All arguments are optional.
__construct( string $format = null, string $timeFormat = null )
Usage
$formater = new LineFormater( format: "{:date} {:errorLevel} {:message} {:context}", timeFormat: "m-d-Y H:i" );