More Effective PHP Logging with Loggly

Share this article

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

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, so 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 occurs). 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");
Loggly and Monolog

Loggly with Laravel

Since Laravel is using Monolog for the logging process, we can easily bind our handler to it.

$handler = new \Monolog\Handler\LogglyHandler('YOUR_TOKEN/tag/monolog'); $logger = Log::getMonolog(); $logger->pushHandler($handler); // using the Log facade Log::info("Test from Laravel"); Log::warning("Test from Laravel");

Configuring Loggly on Heroku

When you move your application to production, you want to make sure that your logs tracking is working properly. In this section we are going to attach Loggly to our Heroku application. Heroku uses Log Drains to help you forward your logs to an external logging service. You can check the documentation for more details about the installation process.

heroku drains:add --app HEROKU_APP_NAME

You need to update the TOKEN with your actual token that you can find on the Source setup > Customer Tokens page. The HEROKU_APP_NAME can be omitted if you’re already logged into your Heroku instance, otherwise you need to specify your Heroku application name.

The drain URL ends with tag/heroku, this will help us filter our logs using the defined tag, we will talk more about this later.

Loggly and Heroku

Loggly can be easily integrated with any external service like Heroku. Check the Loggly documentation for an overview of log transmission methods and available scripts or libraries.

Analyzing Logs with Loggly

Now that we’ve discussed how to send your logs to Loggly, we can start analysing and working with our data. The search page provides a set of tools to filter, analyze and visualise our logs.

Loggly's search page

The dashboard shows a timeline of your events along with a list of logs at the bottom. The top of the page contain a search box and a date range to filter events, you can select the last 30 minutes or specify a custom date range for example and click search to validate.

Filtering Logs

You can use the search input to filter data using a specific term like “email” or “event_*”. Loggly will search inside the body of your log entries and display the result at the bottom of the page. You can also use fields for searching, like “tag:monolog” to filter the events sent previously from Monolog. The left side menu is called the Dynamic Field Explorer and it can help you identify the available field filters.

Loggly's field explorer

Let’s take the following examples to better understand how to analyze logs.

Internal Server Errors

An internal server error is thrown when your server was unable to process a valid request from the user. In PHP, details about this error can be found inside your Apache logs, if you register an error handler using PHP you can group your own log files so you can analyze them and fix errors according to the log message.

You can use the Field Explorer widget on the left side of the page to filter down logs using the apache 5XX status code. The search can be specific like “apache.status:500” or you can make it more generic like “apache.status:[500 TO 599]”.

Internal server error alerts

Fatal Errors

In PHP, when a fatal error is thrown, the program will stop the execution and log the error to your system. Because fatal errors are not supposed to happen on production, Loggly provides an easy way to track errors’ severity from different sources. The “syslog.appName:php” term will only show PHP logs, now we need to show errors from a specific severity using the syslog.severity:Error term, we can also specify a range like “syslog.severity:[Warning TO Error]”. You can read more about the list of available fields on the documentation.

syslog.appName:php AND syslog.severity:Error

Most of the time we’re displaying results as a list, but you may want to use another graph from the list of charts at the bottom of the page.

Pie chart showing alert severity


One of my favorite features on Loggly is the alerts tool. You can configure Loggly to send notifications to your email or another service whenever an action occurs on your application. Lets go through the process in detail.

First you need to create a new search and save it. Lets take this search for example: “syslog.appName:php AND php.level:”Fatal Error””

This will show us the list of PHP fatal errors on our system. Next, we need to save this search criteria. Click on the little star on the top right of the dashboard page, select the “Save this search as…” item and name the search.

Fatal errors Adding a notification for fatal errors

Now we can go to the list of alerts and click Add new to create a new one. After giving a name and a description to your alert, you can select a saved search from the list and a condition (Alert if count is >= 1 within 5 minutes). For my example I will send an email notification, but if you have a service that automatically passes the error to your team for verification, you can configure it to post it to your endpoint. You can read more about alerts on the documentation.

A notification for a fatal error


Loggly can change the way you deal with your logs, from regexp search to archiving. The service also has great UX, and the documentation is very straightforward, covering the majority of the possibilities. You can start with a free trial to test all the features. If you have any questions, feel free to post them below and I will do my best to answer them.

