Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active March 4, 2016 16:31
Show Gist options
  • Save dhaupin/b9e890d3c3da417d2d15 to your computer and use it in GitHub Desktop.
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)
<?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;
}
<?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