PHP Classes

File: TestClass.php

Recommend this page to a friend!
  Classes of Stefan Kientzler   XLogger PHP PSR Logger   TestClass.php   Download  
File: TestClass.php
Role: Example script
Content type: text/plain
Description: Class source
Class: XLogger PHP PSR Logger
Log events to browser console, text and XML files
Author: By
Last change: phpstan changes
Date: 4 years ago
Size: 1,407 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Psr\Log\LoggerAwareTrait;
use
Psr\Log\NullLogger;
// use Psr\Log\LoggerAwareInterface;

/**
 * Testclass to demonstrate the integration of any PSR-3 logger into own classes.
 * Either implement LoggerAwareInterface or integrate LoggerAwareTrait by use stetemant.
 */
class TestClass // implements LoggerAwareInterface
{
    use
LoggerAwareTrait;
   
   
/**
     * In both cases it makes sense to initialize the $logger property with an
     * instance of the PSR-3 NullLogger() so nowhere in the code have to be tested,
     * if any logger is set.
     */
   
public function __construct()
    {
       
$this->logger = new NullLogger();
    }
   
    public function
doSomething()
    {
       
$this->logger->info('Start {class}::doSomething()', ['class' => get_class($this)]);
        for (
$i = 1; $i < 10; $i++) {
           
// run a loop
           
$this->logger->debug('Run loop ({loop})', ['loop' => $i]);
        }
       
$this->logger->info('{class}::doSomething() finished', ['class' => get_class($this)]);
    }
   
    public function
causeException()
    {
        try {
           
$this->throwException();
        } catch (
Exception $e) {
           
$this->logger->error($e->getMessage(), ['exception' => $e]);
        }
    }
   
    protected function
throwException()
    {
        throw new
LogicException('Caused any Exception');
    }
}