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

yipeiwu_com6年前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/

相关文章

ThinkPHP中实例Model方法的区别说明

在TP中,我们可以用下面两种方法去创建一个数据表的映射对象(我暂时用到的) 第一种:$Test = D('Test') 第二种:$Test = new Model('Test') 虽然这...

php中filter_input函数用法分析

本文实例分析了php中filter_input函数用法。分享给大家供大家参考。具体分析如下: 在 php5.2 中,内置了filter 模块,用于变量的验证和过滤,过滤变量等操作,这里我...

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

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

PHP学习笔记之三 数据库基本操作

下面是在Linux上登录mysql,创建数据库和创建表的过程。 yin@yin-Ubuntu10:~$ mysql -u root -p Enter password: Welcome...

Godaddy空间Zend Optimizer升级方法

但今天在安装测试程序的时候提示zend Optimizer not installed.这让我很迷茫,这么普遍的空间为什么没有安装zend了,然后我查看info.php,我惊奇的发现,服...