Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DebugLog
- {
- const DEBUG_FILE = '/tmp/debug.log';
- protected static $_instance;
- protected $_handle = NULL;
- protected function __construct() {
- $this->_handle = fopen($this::DEBUG_FILE, 'a+');
- if ($this->_handle === FALSE) {
- throw new Exception('Failed to open debug log!');
- }
- }
- public function __destruct() {
- //fclose($this->_handle);
- }
- public static function getInstance() {
- if(!self::$_instance instanceof self) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- public function log($msg, $tag='')
- {
- // retrieve the backtrace and set the file & line
- $backtrace = debug_backtrace();
- $formattedBacktrace = '';
- foreach ($backtrace as $i=>$entry) {
- $formattedBacktrace.= sprintf('#%-2d | %-4s | %-30s | %s'. PHP_EOL, $i, $entry['line'], $entry['function'], $entry['file']);
- }
- // get the currently formatted time
- $time = date('Ymd H:i:s');
- // format the tags
- if (!empty($tag)) {
- if (is_array($tag)) {
- $tag = implode(',', $tag);
- }
- }
- // format the start line
- $preamble = sprintf('-------------- %s tags(%s) --------------', $time, $tag);
- // format the message
- if (is_array($msg) || is_object($msg)) {
- $msg = print_r($msg, TRUE);
- }
- $hr = '----------';
- // log the message
- if (fwrite($this->_handle, $preamble. PHP_EOL. $msg. PHP_EOL. $hr. PHP_EOL. $formattedBacktrace) === FALSE) {
- throw new Exception('Failed to write to debug log.');
- }
- return $this;
- }
- }
Add Comment
Please, Sign In to add comment