PHP code example of webarchitect609 / log-tools

1. Go to this page and download the library: Download webarchitect609/log-tools 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/ */

    

webarchitect609 / log-tools example snippets


use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LogLevel;
use WebArch\LogTools\Traits\LogExceptionTrait;

class FooService implements LoggerAwareInterface
{
    use LogExceptionTrait;

    public function __construct()
    {
        $this->setLogger(
            new Logger(
                'FooLogger',
                [new StreamHandler(sys_get_temp_dir() . '/foo-service.log')]
            )        
        );
    }

    public function bar()
    {
        try {

            throw new LogicException('Exception occurs in ' . __METHOD__);

        } catch (Throwable $exception) {

            /**
             * Exception will be logged with it's type, message, code, file, line and stack trace.
             */
            $this->logException($exception, LogLevel::CRITICAL, ['var1' => 'ABC']);
        }
    }

}

use WebArch\LogTools\Enum\SystemStream;
use WebArch\LogTools\Factory\MonologLoggerFactory;

$debug = false;
$loggerFactory = new MonologLoggerFactory('/tmp/log/www', $debug);
$logger = $loggerFactory->createFileLogger('bar', 'foo/baz.log', SystemStream::STDERR);

/**
 * Creates `/tmp/log/www/foo/baz.log`
 * and outputs `[2019-02-15 12:42:59] bar.INFO: Hello, world! [] []` there
 * and to the STDERR. 
 */
$logger->info(
    'Hello, world!'
);