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: const error in FirePHP / context display

- fixed error in const definition in FirePHP
- FirePHP and ChromePHP Logger:: output context key(s) as info only in case message does't include placeholder for
Date: 6 hours ago
Size: 1,403 bytes
 

Contents

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('Catch Exception', ['exception' => $e]);
        }
    }
   
    protected function
throwException()
    {
        throw new
Exception('Caused any Exception');
    }
}


For more information send a message to info at phpclasses dot org.