Cron.php
8 years ago
Ecommerce.php
7 years ago
Language.php
10 years ago
Logger.php
8 years ago
MobileDetect.php
8 years ago
Options.php
8 years ago
Plugin.php
10 years ago
Properties.php
10 years ago
Tracking.php
8 years ago
Utils.php
8 years ago
Logger.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 | |
| 6 | class TCMP_Logger { |
| 7 | private $name; |
| 8 | private $context=array(); |
| 9 | |
| 10 | public function __construct($name='TCMP') { |
| 11 | if($name=='') $name='TCMP'; |
| 12 | $this->name=$name; |
| 13 | } |
| 14 | |
| 15 | public function pushContext($context) { |
| 16 | array_push($this->context, $context); |
| 17 | } |
| 18 | public function popContext() { |
| 19 | array_pop($this->context); |
| 20 | } |
| 21 | |
| 22 | public function fatal($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL, $v6=NULL) { |
| 23 | $what=$this->write('[FATAL]', $message, $v1, $v2, $v3, $v4, $v5, $v6); |
| 24 | die($what); |
| 25 | } |
| 26 | public function debug($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL, $v6=NULL) { |
| 27 | $this->write('[DEBUG]', $message, $v1, $v2, $v3, $v4, $v5, $v6); |
| 28 | } |
| 29 | public function info($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL, $v6=NULL) { |
| 30 | $this->write('[INFO] ', $message, $v1, $v2, $v3, $v4, $v5, $v6); |
| 31 | } |
| 32 | public function error($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL, $v6=NULL) { |
| 33 | $this->write('[ERROR]', $message, $v1, $v2, $v3, $v4, $v5, $v6); |
| 34 | } |
| 35 | private function dump($v) { |
| 36 | if(is_array($v) && count($v)==0) { |
| 37 | $v='[]'; |
| 38 | } |
| 39 | if($v!=NULL) { |
| 40 | if(is_array($v) || is_object($v)) { |
| 41 | $v=print_r($v, TRUE); |
| 42 | } |
| 43 | } |
| 44 | if(is_bool($v)) { |
| 45 | $v=($v ? 'TRUE' : 'FALSE'); |
| 46 | } |
| 47 | return $v; |
| 48 | } |
| 49 | private function write($verbosity, $message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL, $v6=NULL) { |
| 50 | global $tcmp; |
| 51 | |
| 52 | $text=sprintf($message |
| 53 | , $this->dump($v1) |
| 54 | , $this->dump($v2) |
| 55 | , $this->dump($v3) |
| 56 | , $this->dump($v4) |
| 57 | , $this->dump($v5) |
| 58 | , $this->dump($v6)); |
| 59 | $message=date("d/m/Y H:i:s")." ".$verbosity." "; |
| 60 | if(count($this->context)>0) { |
| 61 | $message.='{'.$this->context[count($this->context)-1].'} '; |
| 62 | } |
| 63 | $message="\n".$message.$text; |
| 64 | if(!$tcmp->Options->isLoggerEnable()) { |
| 65 | return $message; |
| 66 | } |
| 67 | |
| 68 | $hasErrors=false; |
| 69 | $filename=TCMP_PLUGIN_DIR."logs/".$this->name."_".date("Ym").".txt"; |
| 70 | if (!$handle = fopen($filename, 'a')) { |
| 71 | $hasErrors=true; |
| 72 | } |
| 73 | |
| 74 | if(!$hasErrors && fwrite($handle, $message)===FALSE) { |
| 75 | $hasErrors=true; |
| 76 | } |
| 77 | |
| 78 | if(!$hasErrors) { |
| 79 | fclose($handle); |
| 80 | } |
| 81 | return $message; |
| 82 | } |
| 83 | } |