summaryrefslogtreecommitdiff
path: root/framework/Log/TLogRouter.php
blob: 9060e305f0ec4e4e492f43aee168e22429800e44 (plain)
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
<?php
/*
<module id="log" class="System.Log.LogRouter" ConfigFile="xxx">
	<route class="TFileLogRoute" SizeLimit="111" Levels="xxx" Categories="xxx" />
</module>
*/
class TLogRouter extends TModule
{
	const CONFIG_FILE_EXT='.xml';
	private $_routes=array();

	public function init($config)
	{
		foreach($config->getElementsByName('route') as $routeConfig)
		{
			$class=$routeConfig->removeAttribute('class');
			$route=Prado::createComponent($class);
			foreach($routeConfig->getAttributes() as $name=>$value)
				$route->setSubproperty($name,$value);
		}

		$this->getApplication()->attachEventHandler('EndRequest',array($this,'collectLogs'));
	}

	public function getConfigFile()
	{
		return $this->_configFile;
	}

	public function setConfigFile($value)
	{
		if(($this->_configFile=Prado::getPathOfNamespace($value,self::LOG_FILE_EXT))===null)
			throw new TConfigurationException('logrouter_configfile_invalid',$value);
	}

	public function collectLogs($param)
	{
		foreach($this->_routes as $route)
			$route->collectLogs();
	}
}


abstract class TLogRoute extends TComponent
{
	private static $_levelMap=array(
		'error'=>TLogger::ERROR,
		'debug'=>TLogger::DEBUG,
		'info'=>TLogger::INFO,
		'notice'=>TLogger::NOTICE,
		'warning'=>TLogger::WARNING,
		'error'=>TLogger::ERROR,
		'alert'=>TLogger::ALERT,
		'fatal'=>TLogger::FATAL
	);
	private $_levels=null;
	private $_categories=null;

	public function getLevels()
	{
		return $this->_levels;
	}

	public function setLevels($levels)
	{
		$this->_levels=null;
		$levels=strtolower($levels);
		foreach(explode(',',$levels) as $level)
		{
			$level=trim($level);
			if(isset(self::$_levelMap[$level]))
				$this->_levels|=self::$_levelMap[$level];
		}
		$this->_levels=$levels;
	}

	public function getCategories()
	{
		return $this->_categories;
	}

	public function setCategories($categories)
	{
		$this->_categories=null;
		foreach(explode(',',$categories) as $category)
		{
			if(($category=trim($category))!=='')
				$this->_categories[]=$category;
		}
	}

	public function collectLogs()
	{
		$logs=Prado::getLogger()->getLogs($this->getLevels(),$this->getCategories());
		$this->processLogs($logs);
	}

	protected function processLogs($logs);
}

class TFileLogRoute extends TLogRoute
{
	const LOG_FILE_EXT='.log';
	private $_file;

	public function getLogFile()
	{
		return $this->_file;
	}

	public function setLogFile($value)
	{
		if(($this->_file=Prado::getPathOfNamespace($value,self::LOG_FILE_EXT))===null)
			throw new TConfigurationException('filelogroute_logfile_invalid',$value);
	}

	protected function processLogs($logs)
	{
	}
}


?>