summaryrefslogtreecommitdiff
path: root/Postman/PostmanLogger.php
blob: 5454c606cc3aa09cf1044dd9e68fb94c0443eb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if (! class_exists ( "PostmanLogger" )) {
	
	//
	class PostmanLogger {
		const ALL_INT = - 2147483648;
		const TRACE_INT = 5000;
		const DEBUG_INT = 10000;
		const ERROR_INT = 40000;
		const FATAL_INT = 50000;
		const INFO_INT = 20000;
		const OFF_INT = 2147483647;
		const WARN_INT = 30000;
		private $name;
		private $logLevel;
		private $wpDebug;
		function __construct($name) {
			$this->name = $name;
			$this->wpDebug = defined ( 'WP_DEBUG' );
			if (class_exists ( 'PostmanOptions' )) {
				$this->logLevel = PostmanOptions::getInstance ()->getLogLevel ();
			} else {
				$this->logLevel = self::OFF_INT;
			}
		}
		function trace($text) {
			$this->printLog ( $text, self::TRACE_INT, 'TRACE' );
		}
		function debug($text) {
			$this->printLog ( $text, self::DEBUG_INT, 'DEBUG' );
		}
		function info($text) {
			$this->printLog ( $text, self::INFO_INT, 'INFO' );
		}
		function warn($text) {
			$this->printLog ( $text, self::WARN_INT, 'WARN' );
		}
		function error($text) {
			$this->printLog ( $text, self::ERROR_INT, 'ERROR' );
		}
		function fatal($text) {
			$this->printLog ( $text, self::FATAL_INT, 'FATAL' );
		}
		/**
		 * better logging thanks to http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
		 *
		 * @param mixed $intLogLevel        	
		 * @param mixed $logLevelName        	
		 */
		private function printLog($text, $intLogLevel, $logLevelName) {
			if ($this->wpDebug && $intLogLevel >= $this->logLevel) {
				if (is_array ( $text ) || is_object ( $text )) {
					error_log ( $logLevelName . ' ' . $this->name . ': ' . print_r ( $text, true ) );
				} else {
					error_log ( $logLevelName . ' ' . $this->name . ': ' . $text );
				}
			}
		}
		public function isDebug() {
			return self::DEBUG_INT >= $this->logLevel;
		}
		public function isTrace() {
			return self::TRACE_INT >= $this->logLevel;
		}
		public function isInfo() {
			return self::INFO_INT >= $this->logLevel;
		}
	}
}