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/

相关文章

thinkphp如何获取客户端IP

thinkphp框架中系统内置了get_client_ip方法用于获取客户端的IP地址,使用示例: $ip = get_client_ip(); 除了thinkphp内置get_cl...

JS操作XML中DTD介绍及使用方法分析

JS操作XML中DTD介绍及使用方法分析

本文实例讲述了JS操作XML中DTD介绍及使用方法。分享给大家供大家参考,具体如下: 什么是DTD,为什么需要DTD? DTD为英文Document Type Definitio...

PHP文章按日期(月日)SQL归档语句

复制代码 代码如下: select FROM_UNIXTIME(pubtime, '%Y-%m') as pubtime, count(*) as cnt from articles g...

关于JSON以及JSON在PHP中的应用技巧

JSON 基础 简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户...

探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法

【造成原因】:Because PHP's integer type is signed, and many IP addresses will result in negative in...