| Recommend this page to a friend! |
| Info | Documentation | Reputation | Support forum | Blog | Links |
| Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
| 2025-08-31 (4 days ago) | Not enough user ratings | Total: 44 | All time: 10,834 This week: 55 | |||||
| Version | License | PHP version | Categories | |||
| slogger 1.0.3 | MIT/X Consortium ... | 8.1 | Files and Folders, Logging, PSR, PHP 8 |
| Description | Author | ||||||||
This package can log messages using log manager classes. |
|
Create a logger to an application
I need a logger class to insert in file or database.
A Simple Logger class for PhP.
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 but do not implement directly the logger interface to make full use of enums as the error levels.
The minimum setup needed to use the Logger class.
<?php
require_once("vendor/Autoloader.php");
use Kuran\SLOGGER\{Logger, ErrorLevel, Managers\FileManager};
/* Instantiate the class.
* Then create a new Manager. The FileManager include a default LineFormatter,
* which writes to 'app.log' by default.
*/
$log = new Logger( array( new FileManager() ) );
/* Simple log.
* Needs a message body, and an array for context.
*/
$log->alert(
message: "Test message for the file {:file}",
context: array(
":file" => __FILE__,
":extras" => array(
["name" => "Admin", "username" => "admin"],
["name" => "Root", "username" => "root"]
)
)
);
Constructor_| Argument | Description | | -------- | ------------------------------------------ | | managers | set an array of managers at instanciation. |
__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 completly replace the Logger's managers list.
setManagers(managers: = array(
new FileManager(filePath: 'debug.log', level: ErrorLevel::Debug, formater: null),
new FileManager(filePath: 'errors.log', level: ErrorLevel::Error, formater: null)
)
);
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);
Constructor_all arguments are optional.
| Argument | Description |
|----------|------------------------------------------------------------------------------------------|
| filePath | If no filepath is defined, it will log message to _app.log_ by default |
| level | Default level is _ErrorLevel::ERROR_ |
| formater | If no Formater is defined, it will default to a _LineFormater_ with default options. |
__construct(
filePath: = 'app.log',
level: = ErrorLevel::ERROR,
formater: = null)
setFormater_used to replace the Formater already in place.
setFormater(FormaterInterface $formater)
$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->setManagers(array($errorManager, $debugManager));
// add $manager to the list of managers
$log->addManager($manager);
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
}
ErrorLevel entry can be returned by name or number using the enum methods fromValue(int) and fromName(string).
ErrorLevel::fromValue(700); // ErrorLevel::ALERT
ErrorLevel::fromName('Notice'); // ErrorLevel::NOTICE
Both will throw an exeption if the value or name are invalid.
Constructor_All arguments are optional.
| Argument | Description | | ---------- | ---------------------------------------------------------------------------------- | | format | If no format is defined, it will default to Date \[ Level \] > Message Context | | timeFormat | If no date format is defined, it will default to Y-m-d H:i:s |
__construct(
string $format = null,
string $timeFormat = null
)
$formater = new LineFormater(
format: "{:date} {:errorLevel} {:message} {:context}",
timeFormat: "m-d-Y H:i"
);
For every entry used in the format using {:parameter}, a replacement will be made with entries from the context array.
| File | Role | Description | ||
|---|---|---|---|---|
| Data | Auxiliary data | |||
| Data | Auxiliary data | |||
| Lic. | License text | |||
| Doc. | Read me | |||
| / | src | / | Kuran | / | SLogger |
| File | Role | Description | ||
|---|---|---|---|---|
| |
Class | Error level definitions | ||
| |
Class | Class source | ||
| |
Class | Class source | ||
| / | src | / | Kuran | / | SLogger | / | Formaters |
| File | Role | Description |
|---|---|---|
| |
Class | Class source |
| |
Class | Class source |
| / | src | / | Kuran | / | SLogger | / | Managers |
| File | Role | Description |
|---|---|---|
| |
Class | Class source |
| |
Class | Class source |
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% |
|
|
| Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.