LoggingThe Ultimate Guide

your open-source resource for understanding, analyzing, and troubleshooting system logs

curated byloggly


PHP Logging Libraries

Many PHP logging libraries offer the most common functionality that any application might need; however, some libraries have advantages over others and some have taken a unique approach to solving problems. To standardize the solution, the PHP-FIG (PHP Framework Interop Group) recommended a logging interface that frameworks can accept as their logging API or build the logging system using it.

PSR-3 Logging Standard

The PSR-3 (PHP Standards Recommendation) logging standard defines a logging interface with eight methods (debug, info, notice, warning, error, critical, alert, emergency). The message passed to those functions should be a string or an object with a ‘__toString‘ method to cast it into a string. You can read more about the PSR-3 logging standard in the official documentation.

PSR-3 Logger Interface Specification

The PSR-3 standardizes the logging process through a common interface for logging. That way, you can use any PSR-3 compliant logger in your application and you maintain the flexibility to switch to a different logger in the future. You can read more about the full PSR-3 specification on the specification page on Github.

If you’re using composer, you can include the ‘psr/log‘ package and integrate it with your project in one of the following ways:

  • Implementing the ‘LoggerInterface‘ and defining the eight methods
  • Inheriting the ‘AbstractLogger‘ class and defining the ‘log’ method
  • Using the ‘LoggerTrait‘ and defining the ‘log’ method (Read more about traits on the PHP Documentation.)

Of course, instead of writing your own Logger classes, it’s much easier to use an existing package such as Monolog. Getting started with Monolog is easy, particularly if you’re already using composer to manage libraries. Check out the documentation for Basic Usage.


Monolog is PSR-3 compliant and has integration components for most popular frameworks like Laravel, Symfony, etc.

Monolog is one of, if not the best logging library out there for PHP. It supports logging to different handlers like database, browser console, chat solutions like Slack, log management solutions like Loggly, etc. You can also specify what level each handler should log. For example, you may want to log error events to a Slack channel while you want to notify your developers team on fatal errors.

Monolog also supports formatters. If you want to format your data as JSON before sending them to a service like Loggly, you can use the ‘JsonFormatter‘ class to do the job. You can read more about Monolog formatters in the documentation.

Here is an example of how to set up Monolog to log to a file:

Monolog also provides ‘Preprocessors’ to add some data to the message before it’s logged. You have a list of preprocessors that can add specific details about your system like memory usage, processor, server details, etc.


The Analog package is a minimal logging package that doesn’t try to add all optional features like log formatters and processors. Through static access to the ‘Analog‘ class, you can configure the log handler (file, email, database, etc.). You can check the list of handlers in the documentation.

The ‘handler‘ method also accepts a closure as a handler. The following example creates a handler to submit a simple log message to your Loggly account.

The ‘Analog‘ class is not PSR-3 compatible, but Analog provides a compatible class called ‘Logger’ that you can use if you want to keep a good level of abstraction. It simply implements the ‘Psr\Log\LoggerInterface‘ and calls the ‘Analog‘ class as shown above.


KLogger allows you to log to a specific file and format the output to fit your requirements. Here’s a simple example:

KLogger supports a number of formatting variables:

Parameter Description
date Current date (uses ‘dateFormat’ option)
level The PSR log level
level-padding White space needed to make this log level line up visually with other log levels in the log file
priority Integer value for log level (see ‘$logLevels’)
message The message being logged
context JSON-encoded context

KLogger is PSR-3 compliant and supports a list of options for formatting. Be sure to check the documentation for more details.


Log4PHP is an Apache Foundation package. It provides most of the functionality mentioned earlier like formatting, logging to different destinations, etc. It uses configuration to attach one or more appenders to the ‘Logger‘ class. Appenders are the same as handlers in the Monolog package. You can use an email appender, console appender, etc. It’s not PSR-3 compliant, and doesn’t use namespacing for its classes. It also hasn’t been updated in awhile and I don’t recommend using it in your projects; instead, you can use an active one like the Monolog package. Nevertheless, let’s start exploring it using a simple example.

To configure the logger, you have the option to use XML, INI or PHP. I’ll be using PHP to configure the logger in my examples, but you can read more about using XML and INI files for configuration in the documentation.

The above code will log the ‘INFO message’ and ‘ERROR message‘ and send them via email too. If you uncomment the ‘$mailAppender->setThreshold(“error”)‘ method call on the mail appender section, only the ‘ERROR message‘ will be sent via email.

You can use layouts to configure logs formatting for appender. The following example will format the console appender using the ‘LoggerLayoutPattern’ class to include the date, file, line number, and log message. You can read more about layouts in the documentation.

If you try to log an object, it will be logged using the PHP ‘var_dump‘ function. You may alter this behavior by using a predefined renderer or create your own. Let’s take this example from the official documentation.

This will dump the person object instance as it is, which is not what we want in most cases. The first step to fix this is to create a class that extends the ‘LoggerRenderer‘.

The ‘config.php’ file contains our logger configuration.

Written & Contributed by




This guide will help software developers and system administrators become experts at using logs to better run their systems. This is a vendor-neutral, community effort featuring examples from a variety of solutions

Meet Our Contributors Become a contributor