File.class.php
2.06 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
<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Log\Driver;
class File
{
protected $config = array(
'log_time_format' => ' c ',
'log_file_size' => 2097152,
'log_path' => ''
);
// 实例化并传入参数
public function __construct($config = array())
{
$this->config = array_merge($this->config, $config);
}
/**
* 日志写入接口
*
* @access public
* @param string $log 日志信息
* @param string $destination 写入目标
* @return void
*/
public function write($log, $destination = '')
{
$now = rgmdate(time(), $this->config['log_time_format']);
if (empty($destination)) {
$destination = $this->config['log_path'] . rgmdate(time(), 'y_m_d') . '.log';
}
// 自动创建日志目录
$log_dir = dirname($destination);
if (! is_dir($log_dir)) {
mkdir($log_dir, 0755, true);
}
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
if (is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination)) {
rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
}
// 清理旧文件
clearLogs($log_dir);
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '--';
error_log("[{$now}] " . $_SERVER['REMOTE_ADDR'] . ' ' . BOARD_URL . ' ' . $referer . "\r\n{$log}\r\n", 3, $destination);
}
}