PHP code example of gd-75 / simple-logger

1. Go to this page and download the library: Download gd-75/simple-logger 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/ */

    

gd-75 / simple-logger example snippets



use GD75\SimpleLogger\SimpleLogger;

LEVEL_INFO);

// Minimal log level at logger-level
$logger->debug("I should not be here");
$logger->info("I should be here");

// Non-valid log level (it will always be logged, no matter the minimal log level)
$logger->log("test", "My level was originally a string saying 'test', which is not a known log level");

// Context
$logger->info("I have a context, check it out", ["message" => "Thanks for checking me out!"]);


use GD75\SimpleLogger\SimpleLogger;
use GD75\SimpleLogger\FileWriter;

 since the minimal level of the logger is INFO, using a lower level here will have no influence
$logger->addWriter(SimpleLogger::LEVEL_DEBUG, new FileWriter("out", "out/contexts"));

$logger->info("It works!");


use GD75\SimpleLogger\SimpleLogger;

LEVEL_INFO);

// Using proxy and proxy of a proxy
$loggerProxy = $logger->getProxy("[MyContext]");
$loggerProxy2 = $loggerProxy->getProxy("[MySubContext]");
$loggerProxy->info("I have gone through a proxy!");
$loggerProxy2->info("This is starting to get out of hands");


use GD75\SimpleLogger\SimpleLogger;
use GD75\SimpleLogger\SimpleLoggerWriterInterface;

ring $levelName, string $message, array $context): void
    {
        $contextElementCount = count($context);
        echo "[{$levelName}] {$message}. Context has {$contextElementCount} element(s) in it.\n";
    }
}

$logger = new SimpleLogger(SimpleLogger::LEVEL_INFO, false);

// Using a writer with a higher minimal level
$logger->addWriter(SimpleLogger::LEVEL_CRITICAL, new CLIWriterSimple());
$logger->critical("This will be seen in the command line");

// Proxies will of course also work with custom writers
$loggerProxy = $logger->getProxy("[MyContext]");
$loggerProxy->critical("I have gone through a proxy!");