-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWriter.php
More file actions
242 lines (200 loc) · 7.69 KB
/
Writer.php
File metadata and controls
242 lines (200 loc) · 7.69 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Log writer
*
* @author Dejan Samardzija, [email protected]
* @copyright Little Genius Studio www.littlegeniusstudio.com All rights reserved
* @version 1.0
*/
namespace G4\Log;
use G4\Utility\Tools;
class Writer
{
const LOGPATH_FORMAT_SIMPLE = 1;
const LOGPATH_FORMAT_COMPLEX = 2;
/**
* Default log file path format
* @var int
*/
private static $logPathFormat = self::LOGPATH_FORMAT_COMPLEX;
/**
* List of valid log file path formats
* @var array
*/
private static $_validLogPathFormats = array(
self::LOGPATH_FORMAT_SIMPLE,
self::LOGPATH_FORMAT_COMPLEX,
);
public static function setLogPathFormat($format)
{
if(!in_array($format, self::$_validLogPathFormats)) {
throw new \Exception('Selected Log file path format is not valid');
}
self::$logPathFormat = $format;
}
/**
* Dump debug variable
*
* @author Dejan Samardzija <[email protected]>
* @param mixed $var - variable
* @param bool $die - terminate script after dump
* @param string $color - text color
* @param bool $parsable - if true it uses var_export that dumps php usable value
* @param bool $output - if true it will echo formated string, for false it will return and override die part
* @param bool $indirect - if call is direct or from wrapper function
* @return void
*/
public static function preVarDump($var, $die = false, $color = '#000000', $parsable = false, $output = true, $indirect = false)
{
// last line of defense ;)
if(!defined('DEBUG') || DEBUG !== true) {
return false;
}
$formated = '';
// if ajax or cli, skip preformating
$skipFormating =
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
|| php_sapi_name() == 'cli'
|| (defined('DEBUG_SKIP_FORMATTING') && DEBUG_SKIP_FORMATTING)
|| !$output;
$skipFormating || $formated .= "<pre style='padding: 5px; background:#ffffff; font: normal 12px \"Lucida Console\", \"Courier\",
monospace; position:relative; clear:both; color:{$color}; border:1px solid {$color}; text-align: left !important;'>";
$formated .= self::_preFormat($var, $parsable, intval($indirect) + 1, $skipFormating);
$skipFormating || $formated .= "</pre>";
if(!$output) {
return $formated;
}
echo $formated . "\n";
if($die) {
die("\n\nDebug terminated\n");
}
}
/**
*
*/
private static function _preFormat($var, $parsable = false, $indirect = false, $skipFormating = false)
{
$index = intval($indirect);
$trace = debug_backtrace();
$line = isset($trace[$index]['line']) ? $trace[$index]['line'] : null;
$file = isset($trace[$index]['file']) ? $trace[$index]['file'] : null;
$type = gettype($var);
if(is_object($var) || is_array($var)) {
$var = !$parsable
? print_r($var, true)
: var_export($var, true);
} elseif(is_resource($var)) {
$var = get_resource_type($var) . ': ' . $var;
}
return "\nCalled in '{$file}' LINE {$line}\nvar dump: ({$type}):\n" . ($skipFormating ? $var : htmlspecialchars($var));
}
/**
* Write to a log file
*
* @param string $filename - log file name
* @param string $string - text to write in log file
* @return bool
*/
public static function writeLog ($filename, $string)
{
$logs_path = defined('PATH_LOGS')
? PATH_LOGS
: getcwd();
$logs_path_real = realpath($logs_path);
if(!$logs_path_real || !is_writable($logs_path_real)) {
throw new \Exception("Log path is not accessible or writable");
}
$file_name = $logs_path_real . DIRECTORY_SEPARATOR . strtolower(trim($filename));
$file_folder = dirname($file_name);
if (!is_dir($file_folder)) {
if (!mkdir($file_folder, 0766, true)) {
throw new \Exception("Couldn't create logs subfolder");
}
}
if (!is_file($file_name)) {
if (@touch($file_name)) {
@chmod($file_name, 0777);
}
}
return file_put_contents($file_name, $string . PHP_EOL, FILE_APPEND | LOCK_EX);
}
/**
* Wrapper for standard writeLog() function, that injects full date before extension part of filename and any other parameters provided
*
* @param string $filename - log file name
* @param string $string - text to write in log file
* @param string|array $extras - other info you need to insert into file name
* @return bool
*/
public static function writeLogVerbose($filename, $string, $extras = array())
{
$filename = self::_formatFilePath($filename, self::$logPathFormat, $extras);
return self::writeLog($filename, $string);
}
/**
* Format file path
*
* @param string $filename
* @param int $format
* @param array $extras
* @param bool $addHostname
* @throws \Exception
* @return string
*/
private static function _formatFilePath($filename, $format, $extras = array(), $addHostname = false)
{
// we can parse scalars also
if(is_scalar($extras)) {
$extras = array($extras);
}
if(!is_array($extras)) {
throw new \Exception('Extras parameter must be array');
}
$dot_pos = strrpos($filename, '.');
if(!$dot_pos) {
$filename .= '.log';
$dot_pos = strrpos($filename, '.');
}
$tmp = strlen($filename) - $dot_pos;
switch ($format) {
case self::LOGPATH_FORMAT_COMPLEX:
// add short date and 24 hour format as last parama
$extras[] = $addHostname
? substr($filename, 0, $dot_pos) . '_' . gethostname()
: substr($filename, 0, $dot_pos);
$extras = array_merge($extras, explode('-', date("H-d-m-Y", Tools::ts())));
// reverse order or extras array so that year is first, month etc...
$extras = array_reverse($extras);
$glue = DIRECTORY_SEPARATOR;
break;
default:
case self::LOGPATH_FORMAT_SIMPLE:
// add machine hostname to extra array
$extras[] = substr($filename, 0, $dot_pos);
if($addHostname) {
$extras[] = gethostname();
}
$extras[] = date("Y-m-d", Tools::ts());
$glue = '_';
break;
}
return implode($glue, $extras) . substr($filename, "-{$tmp}", $tmp);
}
/**
* It will use preVarDump to format variable and WriteLogVerbose to log it to file
*
* @param mixed $var variable to parse and log
* @param string $filename log file name
* @param bool $addRequestData if we want to log more data, request, time...
* @param int $traceIndex debug trace index so we have where method was called
* @return bool
*/
public static function writeLogPre($var, $filename = '__preformated.log', $addRequestData = false, $traceIndex = 1)
{
$msg = self::_preFormat($var, false, $traceIndex, true);
if($addRequestData) {
$msg = Debug::formatHeaderWithTime() . $msg . Debug::formatRequestData();
}
return self::writeLogVerbose($filename, $msg);
}
} // end of class