Building Your Startup: Error Logging

This tutorial is part of the Building Your Startup With PHP series on СodeHolder Tuts+. In this series, I’m guiding you through launching a startup from concept to reality using my Meeting Planner app as a real-life example.

Building Your Startup: Error Logging

Every step along the way, I’ll release the Meeting Planner code as open-source examples you can learn from. I’ll also address startup-related business issues as they arise.
Building Your Startup: Error Logging

Introduction to Error Logging

Once Meeting Planner went into alpha release and people began actively using it to schedule meetings, errors would occur that were rarely reported by users. But, in conversations on the phone with people, they’d commonly tell me about problems they ran into.

In today’s tutorial, I’ll walk you through how I tried two different cloud-based logging services, Rollbar and Sentry, and why I ultimately settled on Sentry for the time being.

If you haven’t tried out Meeting Planner yet, go ahead and schedule your first meeting. I do participate in the comment threads below, so tell me what you think! You can also reach me on Twitter @reifman. I’m especially interested if you want to suggest new features or topics for future tutorials.

As a reminder, all of the code for Meeting Planner is written in the Yii2 Framework for PHP. If you’d like to learn more about Yii2, check out our parallel series Programming With Yii2.

Let’s begin.

My Experience With Rollbar

Building Your Startup: Error Logging

Both Rollbar and Sentry were recommended to me by Alex Makarov, one of the lead volunteer developers behind the Yii Framework. Alex has been very helpful to me expanding my expertise about Yii2 while I’m building Meeting Planner and writing about the benefits of the framework.

Initially, I thought about creating a secondary database connection to log my errors and tracking them manually. But then, I noticed that these services provided a lot of advanced reporting functionality. For example, they count the number of identical errors and report them by severity. At some point, I’ll write a more general-purpose Yii2 error logging tutorial for the Yii programming series, but for now, I just dove in.

And, while I got the basics working use some open-source Yii2 plugins (there are several) for Rollbar. I started having production errors that actually were caused by the plugins.

Building Your Startup: Error Logging

Overall, I was very impressed with Rollbar’s web user interface and services. But I believe some problem with my configuration and the open-source Yii extension led to new problems.

Building Your Startup: Error Logging

In other words, people were seeing production errors due to my attempts at recording other errors. It was worsening people’s impression of Meeting Planner. Even I was running into newly mysterious production errors—and it was confusing and wasting my time.

In order to optimize their experience and my time, I disabled the error handling temporarily and decided to try using Sentry.

Just to be clear, I think with a better Rollbar Yii2 extension and more careful configuration (which is never documented well with third-party plugins like these), the service would have worked well. I don’t want to discourage you from trying it.

Getting Started With Sentry

Sentry is a comparable logging service, though less expensive than Rollbar. Lower cost is often helpful to startups.

Building Your Startup: Error Logging

You can see that their first bump in pricing begins at only $29 per month:

Building Your Startup: Error Logging

Signing Up

I went ahead and used the Sign Up form to get started:

Building Your Startup: Error Logging

Creating a New Project

Within Sentry, I created a new project for Meeting Planner to track bugs within this domain:

Building Your Startup: Error Logging

Then, Sentry offers you guides for a number of languages and frameworks (Yii is not included yet, hint to Sentry team!):

Building Your Startup: Error Logging

Configuring a Yii2 Sentry Plugin

For Meeting Planner, I chose to use the Notamedia Yii2 Sentry extension. I ran into a couple of minor issues that slowed me down.

The First Problem—Not the Plugin’s Fault

Firstly, I had trouble finding my unique authorization codes for my site to identify itself to Sentry. Usually, web services make these very easy to find, but it took me about 10 to 15 minutes to find.

Near the bottom of the left-hand menu of the Settings page, there’s a Client Keys (DSN) option which displays them:

Building Your Startup: Error Logging

I broke the keys into their three parts and placed them in my mp.ini file for all of Meeting Planner’s keys:

#rollbar_key = "bxxxxxxxxxxxxxxxxxxxxxxxxxxxxf"
sentry_key_public = "bxxxyyyyxxxzzzzyyyyccccc"
sentry_key_private = "8b3333xxxyyyyxxxzzzzyyyyccccc4446"
sentry_id ="9922"

Within /frontend/config/main.php, I updated the plugin’s recommended settings to use my specific variables from mp.ini. They begin below with $config:

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'notamediasentrySentryTarget',
            'dsn' => 'http://'.$config['sentry_key_public'].
                ':'.$config['sentry_key_private'].
                '@sentry.io/'.$config['sentry_id'],
            'levels' => ['error', 'warning'],
            'context' => true, // Write the context information. The default is true.
        ],
      ],

The Second Problem

Then, I couldn’t get Sentry to capture the error reports. It turned out that the documentation for notamedia’s plugin used the URL sentry.com instead of sentry.io. Luckily, I figured that out within just a few minutes and didn’t waste too much time.

Sentry Introductory Emails

Sentry does send you an introductory email, but I didn’t notice it arrive until later. Still, it’s very helpful:

Building Your Startup: Error Logging

Testing Sentry With Meeting Planner

On one of my controllers in my local environment, I just added the code to actionIndex which would load when I load a page on the site:

Yii::warning have a problem Portland.');

Very quickly, bug reports started rolling in from the local and production environment. They land in the Sentry dashboard.

The Sentry Dashboard

Building Your Startup: Error Logging

Here’s an example of the reporting for one of my fake testing bugs, “We have a problem Portland.” It shows all the helpful data that Sentry collects for you to make discovering, identifying and debugging production errors easier:

Building Your Startup: Error Logging

Above, you can see tags related to URLs and the user’s browser. Below, the report continues showing you the lines of code that caused the error:

Building Your Startup: Error Logging

It also shows cookies above and headers and user IDs below:

Building Your Startup: Error Logging

It’s all very helpful.

Sentry Email Alerts to New Bugs

By default, Sentry also emails you bugs (so you know about them immediately):

Building Your Startup: Error Logging

In fact, very quickly, Sentry showed me that WordPress hacker bots were already pinging for wp-login and wp-admin on my site. Even though they don’t exist here, I want to minimize their impacts. So I added a neverland action in the front-end SiteController.php to just end the application execution.

public function actionNeverland()
    {
      // for abusive scripts like wp-login probes
      Yii::$app->end();
    }

Sentry also found a permission problem that was occurring with caching in my background tasks. This too was helpful.

For now, I’m sold on Sentry, and I look forward to some quick gains from it as it’s live in production during the ongoing preview release.

What’s Next?

I hope you’ve enjoyed learning about production error handling and logging.

Watch for upcoming tutorials here in the Building Your Startup With PHP series. I’ve done a lot of feature development and polishing this year. Now, it’s time to get people using Meeting Planner. Soon, I’ll write about promotion plans for the preview release—it’s another key element of launching your startup.

If you haven’t yet, please try out scheduling and share Meeting Planner with your friends. As usual, I’d appreciate it if you share your experience below in the comments, and I’m always interested in your suggestions. You can always reach me on Twitter @reifman directly.

I’m also getting closer to launching the experiment with WeFunder based on the implementation of the SEC’s new crowdfunding rules. You can follow our profile there if you’d like. I will also write more about this in a future tutorial.

Related Links