-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLogger.php
72 lines (61 loc) · 1.67 KB
/
SimpleLogger.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
<?php
/*
* Simple php logger class
*
* Usage:
*
* $logger = new SimpleLogger($pathToLogFile)
*
* Will output data to file specified in $pathToLogFile
*
* All logs are appended to the current logfile.
*/
class SimpleLogger {
private $filePtr;
function __construct($pathToLogFile = "") {
try {
$logfile = $pathToLogFile . date("Y-m-d") . "_log.txt";
$this->filePtr = fopen($logfile, "a");
if ( !$this->filePtr ) {
throw new Exception('File open failed.');
}
}
catch ( Exception $e ) {
$this->filePtr = fopen("logfile.txt", "a"); // If the specified folder or file can not be opened, place a default logfile in the current folder
}
}
/*
* Create an entry in the logfile
*
* @param string $text The text string to write to the log file
* @param string $header Text of the log entry header tag. Can be any string, or use presets (d='DEBUG', i='INFO', e="ERROR")
*/
function log($text,$header = "d") {
$date = date(DATE_ATOM);
// Set the header tag
switch($header) {
case "i":
$headerText = "INFO";
break;
case "d":
$headerText = "DEBUG";
break;
case "e":
$headerText = "ERROR";
break;
default:
$headerText = $header;
break;
}
// Construct the log entry and write to the logfile
$logLine = $date . " [ " . $headerText . " ]: " . $text . "\n";
$this->writeToFile($logLine);
}
function writeToFile($logText) {
fwrite($this->filePtr, $logText);
}
function close() {
fclose($this->filePtr);
}
}
?>