Skip to content
This repository was archived by the owner on Jan 23, 2021. It is now read-only.

Getting Started

Emanuel Bitkov edited this page Jul 17, 2019 · 1 revision

This page describes how to simply implement Exceptionist into your project.

Exceptionist is a trait. So it actually extends any object with its methods providing it with a simple exception logging mechanism. Those methods are all private and intended to be only visible to the object itself. Public interfaces must be implemented by your own.

Implementation

Let's assume we have a basic object like so:

namespace MyApp;

class MyObject 
{
    # Your code
}

To implement Exceptionist just use the trait.

namespace MyApp;

class MyObject 
{
    use crystlbrd\Exceptionist\ExceptionistTrait;

    # Your code
}

And your good to go. Now your object inherited all methods out of the trait and can use them.

How To Use (Examples)

namespace MyApp;

use \Exception;
use crystlbrd\Exceptionist\ExceptionistTrait;
use crystlbrd\Exceptionist\Environment;

class MyObject 
{
    use ExceptionistTrait;

    /**
     * This method illustrates how to log and throw an exception
     */
    public function logginExceptions($someParamter)
    {
        /**
         * let's assume we are doing here some serious stuff and something
         * went terribly wrong. Now we want to throw an exception:
         */

        // This will throw an exception
        $this->log(new Exception("some important message"), Environment::E_LEVEL_ERROR);

        // this code won't be executed!
    }


    /**
     * this function illustrates how to log a debug message
     */
    public function logSomeDebugInfo()
    {
        /**
         * doing some stuff over here, like creating folders or anything like this.
         * we want to log a message for debugging purposes but not actually throw an exception
         */
   
        // this won't throw an exception
        // Exceptions will be logged by default as E_LEVEL_DEBUG
        $this->log(new Exception("some debugging info"));

        // code here will be executed
    }


    /**
     * this method illustrates how to manipulate the error level
     */
    public function throwAllOrNothing()
    {
        /**
         * By default Exceptionist will always log an exception internally, but only throw E_LEVEL_ERROR Exceptions.
         * To change that behavior you can set the $ExceptionistReportingLevel property.
         */

        // Setting the level to throw nothing
        $this->ExceptionistReportingLevel = Environment::E_LEVEL_NONE;

        // This will only log the exception and not throw it
        $this->log(new Exception("error"), Environment::E_LEVEL_ERROR);

        // Setting the level to throw everything
        $this->ExceptionistReportingLevel = Environment::E_LEVEL_DEBUG;

        // This will throw an exception
        $this->log(new Exception("debug"), Environment::E_LEVEL_DEBUG);

        // Setting the level to throw warnings and errors
        $this->ExceptionistReportingLevel = Environment::E_LEVEL_WARNING;

        // this won't throw an exception
        $this->log(new Exception("debug"));

        // but this will
        $this->log(new Exception("warning"), Environment::E_LEVEL_WARNING);

       // and this of course too
       $this->log(new Exception("error"), Environment::E_LEVEL_ERROR);
    }

    # your code
}

Clone this wiki locally