Log::addEntry()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 24
rs 9.7333
1
<?php
2
namespace GJClasses;
3
4
class Log
5
{
6
    public $user_id;
7
    public $area;
8
    public $time;
9
    public $url;
10
11
    public function __construct($area)
12
    {
13
        $this->user_id = isset($_SESSION['s_user_id']) ? $_SESSION['s_user_id'] : 0;
14
        $this->area = $area;
15
        $this->time = new Time();
16
        $this->url = $_SERVER['REQUEST_URI'];
17
    }
18
19
    public function addEntry($level, $message, $extra_info)
20
    {
21
        $deeb = Database::getInstance();
22
        $pdo = $deeb->cnxx;
23
        if (!empty($extra_info)) {
24
            $extra_info_formatted = $this->formatExtraInfo($extra_info);
25
        } else {
26
            $extra_info_formatted = '';
27
        }
28
29
        $stmt = $pdo->prepare("
30
            INSERT INTO `log`
31
            (`user_id`, `area`, `level`, `message`, `extra`, `url`, `insert_time`)
32
            VALUES
33
            (:user_id, :area, :level, :message, :extra, :url, :insert_time)");
34
        $stmt->bindValue('user_id', $this->user_id, \PDO::PARAM_INT);
35
        $stmt->bindValue('area', $this->area, \PDO::PARAM_STR);
36
        $stmt->bindValue('level', $level, \PDO::PARAM_STR);
37
        $stmt->bindValue('message', $message, \PDO::PARAM_LOB);
38
        $stmt->bindValue('extra', $extra_info_formatted, \PDO::PARAM_LOB);
39
        $stmt->bindValue('url', $this->url, \PDO::PARAM_LOB);
40
        $bind_timestamp = $this->time->stamp();
41
        $stmt->bindValue('insert_time', $bind_timestamp, \PDO::PARAM_STR);
42
        $stmt->execute();
43
    }
44
45
    public function formatExtraInfo($extra_info)
46
    {
47
        $extra_info_formatted = '';
48
        $last_error = error_get_last();
49
        $last_error_message = array('Last Error' => $last_error['message']);
50
        $merged_array = array_merge($last_error_message, $extra_info);
51
        foreach ($merged_array as $key => $value) {
52
            $extra_info_formatted .= '"' . $key . '":"' . $value . '", ';
53
        }
54
        return substr($extra_info_formatted, 0, -2);
55
    }
56
57
    /*
58
     * EMERGENCY
59
     * System is unusable.
60
     */
61
    public function emergency($message, $extra_info = array())
62
    {
63
        $this->addEntry('emergency', $message, $extra_info);
64
    }
65
66
    /*
67
     * ALERT
68
     * Action must be taken immediately.
69
     * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
70
     */
71
    public function alert($message, $extra_info = array())
72
    {
73
        $this->addEntry('alert', $message, $extra_info);
74
    }
75
76
    /*
77
     * CRITICAL
78
     * Critical conditions.
79
     * Example: Application component unavailable, unexpected exception.
80
     */
81
    public function critical($message, $extra_info = array())
82
    {
83
        $this->addEntry('critical', $message, $extra_info);
84
    }
85
86
    /*
87
     * ERROR
88
     * Runtime errors that do not require immediate action but should typically be logged and monitored.
89
     */
90
    public function error($message, $extra_info = array())
91
    {
92
        $this->addEntry('error', $message, $extra_info);
93
    }
94
95
    /*
96
     * WARNING
97
     * Exceptional occurrences that are not errors.
98
     * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
99
     */
100
    public function warning($message, $extra_info = array())
101
    {
102
        $this->addEntry('warning', $message, $extra_info);
103
    }
104
105
    /*
106
     * NOTICE
107
     * Normal but significant events.
108
     */
109
    public function notice($message, $extra_info = array())
110
    {
111
        $this->addEntry('notice', $message, $extra_info);
112
    }
113
114
    /*
115
     * INFO
116
     * Interesting events.
117
     * Example: User logs in, SQL logs.
118
     */
119
    public function info($message, $extra_info = array())
120
    {
121
        $this->addEntry('info', $message, $extra_info);
122
    }
123
124
    /*
125
     * DEBUG
126
     * Detailed debug information.
127
     */
128
    public function debug($message, $extra_info = array())
129
    {
130
        $this->addEntry('debug', $message, $extra_info);
131
    }
132
133
    /*
134
     * LOG
135
     * Logs with an arbitrary level.
136
     */
137
    public function log($message, $extra_info = array())
138
    {
139
        $this->addEntry('log', $message, $extra_info);
140
    }
141
142
}
143