CLogger   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 194
rs 10
wmc 13
lcom 2
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B setContext() 0 32 4
A log() 0 3 1
A emergency() 0 3 1
A alert() 0 3 1
A critical() 0 3 1
A error() 0 3 1
A warning() 0 3 1
A notice() 0 3 1
A info() 0 3 1
A debug() 0 3 1
1
<?php
2
3
namespace Anax\Log;
4
5
/**
6
 * Anax default logger instance
7
 *
8
 * The message MUST be a string or object implementing __toString().
9
 *
10
 * The message MAY contain placeholders in the form: {foo} where foo
11
 * will be replaced by the context data in key "foo".
12
 *
13
 * The context array can contain arbitrary data, the only assumption that
14
 * can be made by implementors is that if an Exception instance is given
15
 * to produce a stack trace, it MUST be in a key named "exception".
16
 *
17
 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
18
 * for the full interface specification.
19
 */
20
class CLogger 
21
{
22
23
    /**
24
     * Constans for loglevel.
25
     *
26
     */
27
    const EMERGENCY = 'emergency';
28
    const ALERT     = 'alert';
29
    const CRITICAL  = 'critical';
30
    const ERROR     = 'error';
31
    const WARNING   = 'warning';
32
    const NOTICE    = 'notice';
33
    const INFO      = 'info';
34
    const DEBUG     = 'debug';
35
36
37
38
    /**
39
     * Constans for loglevel.
40
     *
41
     */
42
    private $context = 'development'; // production, development or debug
43
44
45
46
    /**
47
     * Init the logger depending on its context production, development or debug.
48
     *
49
     * @param string $context as production, development or debug, default is development
50
     * @return $this
51
     */
52
    public function setContext($context = 'development') {
53
54
        switch($context) {
55
            
56
            case 'production':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
57
58
                break;
59
            
60
            case 'development':
61
                error_reporting(-1);              // Report all type of errors
62
                ini_set('display_errors', 1);     // Display all errors 
63
                ini_set('output_buffering', 0);   // Do not buffer output
64
65
                set_exception_handler(function ($exception) {
66
                    echo "Anax: Uncaught exception: <p>" . 
67
                        $exception->getMessage() . "</p><pre>" . 
68
                        $exception->getTraceAsString() . "</pre>";
69
                });
70
                break;
71
            
72
            case 'debug':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73
74
                break;
75
            
76
            default:
77
                throw new Exception('Unknown context.');
78
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
79
        }
80
81
        $this->context = $context;
82
        return $this;
83
    }
84
85
86
87
    /**
88
     * Logs with an arbitrary level.
89
     *
90
     * @param mixed $level
91
     * @param string $message
92
     * @param array $context
93
     * @return null
94
     */
95
    public function log($level, $message, array $context = array()) {
96
        echo "Level: " . $level . "<br>" . "Message: " . $message . "<br>" . htmlentities(print_r($context, 1)) . "<br>";
97
    }
98
99
100
101
    /**
102
     * System is unusable.
103
     *
104
     * @param string $message
105
     * @param array $context
106
     * @return null
107
     */
108
    public function emergency($message, array $context = array()) {
109
        $this->log(self::EMERGENCY, $message, $context);
110
    }
111
112
113
114
    /**
115
     * Action must be taken immediately.
116
     *
117
     * Example: Entire website down, database unavailable, etc. This should
118
     * trigger the SMS alerts and wake you up.
119
     *
120
     * @param string $message
121
     * @param array $context
122
     * @return null
123
     */
124
    public function alert($message, array $context = array()) {
125
        $this->log(self::ALERT, $message, $context);
126
    }
127
128
129
130
    /**
131
     * Critical conditions.
132
     *
133
     * Example: Application component unavailable, unexpected exception.
134
     *
135
     * @param string $message
136
     * @param array $context
137
     * @return null
138
     */
139
    public function critical($message, array $context = array()) {
140
        $this->log(self::CRITICAL, $message, $context);
141
    }
142
143
144
145
    /**
146
     * Runtime errors that do not require immediate action but should typically
147
     * be logged and monitored.
148
     *
149
     * @param string $message
150
     * @param array $context
151
     * @return null
152
     */
153
    public function error($message, array $context = array()) {
154
        $this->log(self::ERROR, $message, $context);
155
    }
156
157
158
159
    /**
160
     * Exceptional occurrences that are not errors.
161
     *
162
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
163
     * that are not necessarily wrong.
164
     *
165
     * @param string $message
166
     * @param array $context
167
     * @return null
168
     */
169
    public function warning($message, array $context = array()) {
170
        $this->log(self::WARNING, $message, $context);
171
    }
172
173
174
175
    /**
176
     * Normal but significant events.
177
     *
178
     * @param string $message
179
     * @param array $context
180
     * @return null
181
     */
182
    public function notice($message, array $context = array()) {
183
        $this->log(self::NOTICE, $message, $context);
184
    }
185
186
187
188
    /**
189
     * Interesting events.
190
     *
191
     * Example: User logs in, SQL logs.
192
     *
193
     * @param string $message
194
     * @param array $context
195
     * @return null
196
     */
197
    public function info($message, array $context = array()) {
198
        $this->log(self::INFO, $message, $context);
199
    }
200
201
202
203
    /**
204
     * Detailed debug information.
205
     *
206
     * @param string $message
207
     * @param array $context
208
     * @return null
209
     */
210
    public function debug($message, array $context = array()) {
211
        $this->log(self::DEBUG, $message, $context);
212
    }
213
}
214
215
216
217