mylog.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * 日志类文件
  4. *
  5. * 三种功能:
  6. * 1、普通日志,记录在logs/common.log中,filelog($log)
  7. * 2、调试日志,记录在logs/debug.log中,debug($log, $is_exit = false)
  8. * 3、自定义日志,写入文件地址需要作为参数传入,filelog($log, $filepath)
  9. *
  10. * @createtime 2018/03/01
  11. * @author 空竹
  12. * @copyright 芝麻开发 (http://www.zhimawork.com)
  13. */
  14. class Mylog {
  15. public $errcode = 0;
  16. public $errmsg = '';
  17. //当前时间
  18. private $datetime; //Y-m-d H:i:s
  19. //当前日志文件
  20. private $logfile = '';
  21. private $maxlogfilesize = 100; //最大的日志,单位MB
  22. //log文件存放位置
  23. private $file_common = '';
  24. private $file_debug = '';
  25. public function __construct(array $logconfig){
  26. $this->datetime = date('Y-m-d H:i:s');
  27. $this->file_common = $logconfig['common'];
  28. $this->file_debug = $logconfig['debug'];
  29. }
  30. //普通日志
  31. public function filelog($log, $filepath = '', $clear = false){
  32. if(empty($filepath))
  33. $this->logfile = $this->file_common;
  34. else
  35. $this->logfile = $filepath;
  36. if($this->checkfile()){
  37. $content = date('Y-m-d H:i:s').' ';
  38. $content .= $log."\r\n";
  39. $this->writefile($content, $clear);
  40. }
  41. }
  42. //调试日志
  43. public function debug($log, $is_exit = false){
  44. $this->logfile = $this->file_debug;
  45. if($this->checkfile()){
  46. $content = date('Y-m-d H:i:s').' ';
  47. $content .= $log."\r\n";
  48. $this->writefile($content);
  49. }
  50. if($is_exit) exit();
  51. }
  52. //清空日志2018/4/17
  53. public function clear($filepath){
  54. if($filepath == 'common')
  55. $this->logfile = $this->file_common;
  56. elseif($filepath == 'debug')
  57. $this->logfile = $this->file_debug;
  58. else
  59. $this->logfile = $filepath;
  60. self::writefile('', true);
  61. return action_msg('清空成功', 1);
  62. }
  63. //读取日志2018/4/17
  64. public function read($filepath){
  65. if($filepath == 'common')
  66. $this->logfile = $this->file_common;
  67. elseif($filepath == 'debug')
  68. $this->logfile = $this->file_debug;
  69. else
  70. $this->logfile = $filepath;
  71. $file = $this->logfile;
  72. if(file_exists($file)){
  73. $filesize = filesize($file);
  74. if($filesize > 2 * 1024 * 1024){
  75. $errmsg = '日志文件已超过限制(限2M,实际'.round($filesize/1024*1024).'M),请及时清理!';
  76. throw new myException($errmsg, 1101);
  77. }elseif($filesize == 0){
  78. $errmsg = '日志文件为空!';
  79. throw new myException($errmsg, 1102);
  80. }else{
  81. $fp = fopen($file, "r");
  82. $str = fread($fp, $filesize);
  83. $str = str_replace("\r\n", "<br />", $str);
  84. return action_msg($str, 1);
  85. }
  86. }else{
  87. $errmsg = '日志文件读取失败,请检查文件是否存在或路径是否正确。';
  88. throw new myException($errmsg, 1103);
  89. }
  90. }
  91. //检查文件
  92. private function checkfile(){
  93. $file = $this->logfile;
  94. if($this->isExistFile()){
  95. //如果日志文件大小超过预期,就把当前的日志文件重命名,再新建一个日志文件
  96. $filesize = $this->getFileSize();
  97. $filepathinfo = $this->getFilePathInfo();
  98. if($filesize > $this->maxlogfilesize * 1024 * 1024){
  99. $dir = $filepathinfo['dirname'];
  100. $name = $filepathinfo['filename'];
  101. $newname = $dir.'/'.$name.time().'.log';
  102. rename($file, $newname);
  103. $this->buildfile($file);
  104. }
  105. //检查文件是否可写
  106. if(!is_writeable($file)){
  107. $errmsg = '文件'.$file.'不可写';
  108. throw new myException($errmsg, 901);
  109. }
  110. }else{
  111. $errmsg = '文件'.$file.'不存在,并且尝试创建该文件失败';
  112. throw new myException($errmsg, 902);
  113. }
  114. return true;
  115. }
  116. //检查文件是否存在,不存在则尝试新建
  117. private function isExistFile(){
  118. $file = $this->logfile;
  119. if(!is_file($file)) {
  120. if($this->buildfile()) return true;
  121. return false;
  122. }
  123. return true;
  124. }
  125. //新建文件
  126. //@return 建立成功true 建立失败false
  127. private function buildfile($newfile = ''){
  128. if(empty($newfile)){
  129. $file = $this->logfile;
  130. }else{
  131. $file = $newfile;
  132. $this->logfile = $newfile;
  133. }
  134. if($this->checkDir()){
  135. if(touch($file))
  136. return true;
  137. else{
  138. return false;
  139. }
  140. }else{
  141. return false;
  142. }
  143. }
  144. //检查文件所在目录的存在与可写
  145. private function checkDir(){
  146. $file = $this->logfile;
  147. $dir = pathinfo($file, PATHINFO_DIRNAME);
  148. if($dir){
  149. if(!is_writeable($dir)){
  150. return false;
  151. }else{
  152. return true;
  153. }
  154. }else{
  155. return false;
  156. }
  157. }
  158. //得到文件大小
  159. private function getFileSize(){
  160. $file = $this->logfile;
  161. return filesize($file);
  162. }
  163. //得到文件信息
  164. private function getFilePathInfo(){
  165. $file = $this->logfile;
  166. return pathinfo($file);
  167. }
  168. //写入文件
  169. private function writefile($content, $clear = false){
  170. $file = $this->logfile;
  171. if($clear){
  172. $mode = 'w';
  173. }else{
  174. $mode = 'a';
  175. }
  176. $file_h = fopen($file, $mode);
  177. fwrite($file_h, $content);
  178. fclose($file_h);
  179. }
  180. }
  181. ?>