Looking for an easy and more reliable way to store and view your Laravel Application logs? You’re in the right place. We’re going to be integrating Papertail as log storage for your Laravel application.
1. Setup Papertrail account:
First thing first. Let’s create a Papertrail account! They offer a free tier with 100MB/month and many other pricing options based on your needs.
Once you’re logged in, go to your dashboard and then click the Add Systems.
At the top of the page you will see your custom host and port. Take note on that because we’re going to need them below.
2. Configure Laravel:
In the folder App/Providers add the provider PaperTrailServiceProvider.php with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace App\Providers; use Log; use Monolog\Formatter\LineFormatter; use Monolog\Handler\SyslogUdpHandler; use Illuminate\Support\ServiceProvider; class PaperTrailServiceProvider extends ServiceProvider { /** * Register Papertrail Service Provider * * @return void */ public function register() { $monolog = Log::getMonolog(); $syslogHandler = new SyslogUdpHandler(env('PAPERTRAIL_URL'), env('PAPERTRAIL_PORT')); $formatter = new LineFormatter('%channel%.%level_name%: %message% %extra%'); $syslogHandler->setFormatter($formatter); $monolog->pushHandler($syslogHandler); } } |
Open your Laravel config file config/app.php. In the $providers array add the PaperTrailServiceProvider as follow:
1 |
App\Providers\PaperTrailServiceProvider::class |
Finally add your Papertrail details to your .env file as follow:
1 2 |
PAPERTRAIL_URL=YOURHOST PAPERTRAIL_PORT=YOURPORT |
You’re all set, you can start logging using the same Laravel logging levels.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Http\Controllers; use Log; class TestController extends Controller { public function index() { Log::error('Error description'); ... } } |