PHP code example of kuran / slogger

1. Go to this page and download the library: Download kuran/slogger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kuran / slogger example snippets



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"]
            )
    )
);

    __construct(array $managers = array())

    setManagers(array $managers);

    addManager(ManagerInterface $manager);

__construct(
        string $filePath = 'app.log',
        ErrorLevel $level = ErrorLevel::ERROR,
        FormaterInterface $formater = null)

    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->setManager(array($errorManager, $debugManager));

    // add $manager to the list of managers
    $log->addManager($manager);

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
}

    __construct(
        string $format = null,
        string $timeFormat = null
        )

$formater = new LineFormater(
    format: "{:date} {:errorLevel} {:message} {:context}",
    timeFormat: "m-d-Y H:i"
);