-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileDailyLogRoute.php
158 lines (133 loc) · 4.28 KB
/
FileDailyLogRoute.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
class FileDailyLogRoute extends CLogRoute
{
/**
* The 'datePattern' parameter.
* Determines how date will be formatted in file name.
* @var string
*/
private $_dayPattern="Ymd";
/**
* @var string directory storing log files
*/
private $_logPath;
/**
* @var string log file name
*/
private $_logFile='application.log';
/**
* @var int Keep Days
*/
private $_keepDays = 7;
public $newDirMode=0777;
public $newFileMode=0666;
/**
* Initializes the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
parent::init();
if($this->getLogPath()===null)
$this->setLogPath(Yii::app()->getRuntimePath());
}
/**
* @return string directory storing log files. Defaults to application runtime path.
*/
public function getLogPath()
{
return $this->_logPath;
}
/**
* @param string $value directory for storing log files.
* @throws CException if the path is invalid
*/
public function setLogPath($value)
{
@mkdir($value,$this->newDirMode,true);
@chmod($value,$this->newDirMode);
$this->_logPath=realpath($value);
if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
throw new CException(Yii::t('yii','CFileLogRoute.logPath "{path}" does not point to a valid directory. Make sure the directory exists and is writable by the Web server process.',
array('{path}'=>$value)));
}
/**
* @return string log file name. Defaults to 'application.log'.
*/
public function getLogFile()
{
return $this->_logFile;
}
/**
* @param string $value log file name
*/
public function setLogFile($value)
{
$this->_logFile=$this->toFileName($value);
}
public function getKeepDays() {
return $this->_keepDays;
}
public function setKeepDays($value)
{
if(($this->_keepDays=(int)$value)<1)
$this->_keepDays=7;
}
public function getDatePattern() {
return $this->_dayPattern;
}
public function setDatePattern($value) {
if (''==trim($value)) $this->_dayPattern = 'Ymd';
else $this->_dayPattern = trim($value);
}
protected function resolveLogFile($date="") {
$logFile = $this->getLogFile();
$ext = pathinfo($logFile, PATHINFO_EXTENSION);
$main = pathinfo($logFile, PATHINFO_FILENAME);
if (''==$date) $date = $this->getDate();
$main .= "." . $date;
if (''!=$ext) $main .= '.'. $ext;
return $main;
}
protected function formatLogMessage($message,$level,$category,$time)
{
$pid = getmypid();
return @date('Y/m/d H:i:s',$time)." [$pid] [$level] [$category] $message\n";
}
/**
* Saves log messages in files.
* @param array $logs list of log messages
*/
protected function processLogs($logs)
{
$text='';
foreach($logs as $log)
$text.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
$logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->resolveLogFile();
//check file run keepDays
if (!@is_file($logFile)) {
$timestamp = strtotime($this->getDate());
$timestamp = $timestamp - ($this->getKeepDays() * 60 * 60 * 24);
$removeDateLogFile = $this->resolveLogfile(date($this->getDatePattern(), $timestamp));
$removeDateLogFile = $this->getLogPath().DIRECTORY_SEPARATOR.$removeDateLogFile;
@unlink($removeDateLogFile);
}
$fp=@fopen($logFile,'a');
@flock($fp,LOCK_EX);
@fwrite($fp,$text);
@flock($fp,LOCK_UN);
@fclose($fp);
}
protected function getDate() {
return date($this->getDatePattern(), time());
}
protected function toFileName($filename) {
$find = array('<', '>', '*', '?', '/', '\\', '"', '|');
$filename = str_replace($find, '', $filename);
$left_one = substr($filename, 0, 1);
if ($left_one == '.' && PHP_OS == 'WINNT') {
$filename = '_'.substr($filename, 1);
}
return $filename;
}
}