-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.php
212 lines (178 loc) · 6.91 KB
/
Log.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Log.php
*
* @package GL
* @author Giorgi Lazashvili <giolaza@gmail.com>
*
* --CONFIGURATION--
* (constant) GIOLAZA_SHOW_ERRORS - true or false;
* (constant) GIOLAZA_SAVE_ERRORS - true or false;
* (constant) GIOLAZA_LOGS_FOLDER - log folder, if not set is root folder
*
*/
namespace GioLaza\Logger;
use Exception;
/**
* Class Log
* @package GioLaza\Logger
*/
class Log
{
/**
* Logs an error message, displays it if specified, and saves it to a log file if specified.
*
* @param string $text Error message to be logged
* @param string $filename Optional filename to save the log
* @param bool $engineForceStop Optional flag to force stop the engine after logging
* @param bool $displayErrors Optional flag to display errors
* @return void
*/
public static function logError(string $text, string $filename = '.logs.php', bool $engineForceStop = true, bool $displayErrors = true): void
{
$saveErrors = defined('GIOLAZA_SAVE_ERRORS') ? GIOLAZA_SAVE_ERRORS : true;
$projectFolder = defined('GIOLAZA_LOGS_FOLDER') ? GIOLAZA_LOGS_FOLDER : $_SERVER['DOCUMENT_ROOT'];
$filename = strtolower(substr($filename, -4)) != '.log' ? $filename . '.log' : $filename;
$fileLink = $projectFolder . '/' . $filename;
if ($displayErrors) {
self::showLog($text);
}
if (!$saveErrors) {
if ($engineForceStop) {
die(PHP_EOL . 'Engine force stop...' . PHP_EOL);
}
return;
}
if (!is_dir($projectFolder) && !@mkdir($projectFolder, 0775, true)) {
self::handleError('GL ENGINE ERROR - log folder "' . $projectFolder . '" does not exist and was unable to create it, please fix it to see GL system .logs', $engineForceStop);
return;
}
if (!file_exists($fileLink)) {
if (!self::fillEmpty($fileLink, $filename)) {
self::handleError('GL ENGINE ERROR - unable to create log file "' . $fileLink . '", please fix it to see GL system .logs', $engineForceStop);
return;
}
}
if (!is_writable($fileLink)) {
self::handleError('GL ENGINE ERROR - file "' . $fileLink . '" is not writable, please fix it to see GL system .logs', $engineForceStop);
return;
}
if (filesize($fileLink) < 100) {
self::fillEmpty($fileLink, $filename);
}
$debug = array_filter(debug_backtrace(), function ($value) {
return $value['function'] !== 'logError' && $value['function'] !== 'saveLog';
});
$sections = [
'TIME' => date('r'),
'ERROR' => $text,
'Debug' => print_r($debug, 1),
'GET' => print_r($_GET, 1),
'POST' => print_r($_POST, 1),
'Request' => print_r($_REQUEST, 1)
];
$textToWrite = '';
foreach ($sections as $title => $content) {
$textToWrite .= self::logTitle($title);
$textToWrite .= $content . PHP_EOL . self::logLines(100, 1);
}
file_put_contents($fileLink, $textToWrite, FILE_APPEND);
if ($engineForceStop) {
die(PHP_EOL . 'Log saved...' . '<br>' . PHP_EOL . 'Engine force stop...' . PHP_EOL);
}
}
/**
* Displays the log message on the screen with proper formatting.
*
* @param string $string Log message to be displayed
* @return void
*/
public static function showLog(string $string): void
{
$showErrors = defined('GIOLAZA_SHOW_ERRORS') && GIOLAZA_SHOW_ERRORS;
$text = PHP_EOL
. '<!-- ----------------------------------------------------------------------------- -->' . PHP_EOL
. '<!-- ----------------------------------------------------------------------------- -->' . PHP_EOL;
if ($showErrors) {
$text .= PHP_EOL . '<div style="text-align: center"><b style="color: darkred">' . PHP_EOL
. '<h2>' . 'SOMETHING WENT WRONG' . '</h2>' . PHP_EOL
. $string . PHP_EOL
. '</b></div>' . PHP_EOL;
}
else {
$text .= PHP_EOL . '<div style="text-align: center"><b style="color: darkred">' . PHP_EOL
. '<h2>' . 'SOMETHING WENT WRONG' . '</h2>' . PHP_EOL
. '</b></div>' . PHP_EOL;
}
$text .= PHP_EOL
. '<!-- ----------------------------------------------------------------------------- -->' . PHP_EOL
. '<!-- ----------------------------------------------------------------------------- -->' . PHP_EOL;
echo $text;
}
private static function handleError(string $message, bool $engineForceStop): void
{
@error_log($message);
if ($engineForceStop) {
die(PHP_EOL . 'Engine force stop...' . PHP_EOL);
}
}
/**
* Fills an empty log file with standard text.
*
* @param string $fileLink File link to the log file
* @param string $filename Filename of the log file
* @return bool Returns true on success
*/
public static function fillEmpty(string $fileLink, string $filename): bool
{
$standardText = ' * ' . $filename . PHP_EOL
. ' *' . PHP_EOL
. ' * @category System' . PHP_EOL
. ' * @package GL' . PHP_EOL
. ' * @author Giorgi Lazashvili <giolaza@gmail.com>' . PHP_EOL
. ' * @version 4.0 (28 APR 2023)' . PHP_EOL
. ' * @created (' . date('r') . ')' . PHP_EOL
. ' *' . PHP_EOL
. ' *' . PHP_EOL;
try{
file_put_contents($fileLink, $standardText, FILE_APPEND);
chmod($fileLink, 0775);
} catch (Exception $e){
return false;
}
return true;
}
/**
* Creates a formatted string for a log title.
*
* @param string $textSTR Title text
* @param string $symbol Symbol to be used for the title (default is '*')
* @return string Formatted log title string
*/
public static function logTitle(string $textSTR, string $symbol = '*'): string
{
$text = PHP_EOL . PHP_EOL;
$text .= $symbol . $symbol . $symbol;
$text .= strtoupper($textSTR);
$text .= PHP_EOL;
return $text;
}
/**
* Creates a formatted string of repeating symbols for log formatting purposes.
*
* @param int $x Width of the formatted string
* @param int $y Height of the formatted string
* @param string $symbol Symbol to be repeated (default is '-')
* @return string Formatted string of repeating symbols
*/
public static function logLines(int $x, int $y, string $symbol = '-'): string
{
$text = PHP_EOL . PHP_EOL;
for ($i = 0; $i < $y; $i++) {
$text .= str_repeat($symbol, $x);
$text .= PHP_EOL;
}
$text .= PHP_EOL;
return $text;
}
}