Last active
December 22, 2016 01:47
-
-
Save frankmullenger/ffb43277bd2f3c40dff6afea95e2996f to your computer and use it in GitHub Desktop.
Logging to file and Chrome console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Logging to file (create notices.log with necessary permissions for apache to write to) | |
$handler = new Monolog\Handler\StreamHandler('/var/www/html/assets/notices.log'); | |
$handler->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true)); | |
Injector::inst()->get('Logger')->pushHandler($handler); | |
// Logging to Chrome console (composer require --dev ccampbell/chromephp 4.1.0) | |
$handler = new Monolog\Handler\ChromePHPHandler(); | |
$handler->setFormatter(new Monolog\Formatter\ChromePHPFormatter()); | |
Injector::inst()->get('Logger')->pushHandler($handler); | |
/** | |
* Log wrapping class for debugging with line and file references and optional stack trace e.g: | |
* Injector::inst()->get('Logger')->debug(new LogWrapper('something here')); | |
* Injector::inst()->get('Logger')->debug(new LogWrapper('something here', true)); | |
*/ | |
class LogWrapper extends Exception { | |
private $showTrace; | |
public function __construct($message = null, $showTrace = false, $code = 0, Exception $previous = null) | |
{ | |
$this->showTrace = $showTrace; | |
if (!is_scalar($message)) { | |
$message = print_r($message, true); | |
} | |
return parent::__construct($message, $code, $previous); | |
} | |
/** | |
* Create a string with file, line and optionally stack trace. | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
$str = "{$this->file} {$this->line}: {$this->message} \n"; | |
if ($this->showTrace) { | |
$str .= $this->getTraceAsString() . "\n"; | |
} | |
return $str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment