log.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace com\unionpay\acp\sdk;
  3. class PhpLog
  4. {
  5. const DEBUG = 1;// Most Verbose
  6. const INFO = 2;// ...
  7. const WARN = 3;// ...
  8. const ERROR = 4;// ...
  9. const FATAL = 5;// Least Verbose
  10. const OFF = 6;// Nothing at all.
  11. const LOG_OPEN = 1;
  12. const OPEN_FAILED = 2;
  13. const LOG_CLOSED = 3;
  14. /* Public members: Not so much of an example of encapsulation, but that's okay. */
  15. public $Log_Status = PhpLog::LOG_CLOSED;
  16. public $DateFormat= "Y-m-d G:i:s";
  17. public $MessageQueue;
  18. private $filename;
  19. private $log_file;
  20. private $priority = PhpLog::INFO;
  21. private $file_handle;
  22. /**
  23. * AUTHOR: gu_yongkang
  24. * DATA: 20110322
  25. * Enter description here ...
  26. * @param $filepath
  27. * 文件存储的路径
  28. * @param $timezone
  29. * 时间格式,此处设置为"PRC"(中国)
  30. * @param $priority
  31. * 设置运行级别
  32. */
  33. public function __construct( $filepath, $timezone, $priority )
  34. {
  35. if ( $priority == PhpLog::OFF ) return;
  36. $this->filename = date('Y-m-d', time()) . '.log'; //默认为以时间+.log的文件文件
  37. $this->log_file = $this->createPath($filepath, $this->filename);
  38. $this->MessageQueue = array();
  39. $this->priority = $priority;
  40. date_default_timezone_set($timezone);
  41. if ( !file_exists($filepath) ) //判断文件路径是否存在
  42. {
  43. if(!empty($filepath)) //判断路径是否为空
  44. {
  45. if(!($this->_createDir($filepath)))
  46. {
  47. $this->Log_Status = PhpLog::OPEN_FAILED;
  48. $this->MessageQueue[] = "Create file failed.";
  49. return;
  50. }
  51. if ( !is_writable($this->log_file) )
  52. {
  53. $this->Log_Status = PhpLog::OPEN_FAILED;
  54. $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
  55. return;
  56. }
  57. }
  58. }
  59. if ( $this->file_handle = fopen( $this->log_file , "a+" ) )
  60. {
  61. $this->Log_Status = PhpLog::LOG_OPEN;
  62. $this->MessageQueue[] = "The log file was opened successfully.";
  63. }
  64. else
  65. {
  66. $this->Log_Status = PhpLog::OPEN_FAILED;
  67. $this->MessageQueue[] = "The file could not be opened. Check permissions.";
  68. }
  69. return;
  70. }
  71. public function __destruct()
  72. {
  73. if ( $this->file_handle )
  74. fclose( $this->file_handle );
  75. }
  76. /**
  77. *作用:创建目录
  78. *输入:要创建的目录
  79. *输出:true | false
  80. */
  81. private function _createDir($dir)
  82. {
  83. // return is_dir($dir) or (self::_createDir(dirname($dir)) and mkdir($dir, 0777));
  84. return false;
  85. }
  86. /**
  87. *作用:构建路径
  88. *输入:文件的路径,要写入的文件名
  89. *输出:构建好的路径字串
  90. */
  91. private function createPath($dir, $filename)
  92. {
  93. if (empty($dir))
  94. {
  95. return $filename;
  96. }
  97. else
  98. {
  99. return $dir . "/" . $filename;
  100. }
  101. }
  102. public function LogInfo($line)
  103. {
  104. /**
  105. * AUTHOR : gu_yongkang
  106. * 增加打印函数和文件名的功能
  107. */
  108. $sAarray = array();
  109. $sAarray = debug_backtrace();
  110. $sGetFilePath = $sAarray[0]["file"];
  111. $sGetFileLine = $sAarray[0]["line"];
  112. $this->Log( $line, PhpLog::INFO, $sGetFilePath, $sGetFileLine);
  113. unset($sAarray);
  114. unset($sGetFilePath);
  115. unset($sGetFileLine);
  116. }
  117. public function LogDebug($line)
  118. {
  119. /**
  120. * AUTHOR : gu_yongkang
  121. * 增加打印函数和文件名的功能
  122. */
  123. $sAarray = array();
  124. $sAarray = debug_backtrace();
  125. $sGetFilePath = $sAarray[0]["file"];
  126. $sGetFileLine = $sAarray[0]["line"];
  127. $this->Log( $line, PhpLog::DEBUG, $sGetFilePath, $sGetFileLine);
  128. unset($sAarray);
  129. unset($sGetFilePath);
  130. unset($sGetFileLine);
  131. }
  132. public function LogWarn($line)
  133. {
  134. /**
  135. * AUTHOR : gu_yongkang
  136. * 增加打印函数和文件名的功能
  137. */
  138. $sAarray = array();
  139. $sAarray = debug_backtrace();
  140. $sGetFilePath = $sAarray[0]["file"];
  141. $sGetFileLine = $sAarray[0]["line"];
  142. $this->Log( $line, PhpLog::WARN, $sGetFilePath, $sGetFileLine);
  143. unset($sAarray);
  144. unset($sGetFilePath);
  145. unset($sGetFileLine);
  146. }
  147. public function LogError($line)
  148. {
  149. /**
  150. * AUTHOR : gu_yongkang
  151. * 增加打印函数和文件名的功能
  152. */
  153. $sAarray = array();
  154. $sAarray = debug_backtrace();
  155. $sGetFilePath = $sAarray[0]["file"];
  156. $sGetFileLine = $sAarray[0]["line"];
  157. $this->Log( $line, PhpLog::ERROR, $sGetFilePath, $sGetFileLine);
  158. unset($sAarray);
  159. unset($sGetFilePath);
  160. unset($sGetFileLine);
  161. }
  162. public function LogFatal($line)
  163. {
  164. /**
  165. * AUTHOR : gu_yongkang
  166. * 增加打印函数和文件名的功能
  167. */
  168. $sAarray = array();
  169. $sAarray = debug_backtrace();
  170. $sGetFilePath = $sAarray[0]["file"];
  171. $sGetFileLine = $sAarray[0]["line"];
  172. $this->Log( $line, PhpLog::FATAL, $sGetFilePath, $sGetFileLine);
  173. unset($sAarray);
  174. unset($sGetFilePath);
  175. unset($sGetFileLine);
  176. }
  177. /**
  178. * Author : gu_yongkang
  179. * Enter description here ...
  180. * @param unknown_type $line
  181. * content 内容
  182. * @param unknown_type $priority
  183. * 打印级别
  184. * @param unknown_type $sFile
  185. * 调用打印日志的文件名
  186. * @param unknown_type $iLine
  187. * 打印文件的位置(行数)
  188. */
  189. public function Log($line, $priority, $sFile, $iLine)
  190. {
  191. if ($iLine > 0)
  192. {
  193. //$line = iconv('GBK', 'UTF-8', $line);
  194. if ( $this->priority <= $priority )
  195. {
  196. $status = $this->getTimeLine( $priority, $sFile, $iLine);
  197. $this->WriteFreeFormLine ( "$status $line \n" );
  198. }
  199. }
  200. else
  201. {
  202. /**
  203. * AUTHOR : gu_yongkang
  204. * 增加打印函数和文件名的功能
  205. */
  206. $sAarray = array();
  207. $sAarray = debug_backtrace();
  208. $sGetFilePath = $sAarray[0]["file"];
  209. $sGetFileLine = $sAarray[0]["line"];
  210. if ( $this->priority <= $priority )
  211. {
  212. $status = $this->getTimeLine( $priority, $sGetFilePath, $sGetFileLine);
  213. unset($sAarray);
  214. unset($sGetFilePath);
  215. unset($sGetFileLine);
  216. $this->WriteFreeFormLine ( "$status $line \n" );
  217. }
  218. }
  219. }
  220. // 支持输入多个参数
  221. public function WriteFreeFormLine( $line )
  222. {
  223. if ( $this->Log_Status == PhpLog::LOG_OPEN && $this->priority != PhpLog::OFF )
  224. {
  225. if (fwrite( $this->file_handle , $line ) === false)
  226. {
  227. $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
  228. }
  229. }
  230. }
  231. private function getRemoteIP()
  232. {
  233. foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
  234. {
  235. if (array_key_exists($key, $_SERVER) === true)
  236. {
  237. foreach (explode(',', $_SERVER[$key]) as $ip)
  238. {
  239. $ip = trim($ip);
  240. if (!empty($ip))
  241. {
  242. return $ip;
  243. }
  244. }
  245. }
  246. }
  247. return "_NO_IP";
  248. }
  249. private function getTimeLine( $level, $FilePath, $FileLine)
  250. {
  251. $time = date( $this->DateFormat );
  252. $ip = $this->getRemoteIP();
  253. switch( $level )
  254. {
  255. case PhpLog::INFO:
  256. return "$time, " . "INFO, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  257. case PhpLog::WARN:
  258. return "$time, " . "WARN, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  259. case PhpLog::DEBUG:
  260. return "$time, " . "DEBUG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  261. case PhpLog::ERROR:
  262. return "$time, " . "ERROR, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  263. case PhpLog::FATAL:
  264. return "$time, " . "FATAL, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  265. default:
  266. return "$time, " . "LOG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  267. }
  268. }
  269. }