PHP Classes

Simple PHP Logger: Log messages to storage implemented by streams

Recommend this page to a friend!
  Info   View files Example   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-30 (6 days ago) RSS 2.0 feedNot enough user ratingsTotal: 36 This week: 36All time: 10,919 This week: 2Up
Version License PHP version Categories
simple-php-logger 1.0.0MIT/X Consortium ...5Debug, Logging, Performance and optim..., P...
Description 

Author

This package can log messages to storage implemented by streams.

It provides a class that can be taken as a parameter, a stream object that will handle the storage of the log message.

The log class uses a log result class to format the log message before sending the formatted log message to the stream object.

The package provides several stream classes that can store log messages in regular files:

1. Synchronously waiting for the messages to be stored in the files

2. Asynchronously without waiting for the messages to be stored in the files

Innovation Award
PHP Programming Innovation award nominee
March 2024
Nominee
Vote
Logging application messages is helpful to debug issues that those applications may have.

Developers may check the application logs to discover details that may be incorrect and causing issues that must be fixed as soon as possible.

This package can generate log messages in two ways. One way is synchronous. That is the slower way because the applications need to wait for the messages to be stored in files to carry on with the execution of the application code.

The other way is asynchronous. This way, applications can send messages to log files and continue with the execution of the application code without waiting for the messages to be stored in the files.

The asynchronous way of storing log messages is faster.

Manuel Lemos
Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 23 packages by
Country: Peru Peru
Innovation award
Innovation award
Nominee: 10x

Winner: 3x

Example

<?php

declare (strict_types=1);

use
SimpleLogger\Logger;
use
SimpleLogger\streams\{CollectionStream, FileStream, StdoutStream};

require
__DIR__ . '/vendor/autoload.php';

$logger = new Logger(stream: new CollectionStream([
    new
StdoutStream(),
   
FileStream::async(__DIR__ . '/log.log'),
]));

$logger->info('This is an info message', ['exception' => new Exception('This is an exception')]);
$logger->warning('This is a warning message');
$logger->debug('This is a debug message with {msg}', ['msg' => 'parameters']);


Details

Simple psr logger

Installation

composer require mateodioev/simple-logger

Usage

use SimpleLogger\Logger;
use SimpleLogger\streams\{CollectionStream, FileStream, StdoutStream};

$logger = new Logger(stream: new CollectionStream([
    new StdoutStream(),
    FileStream::async(__DIR__ . '/log.log'),
]));

$logger->debug('The debug message');

Creating a new stream

A stream is a class that implements the SimpleLogger\streams\LogStream interface. You can create your own stream by implementing the write method.

use SimpleLogger\streams\LogStream;

class MyStream implements LogStream
{
    public function write(LogResult $message): void
    {
        // Write the message
    }
}

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 2 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imagedata (3 files)
Files folder imagestreams (6 files)
  Plain text file Logger.php Class Class source

  Files folder image Files  /  src  /  data  
File Role Description
  Plain text file AsyncFileWriter.php Class Class source
  Plain text file FileWriter.php Class Class source
  Plain text file SyncFileWriter.php Class Class source

  Files folder image Files  /  src  /  streams  
File Role Description
  Plain text file CollectionStream.php Class Class source
  Plain text file FileStream.php Class Class source
  Plain text file LogResult.php Class Class source
  Plain text file LogStream.php Class Class source
  Plain text file NullStream.php Class Class source
  Plain text file StdoutStream.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:36
This week:36
All time:10,919
This week:2Up