php打印日志方法

php

浏览数:22

2019-1-7


addlog.php

<?php
//打印日志函数

function AddLog($log='',$filePrefix='',$fileSuffix='.log',$time='day'){
        $time1=date('Y-m-d H:i:s',time());
        if($time=='year'){
            $period=date('Y',time());
        }elseif($time=='month'){
            $period=date('Ym',time());
        }elseif($time=='hour'){
            $period=date('YmdH',time());
        }elseif($time=='minute'){
            $period=date('YmdHi',time());
        }elseif($time=='second'){
            $period=date('YmdHis',time());
        }else{
            $period=date('Ymd',time());
        }
        $filename=$filePrefix.$period.$fileSuffix;
        $fp=fopen($filename,'a');
        if($fp){
            $wr=fwrite($fp,$time1."\n".$log."\n");
            if($wr){
                $close=fclose($fp);
                if($close){
                    return 1;
                }else{
                    return -1;
                }
            }else{
                return -2;
            }
        }else{
            return -3;
        }
    } 
/*
调用方法: 
$a = array(); //要打印的数组
AddLog($a,"新增文件名称");

*/

?>

相关推荐