一个PHP写日志的接口
Loginterface.php文件
1 <?php 2 class Others_Loginterface extends Others_Log 3 { 4 /** 5 *EMERG = 0; // Emergency: 系统不可用 6 ALERT = 1; // Alert: 报警 7 CRIT = 2; // Critical: 紧要 8 ERR = 3; // Error: 错误 9 WARN = 4; // Warning: 警告 10 NOTICE = 5; // Notice: 通知 11 INFO = 6; // Informational: 一般信息 12 DEBUG = 7; // Debug: 小时消息 13 * 14 * 15 * @param unknown_type $file 16 * @param unknown_type $msg 17 */ 18 protected static $msg=array(); 19 protected static $body=array(); 20 protected static $file=""; 21 protected static $charset=""; 22 23 /** 24 *开始记录日志* 25 * 26 */ 27 public function start($fname="",$charset="") 28 { 29 self::$msg=array(); 30 self::$body=array(); 31 //self::$file=$_SERVER["Root_STATIC"]."/apparel/interface/".date("Ym")."/".date("Ymd")."/".$fname.date("Ymd").".log"; 32 //self::createfile(self::$file); ///不用开创文件 33 self::$file=$fname.date("Ymd").".log"; 34 $tmp[]="\r\n时间:".date("Y-m-d H:i:s").'IP'.$_SERVER['Server_IP']; 35 $tmp[]="====================+++++++++++++++++++开始+++++++++++++++++++================\r\n"; 36 self::$msg[]=implode("\r\n",$tmp); 37 self::$charset=$charset; 38 } 39 40 public static function add($msg) 41 { 42 ///判断编码 只有utf-8写入,非utf-8进行转码 43 //if(is_string($msg) && strlen($msg)>0 && self::isGBK($msg)) $msg=iconv("GBK","UTF-8//IGNORE",$msg); 44 if(is_string($msg) && strlen($msg)>0 && self::isGBK($msg)) $msg = mb_convert_encoding($msg,"UTF-8","GBK"); 45 46 if(count(self::$body)==0) self::$body[]="\r\n其它输出:"; 47 self::$body[]= "\r\n".$msg; 48 } 49 50 public static function end() 51 { 52 self::$msg[]=implode("\r\n",self::$body); 53 self::$msg[]="====================+++++++++++++++++++结束+++++++++++++++++++================\r\n\r\n\r\n"; 54 /*以下进行编码转换*/ 55 $msg=implode("\r\n",self::$msg); 56 //var_dump(self::$file); 57 58 ///不是这几个接口文件,进行编码转换,因为这些是gb2312编码 。以后写日志,需要写入编码为:utf8就不会有乱码 59 /* 60 if(strlen($msg)>0 && !preg_match("/(sailsearch|sailpay|SUAN|HU|MU|CZ|HOTEL)/",self::$file)) ///gb2312 都需要写入这里 61 { 62 $msg=iconv("utf-8","gb2312//TRANSLIT",$msg); 63 } 64 */ 65 //if (is_string($msg) && strlen($msg)>0) $msg=iconv("UTF-8","GBK//IGNORE",$msg); ///所有写入编码转为gbk,因为通过前面操作,所有已经是utf-8 66 if (is_string($msg) && strlen($msg)>0) $msg = mb_convert_encoding($msg,"GBK","UTF-8"); 67 68 //self::write(self::$file,$msg,6); 69 Others_Log::write(self::$file,$msg); 70 } 71 72 /***本写入日志文件,目前支持utf8与gbk ***/ 73 protected static function isGBK($msg) 74 { 75 return is_string($msg)&&!mb_check_encoding($msg,'utf-8')?true:false; 76 } 77 78 public static function write($file,$data,$info=6) 79 { 80 $posturl = $_SERVER['APP_FILE_URL']; 81 $data=array('file'=>$file,'data'=>$data,'option'=>'append'); 82 Others_Httpclient::quickPost($posturl,$data); 83 } 84 85 86 87 }
Log.php文件
1 <?php 2 class Others_Log 3 { 4 /** 5 * @param unknown_type $file 6 * @param unknown_type $msg 7 */ 8 public static function write($file,$msg) 9 { 10 $logPath = "/data/web_files/static/apparel/interface/".date("Ym")."/".date("Ymd")."/"; 11 //if(!is_dir($logPath)) mkdir($logPath); 12 self::Directory($logPath); 13 $f = file_put_contents($logPath.$file, $msg,FILE_APPEND) 14 } 15 16 public static function Directory( $dir ){ 17 return is_dir ( $dir ) or self::Directory(dirname( $dir )) and mkdir ( $dir , 0777); 18 } 19 20 21 function createdir($path) 22 { 23 24 $arr=explode('/',dirname($path)); 25 $tmp = Data_Path; 26 foreach ($arr as $a) 27 { 28 $tmp = $tmp.'/'.$a; 29 if(!file_exists($tmp)) mkdir($tmp,0755); 30 } 31 return Data_Path."/".$path; 32 } 33 34 35 }
使用:
Loginterface::start( 'SetArrVal' );
Loginterface::add( '$params22222222222= ');
Loginterface::end();