File.php 1.19 KB
Newer Older
ale's avatar
ale committed
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
<?php
	
	namespace WP_Piwik\Logger;
	
	class File extends \WP_Piwik\Logger {
	
		private $loggerFile = null;
	
		private function encodeFilename($fileName) {
			$fileName = str_replace (' ', '_', $fileName);
			preg_replace('/[^0-9^a-z^_^.]/', '', $fileName);
			return $fileName;
		}
		
		private function setFilename() {
			$this->loggerFile = WP_PIWIK_PATH.'logs'.DIRECTORY_SEPARATOR.
				date('Ymd').'_'.$this->encodeFilename($this->getName()).'.log';
		}
		
		private function getFilename() {
			return $this->loggerFile;
		}
		
		private function openFile() {
			if (!$this->loggerFile)
				$this->setFilename();
			return fopen($this->getFilename(), 'a');			
		}
		
		private function closeFile($fileHandle) {
			fclose($fileHandle);
		}
		
		private function writeFile($fileHandle, $fileContent) {
			fwrite($fileHandle, $fileContent."\n");
		}
		
		private function formatMicrotime($loggerTime) {
			return sprintf('[%6s sec]',number_format($loggerTime,3));
		}
		
		public function loggerOutput($loggerTime, $loggerMessage) {
			if ($fileHandle = $this->openFile()) {
				$this->writeFile($fileHandle, $this->formatMicrotime($loggerTime).' '.$loggerMessage);
				$this->closeFile($fileHandle);
			}
		}
    }