Logmatic.io Blog

Two Fresh PHP Logging Libraries:
PHP Monolog & Zend-log

So you are a PHP developer and want to upgrade your app performance. What do you think of PHP logging for a solution? If logging has been on your mind lately, you’ve come to the right place. We’ll explain PHP logging via two different logging libraries: Monolog and Zend-log. Find out how to implement the logging libraries, how to send your logs to Logmatic.io, how to configure them & a few extra tricks to smoothen logging.

php logo

Let’s get it kicking and see what you can do with each one of them!

PHP Monolog

  • Installation
  • There are two ways of installing PHP Monolog, feel free to pick one: add Monolog as dependency through Composer or install it manually. Find the steps to follow on our doc.

  • Configuration
  • You can either configure PHP Monolog to write logs into files or to stream them directly to Logmatic.io. Although we recommend writing your log into a file and then forwarding it with a shipper to avoid network issues, the choice is yours.

  • Context and meta descriptions
  • We cannot stress this enough, log with context – to fully understand phenomena you’re tracking. Lucky you, this is easy to do with Monolog thanks to its callback feature that facilitates enriching your events. A glimpse of it:

    <?php $log->pushProcessor(function ($record) {
        // record the current user
        $user = Acme::getCurrentUser();
        $record['context']['user'] = array(
            'name' => $user->getName(),
            'username' => $user->getUsername(),
            'email' => $user->getEmail(),
        // Add various tags
        $record['context']['tags'] = array('key' => 'value');
        // Add various generic context
        $record['extra']['key'] = 'value';
        return $record;
  • Bonus
  • We shared a solution on GitHub to help you keep the right number type for all your logs: make sure you send real numbers as real numbers and not whole numbers.

Now… Take PHP Monolog logging to the next level by visiting our doc.

PHP Zend-log

  • Installation
  • If you’re using a Zend framework, the handler is already included. Otherwise, you friend Composer is here to help you add Zend-log as a dependency; you can also install it manually.

  • Configuration
  • We’ll make it simple: the only configuration option is the log shipper one. This means you should configure Zend to write logs into files and then forward them with a log shipper. Here’s a JSON-friendly idea on how to get started:

    <?php use Zend\Log\Logger; use Zend\Log\Writer\Stream; use Zend\Log\Formatter\JsonFormatter; // create a logger $logger = new Logger(); // create a writer $writer = new Stream('file://' . __DIR__ . '/application-json.log'); // create a Json formatter $formatter = new JsonFormatter(); $writer->setFormatter($formatter);
    // bind
  • Meta context
  • We’re here again: context and meta descriptions. This PHP logging library has a great processor that will provide additional info automatically.

If this looks fun, take the adventure further and visit our doc for more details.

Related Posts