PHP Classes

File: src/Generics/Logger/ExceptionLoggerTrait.php

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

Contents

Class file image Download
<?php

/**
 * This file is part of the PHP Generics package.
 *
 * @package Generics
 */
namespace Generics\Logger;

use
Psr\Log\LogLevel;
use
ErrorException;
use
Exception;
use
RuntimeException;

/**
 * Implementation for logging exceptions
 *
 * @author Maik Greubel <greubel@nkey.de>
 */
trait ExceptionLoggerTrait
{

    abstract protected function
logImpl($level, $message, array $context = array());

   
/**
     *
     * {@inheritdoc}
     * @see \Generics\Logger\ExceptionLogger::logException()
     */
   
public function logException(Exception $ex)
    {
       
$level = LogLevel::ALERT;
       
        if (
$ex instanceof ErrorException) {
           
$level = LogLevel::ERROR;
        } elseif (
$ex instanceof RuntimeException) {
           
$level = LogLevel::EMERGENCY;
        }
       
       
$this->logImpl($level, "({code}): {message}\n{stackTrace}", array(
           
'code' => $ex->getCode(),
           
'message' => $ex->getMessage(),
           
'stackTrace' => $ex->getTraceAsString()
        ));
    }
}