Passed
Push — master ( df2595...b8f383 )
by Patrick
01:54
created

Logging   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
dl 0
loc 110
rs 10
c 1
b 0
f 0
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setDestination() 0 7 3
A setFile() 0 7 2
A setLevel() 0 7 4
A setLogging() 0 12 3
B log() 0 34 11
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)
0 ignored issues
show
introduced by
The condition is_integer($level) is always true.
Loading history...
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))
0 ignored issues
show
introduced by
The condition is_string($destination) is always true.
Loading history...
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))
0 ignored issues
show
introduced by
The condition is_string($file) is always true.
Loading history...
115
        {
116
            throw new Exception('Invalid log file');
117
        }
118
        $this->outputFile=$file;
119
    }
120
    
121
}