|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trapdirector; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
define("ERROR", 1);define("WARN", 2);define("INFO", 3);define("DEBUG", 4); |
|
8
|
|
|
|
|
9
|
|
|
class Logging |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
//**** Options from config database |
|
13
|
|
|
// Default values |
|
14
|
|
|
public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
15
|
|
|
public $outputMode='syslog'; // alert type : file, syslog, display |
|
16
|
|
|
public $outputFile="/tmp/trapdebug.txt"; |
|
17
|
|
|
protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
18
|
|
|
protected $outputList=array('file', 'syslog', 'display'); |
|
19
|
|
|
|
|
20
|
|
|
/** Send log. Throws exception on critical error |
|
21
|
|
|
* @param string $message Message to log |
|
22
|
|
|
* @param int $level 1=critical 2=warning 3=trace 4=debug |
|
23
|
|
|
* @param string $destination file/syslog/display |
|
24
|
|
|
* @return void |
|
25
|
|
|
* @throws Exception |
|
26
|
|
|
**/ |
|
27
|
|
|
public function log( $message, $level, $destination ='') |
|
28
|
|
|
{ |
|
29
|
|
|
if ($this->debugLevel >= $level) |
|
30
|
|
|
{ |
|
31
|
|
|
$message = '['. date("Y/m/d H:i:s") . '] ' . |
|
32
|
|
|
'[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
33
|
|
|
|
|
34
|
|
|
$output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
35
|
|
|
switch ($output) |
|
36
|
|
|
{ |
|
37
|
|
|
case 'file': |
|
38
|
|
|
file_put_contents ($this->outputFile, $message , FILE_APPEND); |
|
39
|
|
|
break; |
|
40
|
|
|
case 'syslog': |
|
41
|
|
|
switch($level) |
|
42
|
|
|
{ |
|
43
|
|
|
case 1 : $prio = LOG_ERR;break; |
|
44
|
|
|
case 2 : $prio = LOG_WARNING;break; |
|
45
|
|
|
case 3 : $prio = LOG_INFO;break; |
|
46
|
|
|
case 4 : $prio = LOG_DEBUG;break; |
|
47
|
|
|
default: $prio = LOG_ERR; |
|
48
|
|
|
} |
|
49
|
|
|
syslog($prio,$message); |
|
50
|
|
|
break; |
|
51
|
|
|
case 'display': |
|
52
|
|
|
echo $message; |
|
53
|
|
|
break; |
|
54
|
|
|
default : // nothing we can do at this point |
|
55
|
|
|
throw new Exception($message); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
if ($level == 1) |
|
59
|
|
|
{ |
|
60
|
|
|
throw new Exception($message); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->setLevel($debugLvl); |
|
68
|
|
|
switch ($outputType) |
|
69
|
|
|
{ |
|
70
|
|
|
case 'file': |
|
71
|
|
|
if ($outputFile == null) throw new Exception("File logging without file !"); |
|
72
|
|
|
$this->setFile($outputFile); |
|
73
|
|
|
$this->setDestination('file'); |
|
74
|
|
|
break; |
|
75
|
|
|
default: |
|
76
|
|
|
$this->setDestination($outputType); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set logging level |
|
82
|
|
|
* @param integer $level |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setLevel($level) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!is_integer($level) || $level < 0 || $level > 10) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
throw new Exception('Invalid log level'); |
|
90
|
|
|
} |
|
91
|
|
|
$this->debugLevel=$level; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set logging destination |
|
96
|
|
|
* @param string $destination |
|
97
|
|
|
* @throws Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setDestination($destination) |
|
100
|
|
|
{ |
|
101
|
|
|
if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
throw new Exception('Invalid log destination'); |
|
104
|
|
|
} |
|
105
|
|
|
$this->outputMode=$destination; |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Set file destination |
|
109
|
|
|
* @param string $file |
|
110
|
|
|
* @throws Exception |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setFile($file) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!is_string($file)) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
throw new Exception('Invalid log file'); |
|
117
|
|
|
} |
|
118
|
|
|
$this->outputFile=$file; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |