-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggerExtCommandBehavior.php
98 lines (84 loc) · 3.39 KB
/
LoggerExtCommandBehavior.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
<?php
class LoggerExtCommandBehavior extends CConsoleCommandBehavior
{
public function beforeAction($event)
{
$actionId = strtolower($event->action);
$commandId = strtolower($event->sender->getName());
$logCommands = Yii::app()->logext->commands;
$logCommand = array();
if (isset($logCommands[$commandId]) ) {
$logCommand = $logCommands[$commandId];
} elseif (isset($logCommands['*'])) {
$logCommand = $logCommands['*'];
}
$logFile = "console.log";
if ( isset($logCommand['logFilePattern']) ) {
$pattern = $logCommand['logFilePattern'];
$logFile = $pattern;
if (
isset($logCommand['logFileUseParam']) &&
$logCommand['logFileUseParam'] &&
isset($logCommand['params']) &&
isset($logCommand['paramPattern']) &&
isset($logCommand['joinCharacter'])
) {
$params = explode(',', $logCommand['params']);
$joinCharacter = $logCommand['joinCharacter'];
$paramPattern = $logCommand['paramPattern'];
$pp = array();
$methodName = 'action'.$actionId;
$method=new ReflectionMethod($event->sender,$methodName);
$paramsName = array();
$paramsNow = array();
foreach($method->getParameters() as $i=>$param)
{
$name=$param->getName();
array_push($paramsName, $name)
}
for($i=0; $i<count($paramsName); $i++) {
$paramsNow[$paramsName[$i]] = $event->params[$i];
}
foreach($params as $key ) {
if (isset($paramsNow[trim($key)])) {
$v = $paramsNow[trim($key)];
} else {
$v = null;
}
if (!is_null($v)) {
$pp[] = str_replace(array('%n', '%v'), array(trim($key), $v), $paramPattern);
}
}
$logFile = str_replace('%p', join($joinCharacter, $pp), $pattern);
}
$find = array('%c', '%a');
$replace = array($commandId, $actionId);
$logFile = str_replace($find, $replace, $logFile);
}
$logPath = "";
if ( isset($logCommand['logPathPattern'])) {
$logPath = $logCommand['logPathPattern'];
$find = array('%c', '%a');
$replace = array($commandId, $actionId);
$logPath = str_replace($find, $replace, $logPath);
$find = array('<', '>', '*', '?', '"', '|');
$logPath = trim(str_replace($find, '', $logPath));
if (PHP_OS == 'WINNT') {
$find = '/';
} else {
$find = '\\';
}
$logPath = trim(str_replace($find, '', $logPath));
}
$route = Yii::app()->logext->route;
if ($route['class'] == 'FileDailyLogRoute') {
$route['logFile'] = $logFile;
$route=Yii::createComponent($route);
$route->init();
if ($logPath!="") {
$route->logPath = $route->logPath.'/'.$logPath;
}
Yii::app()->log->setRoutes(array('controller'=>$route));
}
}
}