PHP Classes

SLogger: Log messages following the PSR-3 specification

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-10-01 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 39 All time: 10,824 This week: 78Up
Version License PHP version Categories
slogger 1.0.1MIT/X Consortium ...8.1Files and Folders, Logging, PSR, PHP 8
Description 

Author

This package can log messages using log manager classes.

It provides a base log class that can take messages to be sent to logs following the PSR-3 specification.

The log class also take a manager object to process log messages in customizable ways.

Currently, the package provides a log manager class that can send log messages to files.

It also provides a log formatter class to output the log to a specific format.

Picture of Eric AGNEL
  Performance   Level  
Name: Eric AGNEL <contact>
Classes: 2 packages by
Country: Taiwan Taiwan
Age: ???
All time rank: 436110 in Taiwan Taiwan
Week rank: 341 Up2 in Taiwan Taiwan Up

Recommendations

Create a logger to an application
I need a logger class to insert in file or database.

Documentation

SLogger

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_

| 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 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. | 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(
        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.

| 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
        )

Usage

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

  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  src  
File Role Description
Files folder imageKuran (1 directory)

  Files folder image Files  /  src  /  Kuran  
File Role Description
Files folder imageSLogger (2 files, 2 directories)

  Files folder image Files  /  src  /  Kuran  /  SLogger  
File Role Description
Files folder imageFormaters (2 files)
Files folder imageManagers (2 files)
  Plain text file ErrorLevel.php Class Error level definitions
  Plain text file Logger.php Class Class source

  Files folder image Files  /  src  /  Kuran  /  SLogger  /  Formaters  
File Role Description
  Plain text file FormaterInterface.php Class Class source
  Plain text file LineFormater.php Class Class source

  Files folder image Files  /  src  /  Kuran  /  SLogger  /  Managers  
File Role Description
  Plain text file FileManager.php Class Class source
  Plain text file ManagerInterface.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:39
This week:0
All time:10,824
This week:78Up