Want to get more articles like this one? Join my newsletter

How to send Laravel Vapor logs to Papertrail

As a serverless application, Ymir runs on Laravel Vapor. (Yes, a serverless WordPress platform runs on a serverless Laravel platform. Very meta lol.) Laravel Vapor is such an amazing service, and it’s why I built Ymir. But there are a few rough spots and one of them is logging.

CloudWatch is terrible for monitoring logs

If you ask around, you’ll find that most people who output logs on Vapor send them to CloudWatch. You can do that using the stderr channel. Lambda will pick up everything sent to the error log and send it to CloudWatch.

The problem with this solution is that CloudWatch isn’t a very good platform for working with logs. In fact, it’s pretty terrible. It’s hard to get a feed of all your Lambda logs to work with. At least, I never really figured out a good way to do it.

That’s why Ymir didn’t really have any logging. It just had error monitoring with Sentry. Error monitoring is great and necessary, but it doesn’t quite solve the same problem logging does. (Although there’s some overlap.)

But there’s good logging software and one of them is Papertrail. Laravel even has a channel for it by default. That’s how popular it is.

Papertrail is great, but doesn’t work with UDP

Unfortunately, when asking around, no one could get Papertrail to work with Laravel Vapor. That’s because Lambda blocks UDP and that’s how you send your logs to Papertrail by default.

I highlight the default because almost everyone (myself included) thought this was the only way to send logs to Papertrail. It’s not. You can send them via TCP.

Something my friend Shawn wrote about a few years ago. He was trying to get Papertrail to work with Platform.sh, but they also block UDP. He ended up finding out that you could send it via TCP.

Configuring Laravel to send logs to Papertrail using TCP

In his article, Shawn created a new logger class to send his logs to Papertrail. You actually don’t even need to do that. You can just make some changes to the default papertrail log channel in your logging.php configuration file.

<?php

# config/logging.php

use Monolog\Handler\SocketHandler;

return [

    // ...

    'channels' => [

        // ...

        'papertrail' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => SocketHandler::class,
            'handler_with' => [
                'connectionString' => 'tls://'.env('PAPERTRAIL_URL').':'.env('PAPERTRAIL_PORT'),
            ],
        ],

        // ...

    ],

];

Here’s the configuration you need to use. Instead of the monolog SyslogUdpHandler, you want to use the SocketHandler. You then replace the handler_with with the connection string used for TCP connections. You still use the same two papertrail environment variables as before.

On the Papertrail side, you just need to make sure that your log destination accepts TLS encrypted connections over TCP. As you can see above, this is just a checkbox in the Papertrail admin.

Sweet sweet logs

So that’s it! With this, you’ll finally be able to view your Laravel Vapor logs in an application that makes them useable. The only thing left is to generate useful logs to view. (Another challenging problem in itself!)

Creative Commons License