Chapter 2. First notifications

With this first example, we want to be notified with Growl as soon as a critical condition occurs.

Using GrowlHandler, is no more no less simple, than with other monolog handlers.

GrowlHandler class constructor requires :

So to receive only CRITICAL events or higher, you have just to set the logging level to Logger::CRITICAL.

<?php

try {
    $growl = new GrowlHandler(
        array(), // with all default options
        Logger::CRITICAL
    );

} catch (\Exception $e) {
    // Growl server is probably not started
    echo $e->getMessage(), PHP_EOL;
}
[Tip]

DO NOT forget to try-catch instance creation, because it will attempt to connect to server, and raise a Net_Growl_Exception if its impossible.

Of course it may be combined with any other monolog handler. Here is now the full script:

<?php
require_once 'vendor/autoload.php';

use Bartlett\Monolog\Handler\GrowlHandler;

use Monolog\Logger;

// Create the logger
$logger = new Logger('my_logger');

// Create some handlers
try {
    $growl = new GrowlHandler(
        array(), // with all default options
        Logger::CRITICAL
    );

    $logger->pushHandler($growl);

} catch (\Exception $e) {
    // Growl server is probably not started
    echo $e->getMessage(), PHP_EOL;
}

// You can now use your logger
$logger->addInfo('My logger is now ready');

$logger->addError('An error has occured.');

try {
    throw new \RuntimeException();

} catch (\Exception $e) {
    $logger->addCritical(
        'A critical condition has occured. You will be notified by growl.',
        array('exception' => $e)
    );
}