More Effective PHP Logging with Loggly

Originally published at: http://www.sitepoint.com/effective-php-logging-loggly/

This article was sponsored by Loggly. Thank you for supporting the sponsors who make SitePoint possible.

Introduction

Server overloaded, library throwing an exception, error while sending email: these errors are unfortunately part of every system. If you’re in charge of making a system function well, that’s still cold comfort. What’s more, I bet you already have all the data you need to solve them sitting in your log files. But having access to that data doesn’t really help unless you have a way to store, process, and analyse that data? In this article I’ll explain how to use this data more effectively by using a PHP logging library with a proper log management solution (in this case, Loggly). I’ll also show an example of how to use a log management service to store and analyze them easier.

PHP Logging

When logging with PHP, we tend to use the error_log and trigger_error functions, or we can use an error handler to make the logging process more generic. If you choose to do it this way, you’ll need to wrap your functions inside some kind of object to make things cleaner and flexible. You can also forward your logs directly to your system to handle them, using the syslog function.

openlog('php', LOG\_CONS | LOG\_NDELAY | LOG\_PID, LOG\_USER | LOG\_PERROR); syslog(LOG\_ERR, 'Error!'); syslog(LOG\_INFO, 'Hello World!'); closelog();

But what would you do if you had to log to multiple places at the same time, or you were sending logs to a given service depending on the error level? Rather than using built-in tools, it’s often easier to use logging libraries.

Why I Use Monolog

While some recommend libraries like log4php, KLogger and Monolog try to solve those common problems, they have a few limitations, and Monolog has a lot of advantages over them.

  • It’s PSR-3 compliant.
  • Includes handlers for a variety of services, including Loggly.
  • Support for formatters to customise your logs output.
  • Helpers for development logging like the BrowserConsoleHandler for logging to the browser console.

Make sure to check the documentation for more details about the package. Most popular frameworks include Monolog out of the box, check the full list on the documentation. If you don’t have Monolog installed, you can add it to a project using Composer.

composer require monolog/monolog

The Problems of Logging at Scale

Since logging requires writing to the disk, doing backups and searching files, some companies create a separate service to handle the job, usually a set of scripts or apps to grep files searching for info when some kind of error occurrs. As your company or service scales up, this can quickly become a nightmare for your developers and analysts. Another, better, alternative is a cloud based service for their log storage and analysis and this has a lot of benefits, as we’ll discuss further in this article.

What Is Loggly?

There are several log management services that will make storing and analyzing your logs easier. Loggly is the most popular one and it has several ways to integrate with PHP.. Once Loggly receives your logs, you’ll be able to search, group and visualize your data in a really impressive way. You can try it for free. You only pay if you have sizable traffic on your site. Let’s start first by looking on how to install it on your server to track your system logs.

Using Loggly with Monolog

If you decided to go with the Monolog package library for your logging process, it’s very easy to get it integrated with any log management service, including Loggly.

By default, it comes with a LogglyHandler for Loggly.

 $logger = new \Monolog\Logger('local_test_app'); $logger->pushHandler(new \Monolog\Handler\LogglyHandler('YOUR_TOKEN/tag/monolog'));  

After creating a Monolog instance, we’ll push our handler to the list of registered handlers, and you’ll need to provide your token as previously mentioned. The tag part is optional, but it’s always a good idea to separate your log entries by tagging them accordingly. Now we’re ready to start logging to our service using Monolog.

 $logger->addInfo("Info test from monolog"); //$logger->addWarning("Warning test from monolog");  

Continue reading this article on SitePoint

Here is another PSR-3 compatible Loggly-Logger.

You only pay if you have sizable traffic on your site.

That’s a lie, they offer only free 30 day trial, then you have to pay.

First of all, you should try to be nice when talking to people. Loggly offers a PRo plan and you can pay depending on your logs volume.

So if my logs volume is low enough it will always be free?

It says that on the pricing page.

Thanks, I missed that

Lite – Free Forever Plan
Includes centralized logging, search & filters, and persistent workspaces, for a single user. Up to 6GB/mo. Start trial.

though IMHO “trial” is a bit misleading. (my fault for equating it to “time” instead of “usage”)

No problem, They have good plans for small needs :wink:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.