PHP中设置时区,记录日志文件的实现代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<html>
<body>
<?php
date_default_timezone_set('Asia/Hong_Kong');  //set time zone
set_error_handler("myHandler");               //set error handler
$chinatime = date('Y-m-d H:i:s');             //get current time
$max_size = 500000;
try
{
    $content = "Hello WeiXin!";
    logger2($content);
    //throw new Exception("Value must be 1 or below aaaaaaaaaaaaaaaaaaa");
}
catch(Exception $e)
{
    logger2("Exception Message: ".$e->getMessage());
}
//record operation log into .log file
function logger($log_content)
{
    print_r(date('H:i:s')." ".$log_content."<br />");
    $log_filename = date("Ymd").".log";
    $file = fopen($log_filename ,"a+");
    fwrite($file, date('H:i:s')." ".$log_content."\r\n");
    fclose($file);
}
//record operation log into .log file
function logger2($log_content)
{
    Global $max_size;  
    print_r(date('H:i:s')." ".$log_content." "."<br />");
    $log_filename = date("Ymd").".log";
    if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);sleep(1);}
    file_put_contents($log_filename, date('H:i:s')." ".$log_content." "."\r\n", FILE_APPEND);
}
//error handler function
function myHandler($level, $message, $file, $line, $context)
{
    logger("<b>[ERROR]</b> LEVEL: $level, MESSAGE: $message, FILE: $file, LINE: $line, CONTENT: $context");
    die();
}
?>
</body>
</html>

原文网址:http://txw1958.cnblogs.com/

相关文章

php使用curl实现ftp文件下载功能

本文实例为大家分享了php实现ftp文件下载功能,供大家参考,具体内容如下 不知道为什么用正常的ftp_get函数下载文件速度特别慢,但是用ftp的客户端下载很快,所以换了curl的下载...

使用array mutisort 实现按某字段对数据排序

array_multisort 的用法 一、先看最简单的情况。有两个数组:$arr1 = array(1,9,5);$arr2 = array(6,2,4);array_multisor...

php字符串比较函数用法小结(strcmp,strcasecmp,strnatcmp及strnatcasecmp)

本文实例分析了php字符串比较函数用法。分享给大家供大家参考,具体如下: 直接比较字符串是否完全一致,可以使用"=="来进行,但是有时候可能需要进行更加复杂的字符串比较,如部分匹配等....

PHP字符串word末字符实现大小写互换的方法

本文实例讲述了PHP字符串word末字符实现大小写互换的方法。分享给大家供大家参考。具体实现方法如下: 一、要求: 给出一个字符串如 “A journey of, a thousand...

php判断对象是派生自哪个类的方法

本文实例讲述了php判断对象是派生自哪个类的方法。分享给大家供大家参考。具体实现方法如下: <?php $th = new Thread; //创建新对象 if (...