Code Monkey home page Code Monkey logo

lern's Introduction

LERN (Laravel Exception Recorder and Notifier)

Latest Version Software License Build Status Scrutinizer Code Quality Code Coverage Dependency Status Total Downloads

LERN from your mistakes

LERN is a Laravel 5 package that will record exceptions into a database and will send you a notification.

Currently supported notification channels via Monolog

Migrating from 2.x to 3.x

Version 3.x introduces the ability to collect more information from the error such as the user_id, url, method, and input data. In order to use 3.x you will need to copy over the new config file, the migration file and then migrate it.

# This will only copy over the migration file. For the config file you can either include the --force flag (Which will overwrite it) or copy it manually from github 
php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"
php artisan migrate

Installation

Install via composer - In the terminal:

composer require tylercd100/lern

Now add the following to the providers array in your config/app.php

Tylercd100\LERN\LERNServiceProvider::class

and this to the aliases array in config/app.php

"LERN" => "Tylercd100\LERN\Facades\LERN",

Then you will need to run these commands in the terminal in order to copy the config and migration files

php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"

Before you run the migration you may want to take a look at config/lern.php and change the table property to a table name that you would like to use. After that run the migration

php artisan migrate

Usage

To use LERN modify the report method in the app/Exceptions/Handler.php file

public function report(Exception $e)
{
	if ($this->shouldReport($e)) {
	    LERN::handle($e); //Record and Notify the Exception
	    /*
	    OR...
	    LERN::record($e); //Record the Exception to the database
	    LERN::notify($e); //Notify the Exception
	    */
	}
	
	return parent::report($e);
}

Dont forget to add this to the top of the file

//If you updated your aliases array in "config/app.php"
use LERN;
//or if you didnt...
use Tylercd100\LERN\Facades\LERN;

Recording

You can call LERN::record($exception); to record an Exception to the database. To query any Exception that has been recorded you can use ExceptionModel which is an Eloquent Model

use Tylercd100\LERN\Model\ExceptionModel;
$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first();

To change what is recorded in to the database take a look at config/lern.php

'record'=>[
	'table'=>'vendor_tylercd100_lern_exceptions',
	'collect'=>[
	    'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc...
	    'data'=>false, //When true it will collect Input data
	    'status_code'=>true,
	    'user_id'=>false,
	    'url'=>false,
	],
],

Notifications

LERN uses the Monolog library to send notifications. If you need more than the supported notification channels, then you can add your own custom Monolog handlers. To start using any of the supported handlers just edit the provided config file config/lern.php.

Custom Monolog Handlers

To use a custom Monolog Handler call the pushHandler method

use Monolog\Handler\HipChatHandler;
$handler = new HipChatHandler($token,$room);
LERN::pushHandler($handler);
LERN::notify($exception);

Changing the text

//Change the subject
LERN::setSubject("An Exception was thrown!");

//Change the message body
LERN::setMessage(function($exception){
    $msg = "";

    //Get the route
    $url = Request::url();
    $method = Request::method();
    if($url) {
        $msg.="URL: {$method}@{$url}".PHP_EOL;
    }

    //Get the User
    $user = Auth::user();
    if($user) {
        $msg.="User: #{$user->id} {$user->first_name} {$user->last_name}".PHP_EOL;
    }

    //Exception
    $msg.=get_class($exception).":{$exception->getFile()}:{$exception->getLine()} {$exception->getMessage()}".PHP_EOL;

    //Input
    $input = Input::all();
    if(!empty($input)){
        $msg.="Data: ".json_encode($input).PHP_EOL;
    }

    //Trace
    $msg.=PHP_EOL."Trace: {$exception->getTraceAsString()}";
    return $msg;
});

LERN::notify($e); //Notify the Exception

Roadmap

  • Support more Monolog Handlers
  • Exception report page or command to easily identify your application's issues.
  • Notification rate limiting and/or grouping.

lern's People

Contributors

leamasuero avatar scrutinizer-auto-fixer avatar tylercd100 avatar vincentmendiola avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.