Last active
March 4, 2016 16:31
-
-
Save dhaupin/b9e890d3c3da417d2d15 to your computer and use it in GitHub Desktop.
Function - Send debug logs to browser console and/or system log (var/log/messages)
This file contains hidden or 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 | |
// This goes in your log class, for this example we will access it in the view as $this->log->consoled | |
// These call a native platform log method, for this example we will call it $this->write() | |
public function syslog($message, $errlog = false) { | |
syslog(LOG_NOTICE, 'debug: (' . $_SERVER['SERVER_NAME'] . '@' . $_SERVER['SERVER_ADDR'] . ') [INFO] ' . $this->clean($message)); | |
if ($errlog) { | |
$this->write('@System | ' . $message); | |
} | |
} | |
public function console($message, $errlog = false) { | |
$this->console[] .= $message; | |
if ($errlog) { | |
$this->write('@Console | ' . $message); | |
} | |
} | |
public function consoled() { | |
return $this->clean($this->console); | |
} | |
private function clean($message) { | |
$base_pool = array( | |
'\'' => '\\\'', | |
'"' => '\\\"', | |
"\r" => ' ', | |
"\n" => ' ', | |
"\t" => ' ' | |
); | |
foreach ($base_pool as $key => $value) { | |
$message = str_replace($key, $value, $message); // iterate through pool key => values and replace in $message | |
} | |
return $message; | |
} |
This file contains hidden or 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 // This is the output into a view, building out a console log script ?> | |
<?php if ($this->log->consoled()) { ?> | |
<script type="text/javascript"> | |
var cond_css_head = 'font-weight: bold; font-size: 1.2em; letter-spacing: -1; color: #658500;', | |
cond_css_row = 'font-style: italic; color: #696969;'; | |
console.log('%c<consoled>', cond_css_head); | |
console.log('%c // output from $this->log->consoled():', cond_css_row); | |
<?php foreach($this->log->consoled() as $message) { ?> | |
console.log('%c <?php echo $message; ?>', cond_css_row); | |
<?php } ?> | |
console.log('%c</consoled>', cond_css_head); | |
</script> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment