logger_class.php
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 日志处理类
*
* @author Tiny
* @class Logger
*/
class Logger
{
const TRACE='trace';
const WARNING='warning';
const ERROR='error';
const INFO='info';
private $logs=null;
/**
* 写入日志信息
*
* @access public
* @param mixed $message
* @param string $level
* @return mixed
*/
public function log($message,$level='info')
{
$this->logs[] = array($message,$level);
}
/**
* 得到运行时间
*
* @access public
* @return mixed
*/
public function getExecutionTime()
{
return microtime(true)-BEGIN_TIME;
}
/**
* 取得内存使用信息
*
* @access public
* @return mixed
*/
public function getMemoryUsage()
{
if(function_exists('memory_get_usage'))
return memory_get_usage();
else
{
$output=array();
if(strncmp(PHP_OS,'WIN',3)===0)
{
exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST',$output);
return isset($output[5])?preg_replace('/[\D]/','',$output[5])*1024 : 0;
}
else
{
$pid=getmypid();
exec("ps -eo%mem,rss,pid | grep $pid", $output);
$output=explode(" ",$output[0]);
return isset($output[1]) ? $output[1]*1024 : 0;
}
}
}
/**
* 日志类在销毁时统计进行日志处理
*
* @access public
* @return mixed
*/
public function __destruct()
{
if(LOG && $this->logs!==null)
{
$log_file_name = APP_ROOT.'logs'.DIRECTORY_SEPARATOR.date('Y-m-d').'_log.txt';
if (is_file($log_file_name) && filesize($log_file_name) >= 2097152)
{
rename($log_file_name, APP_ROOT . 'logs'.DIRECTORY_SEPARATOR.date('Y-m-d_His').'_log.txt');
}
$logFile = new File($log_file_name,'a+');
foreach($this->logs as $log)
{
$logFile->write(date('Y-m-d H:i:s')."\t[".strtolower($log[1])."]\t".$log[0]."[time used: ".sprintf('%0.5f',$this->getExecutionTime())."s] [memory used:".number_format($this->getMemoryUsage()/1024)."kb]\r\n");
}
}
}
}