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.ini不生效问题解决方法(上传大于8M的文件)

摘要:上传大于8M的文件需要修改php的配置才可以生效。但是我在网上找了一堆修改配置的资料,但是自己修改之后就是没有生效。 解决方法: 修改php.ini这个选项,网上有很多的教程,可以...

simplehtmldom Doc api帮助文档

API Reference Helper functions object str_get_html ( string $content ) Creates a DOM object f...

探讨方法的重写(覆载)详解

复制代码 代码如下:<?php class Cart{  public function Cart(){   echo...

PHP实现更新中间关联表数据的两种方法

本文以示例形式展示了PHP实现更新中间关联表数据的两种方法。分享给大家供大家参考之用。具体方法如下: 首先,中间关联表:这里的中间表只存表1的主键和表2的主键,即多对多的形式。 执行数据...

php 进阶:实现无限分类第1/4页

1.分析  我们在用 php 制作网站时,分类是很重要的,在分类下面又再分类这第二个分类称为次分类,而现在大多数网站分类只分到第三类:  第一分类...