Chrome Logger Adapter

Occasionally, you may want to log to the browser console. Thanks to the ChromePhp library, you can do just that. Internally, this uses the Chrome Logger Google Chrome extension.

Installation

Strangly enough, Firefox 43+ comes with built-in support for console logging via the Chrome Logger protocol. For Chrome, you need to install the Chrome Logger Google Chrome extension.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\Chrome as ChromeLogger;

$log_adapter = new ChromeLogger();
$log = new Log($log_adapter);

// If you wanted to change the name of the ChromePhp class, you can
// do the following:
$log_adapter = new ChromeLogger('\MyChromePhp');
$log = new Log($log_adapter);

Notes

The Chrome Logger has fewer loglevels (info, warn, error) than the PSR-3 specification; therefore, the Chrome Logger loglevels had to be mapped to the PSR-3 loglevels (see below).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
protected $loglevel_map = [
    LogLevel::DEBUG=>'info',
    LogLevel::INFO=>'info',
    LogLevel::NOTICE=>'warn',
    LogLevel::WARNING=>'warn',
    LogLevel::ERROR=>'error',
    LogLevel::CRITICAL=>'error',
    LogLevel::ALERT=>'error',
    LogLevel::EMERGENCY=>'error'
];