How do you make PHP logging easier?

Frequently Asked Questions (FAQs) about PHP Logging with Loggly

What makes Loggly a good choice for PHP logging?

Loggly stands out as a powerful PHP logging tool due to its robust features and ease of use. It provides centralized log management, which means you can access and manage all your logs from one place. This is particularly useful for large-scale applications where logs are generated from multiple sources. Loggly also supports a wide range of log formats, including PHP, making it versatile for different applications. It offers real-time log monitoring and analysis, which can help in identifying and resolving issues promptly. Additionally, Loggly provides advanced search capabilities, making it easier to find specific log entries.

How does Loggly compare to other PHP logging libraries?

Compared to other PHP logging libraries, Loggly offers more advanced features. While most libraries allow for basic logging functionalities, Loggly goes a step further by providing centralized log management, real-time monitoring, and advanced search capabilities. It also supports a wider range of log formats. However, it’s important to note that the choice of a logging library largely depends on the specific needs of your application. Some developers might prefer simpler libraries for smaller applications, while others might need the robust features of Loggly for larger, more complex applications.

Is Loggly easy to integrate with PHP?

Yes, Loggly is relatively easy to integrate with PHP. It provides a straightforward API that you can use to send logs from your PHP application to Loggly. The process involves installing the Loggly library, configuring it with your Loggly customer token, and then using the library’s functions to send logs. Detailed instructions and code examples are provided in the Loggly documentation, making the integration process easier even for beginners.

Can I use Loggly for real-time PHP log monitoring?

Absolutely. One of the key features of Loggly is its real-time log monitoring capabilities. This means you can view and analyze your PHP logs as they are generated, allowing you to identify and resolve issues promptly. Loggly also provides alerting features, which can notify you when certain conditions are met in your logs. This can be particularly useful for detecting and responding to critical issues.

How does Loggly’s search functionality work?

Loggly’s search functionality is designed to make it easier to find specific log entries. You can search for logs based on various parameters, such as the log level, the source of the log, or any other data contained in the log. Loggly also supports advanced search queries, allowing you to combine multiple parameters and operators for more precise search results. The search results are displayed in a user-friendly format, with options to further filter and sort the logs.

What are some alternatives to Loggly for PHP logging?

While Loggly is a powerful tool for PHP logging, there are several other libraries you might consider. These include Monolog, which is a widely used PHP logging library with support for various log handlers; Apache Log4php, a versatile logging framework for PHP; and KLogger, a simple file-based logging library for PHP. Each of these libraries has its own strengths and weaknesses, so the best choice depends on your specific needs.

Is Loggly suitable for large-scale PHP applications?

Yes, Loggly is well-suited for large-scale PHP applications. Its centralized log management feature makes it easier to handle logs from multiple sources, which is common in large applications. Additionally, its real-time monitoring and advanced search capabilities can help in managing the large volume of logs generated by such applications. However, it’s important to note that Loggly is a paid service, so you’ll need to consider the cost factor when choosing a logging tool for large-scale applications.

Can I use Loggly for PHP error logging?

Yes, you can use Loggly for PHP error logging. In fact, error logging is one of the most common uses of Loggly. It allows you to capture and analyze error logs from your PHP application, helping you identify and resolve issues. Loggly supports various error log formats, and you can customize the logging level to capture different types of errors.

How secure is Loggly for PHP logging?

Loggly takes security seriously and implements several measures to protect your log data. This includes encryption of data in transit and at rest, regular security audits, and compliance with industry standards such as SOC 2. However, as with any third-party service, it’s important to review their security practices and ensure they meet your requirements.

Can I customize the log format in Loggly?

Yes, Loggly allows you to customize the log format. This means you can structure your logs in a way that best suits your needs. You can include various data in your logs, such as the log level, the source of the log, timestamps, and any other relevant information. This can make it easier to analyze the logs and extract useful insights.

Younes RafieYounes Rafie
View Author

Younes is a freelance web developer, technical writer and a blogger from Morocco. He's worked with JAVA, J2EE, JavaScript, etc., but his language of choice is PHP. You can learn more about him on his website.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form