dadapas/log

Common libraries implementating psr-3 logging interface

2.0 2022-06-11 16:13 UTC

This package is auto-updated.

Last update: 2024-10-15 20:14:20 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

This repository implements interfaces related to PSR-3.

Installation

composer require dadapas/log

Usage

If you need a logger, you can use the interface like this:

<?php

use Dadapas\Log\{FileSystemAdapter, Log as Logger};

$localAdapter = new \League\Flysystem\Local\LocalFilesystemAdapter(
    // Determine log directory
    __DIR__.'/path/to/logs'
);

// The FilesystemOperator
$filesystem = new \League\Flysystem\Filesystem($localAdapter);
$filesysAdapter = new FileSystemAdapter($filesystem);

$logger = new Logger();
$logger->setAdapter($filesysAdapter);

To send a logger to an email will be like:

// ...
use Dadapas\Log\PHPMailerAdapter;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

$mail = new PHPMailer(true);

// $mail->SMTPDebug  = SMTP::DEBUG_SERVER;
// $mail->isSMTP();
// $mail->Host       = 'smtp.example.com';
// $mail->SMTPAuth   = true;

// ...
$adapter = new PHPMailerAdapter($mail);

// ...
$logger->setAdapter($adapter);

Make a log to the file

// ...
try {
    throw new Exception("An exception has been thrown.");
} catch (Exception $e) {

    // Log to the the error message to file
    $logger->error($e->getMessage(), $e->getTrace());

} catch (\PHPMailer\PHPMailer\Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

You can then pick one of the implementations of the interface to get a logger.

If you want to implement the interface, you can require this package and implement Psr\Log\LoggerInterface in your code. Please read the specification text for details.

Test

For making a test just

composer test

License

The licence is MIT for more details click here