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/

相关文章

浅析PHP水印技术

浅析PHP水印技术

一直以来对PHP对象处理不是很熟悉,以前都是在用的时候才找手册的。今天有人在phpchina论坛上问到水印的相关问题,恰好我也想学习下,于是研究了下PHP实现图片水印的实现。 ...

php中的实现trim函数代码

去掉前后的空格.  假设有一个字符串" ddd dd d ",经过Trim()之后成为"ddd dd d".&nbs...

php实现网页缓存的工具类分享

php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一、文件缓存 二、数据查询结果缓存,使用内存来实现高速缓存 本例主要使用文件缓存。...

PHP页面实现定时跳转的方法

本文实例讲述了PHP页面实现定时跳转的方法,分享给大家供大家参考。具体实现方法如下: php定时跳转我们需要利用header函数输入html或js代码来实现定时跳转,下面我来介绍一个简单...

PHPWind与Discuz截取字符函数substrs与cutstr性能比较

PHPWind与Discuz截取字符函数substrs与cutstr性能比较

以掌握使用Benchmark_Iterate类工具的方法。 目录 一,什么是PEAR与Benchmark 二,性能比较代码准备与调试 三,性能比较测试结果 四,性能比较测试总结 五,附性...