Completed
Push — master ( ee71a2...2d5b89 )
by Greg
05:23
created

Log::error()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * /classes/DomainMOD/Log.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class Log
25
{
26
    public $area;
27
    public $time;
28
    public $url;
29
    public $user_id;
30
31
    public function __construct($area)
32
    {
33
        $this->area = $area;
34
        $this->time = new Time();
35
        $this->url = $_SERVER['REQUEST_URI'];
36
37
        if ($_SESSION['s_user_id']) {
38
            $this->user_id = $_SESSION['s_user_id'];
39
        } else {
40
            $this->user_id = 0;
41
        }
42
    }
43
44
    public function addEntry($level, $message, $extra_info)
45
    {
46
        $system = new System();
47
        if ($extra_info != '') {
48
            $extra_info_formatted = $this->formatExtraInfo($extra_info);
49
        } else {
50
            $extra_info_formatted = '';
51
        }
52
        $tmpq = $system->db()->prepare("
53
            INSERT INTO `log`
54
            (`user_id`, `area`, `level`, `message`, `extra`, `url`, `insert_time`)
55
            VALUES
56
            (:user_id, :area, :level, :message, :extra, :url, :insert_time)");
57
        $tmpq->execute(['user_id' => $this->user_id,
58
                        'area' => $this->area,
59
                        'level' => $level,
60
                        'message' => $message,
61
                        'extra' => $extra_info_formatted,
62
                        'url' => $this->url,
63
                        'insert_time' => $this->time->stamp()]);
64
    }
65
66
    public function formatExtraInfo($extra_info)
67
    {
68
        $extra_info_formatted = '';
69
        foreach ($extra_info as $key => $value) {
70
            $extra_info_formatted .= '"' . $key . '":"' . $value . '", ';
71
        }
72
        return substr($extra_info_formatted, 0, -2);
73
    }
74
    /*
75
     * EMERGENCY
76
     * System is unusable.
77
     */
78
    public function emergency($message, $extra_info = '')
79
    {
80
        if (DEBUG_MODE != 1) return;
81
        $this->addEntry('emergency', $message, $extra_info);
82
    }
83
84
    /*
85
     * ALERT
86
     * Action must be taken immediately.
87
     * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
88
     */
89
    public function alert($message, $extra_info = '')
90
    {
91
        if (DEBUG_MODE != 1) return;
92
        $this->addEntry('alert', $message, $extra_info);
93
    }
94
95
    /*
96
     * CRITICAL
97
     * Critical conditions.
98
     * Example: Application component unavailable, unexpected exception.
99
     */
100
    public function critical($message, $extra_info = '')
101
    {
102
        if (DEBUG_MODE != 1) return;
103
        $this->addEntry('critical', $message, $extra_info);
104
    }
105
106
    /*
107
     * ERROR
108
     * Runtime errors that do not require immediate action but should typically be logged and monitored.
109
     */
110
    public function error($message, $extra_info = '')
111
    {
112
        if (DEBUG_MODE != 1) return;
113
        $this->addEntry('error', $message, $extra_info);
114
    }
115
116
    /*
117
     * WARNING
118
     * Exceptional occurrences that are not errors.
119
     * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
120
     */
121
    public function warning($message, $extra_info = '')
122
    {
123
        if (DEBUG_MODE != 1) return;
124
        $this->addEntry('warning', $message, $extra_info);
125
    }
126
127
    /*
128
     * NOTICE
129
     * Normal but significant events.
130
     */
131
    public function notice($message, $extra_info = '')
132
    {
133
        if (DEBUG_MODE != 1) return;
134
        $this->addEntry('notice', $message, $extra_info);
135
    }
136
137
    /*
138
     * INFO
139
     * Interesting events.
140
     * Example: User logs in, SQL logs.
141
     */
142
    public function info($message, $extra_info = '')
143
    {
144
        if (DEBUG_MODE != 1) return;
145
        $this->addEntry('info', $message, $extra_info);
146
    }
147
148
    /*
149
     * DEBUG
150
     * Detailed debug information.
151
     */
152
    public function debug($message, $extra_info = '')
153
    {
154
        if (DEBUG_MODE != 1) return;
155
        $this->addEntry('debug', $message, $extra_info);
156
    }
157
158
    /*
159
     * LOG
160
     * Logs with an arbitrary level.
161
     */
162
    public function log($message, $extra_info = '')
163
    {
164
        if (DEBUG_MODE != 1) return;
165
        $this->addEntry('log', $message, $extra_info);
166
    }
167
168
} //@formatter:on
169