PHP Classes

File: src/Generics/Logger/BasicLogger.php

Recommend this page to a friend!
  Classes of Maik Greubel   PHP Generics   src/Generics/Logger/BasicLogger.php   Download  
File: src/Generics/Logger/BasicLogger.php
Role: Class source
Content type: text/plain
Description: Common logging functionality
Class: PHP Generics
Framework for accessing streams, sockets and logs
Author: By
Last change: Update of src/Generics/Logger/BasicLogger.php
Date: 7 months ago
Size: 3,474 bytes
 

Contents

Class file image Download
<?php
/**
 * This file is part of the PHP Generics package.
 *
 * @package Generics
 */
namespace Generics\Logger;

use
Generics\Streams\MemoryStream;
use
Psr\Log\AbstractLogger;
use
Psr\Log\LogLevel;

/**
 * This abstract class provides a basic logger functionality
 *
 * @author Maik Greubel <greubel@nkey.de>
 */
abstract class BasicLogger extends AbstractLogger
{

   
/**
     * The level threshold where to log
     *
     * @var string
     */
   
private $level;

   
/**
     * Set the log level threshold
     *
     * @param string $level
     * @return BasicLogger
     */
   
public function setLevel(string $level): BasicLogger
   
{
       
$this->level = $level;
        return
$this;
    }

   
/**
     * Checks the given level
     *
     * @param string $level
     * @throws \Psr\Log\InvalidArgumentException
     */
   
private static function checkLevel($level)
    {
        if (
$level != LogLevel::ALERT && $level != LogLevel::CRITICAL && $level != LogLevel::DEBUG && //
       
$level != LogLevel::EMERGENCY && $level != LogLevel::ERROR && $level != LogLevel::INFO && //
       
$level != LogLevel::NOTICE && $level != LogLevel::WARNING) {
            throw new \
Psr\Log\InvalidArgumentException("Invalid log level provided!");
        }
    }

   
/**
     * Format the message to log and return a memory stream of it
     *
     * @param integer $level
     * The arbitrary level
     * @param string $message
     * The message to log
     * @param array $context
     * The context of logging
     *
     * @return \Generics\Streams\MemoryStream The formatted message
     */
   
protected function getMessage($level, $message, array $context = array()): MemoryStream
   
{
       
/**
         * This check implements the specification request.
         */
       
self::checkLevel($level);
       
       
$ms = new MemoryStream();
       
       
$ms->write(strftime("%Y-%m-%d %H:%M:%S", time()));
       
$ms->interpolate("\t[{level}]: ", array(
           
'level' => sprintf("%6.6s", $level)
        ));
       
$ms->interpolate($message, $context);
       
$ms->write("\n");
       
        return
$ms;
    }

   
/**
     * Must be implemented by concrete logger
     *
     * @param integer $level
     * The arbitrary level
     * @param string $message
     * The message to log
     * @param array $context
     * The context of logging
     */
   
abstract protected function logImpl($level, $message, array $context = array());

   
/**
     *
     * {@inheritdoc}
     * @see \Psr\Log\LoggerInterface::log()
     */
   
public function log($level, $message, array $context = array())
    {
        if (
$this->levelHasReached($level)) {
           
$this->logImpl($level, $message, $context);
        }
    }

    protected function
levelHasReached($level): bool
   
{
       
$result = true;
       
       
$orderedLevels = array(
           
LogLevel::EMERGENCY => 0,
           
LogLevel::ALERT => 1,
           
LogLevel::CRITICAL => 2,
           
LogLevel::ERROR => 3,
           
LogLevel::WARNING => 4,
           
LogLevel::NOTICE => 5,
           
LogLevel::INFO => 6,
           
LogLevel::DEBUG => 7
       
);
       
        if (
$this->level) {
           
$threshold = $orderedLevels[$this->level];
           
$reached = $orderedLevels[$level];
           
$result = $reached <= $threshold;
        }
       
        return
$result;
    }
}