-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.php
More file actions
executable file
·82 lines (72 loc) · 2.94 KB
/
Log.php
File metadata and controls
executable file
·82 lines (72 loc) · 2.94 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
<?php
/**
* Make Log File with Content, Support Magic Variables for Path.
*
* Table of Magic Variables:
* Year `{{YYYY}}`|`{{YY}}` ; Month `{{MM}}` ; Day `{{DD}}` ;
* Hour `{{HH}}` ; Minute `{{MI}}` ; Second `{{SS}}` ;
* Time Zone `{{TZ}}` ; Time Zone String `{{TZS}}` .
*/
function MakeLog(string $Content, string $Path = "Log/{{YYYY}}-{{MM}}-{{DD}}.txt"): array {
$Response = [
"Ec" => 0,
"Em" => "",
"Path" => ""
];
$TimeConst = time();
$Path = str_replace("{{YYYY}}", date("Y", $TimeConst), $Path); // 年, Eg: 2021
$Path = str_replace("{{YY}}" , date("y", $TimeConst), $Path); // 年, Eg: 21
$Path = str_replace("{{MM}}" , date("m", $TimeConst), $Path); // 月, Eg: 01
$Path = str_replace("{{DD}}" , date("d", $TimeConst), $Path); // 日, Eg: 01
$Path = str_replace("{{HH}}" , date("H", $TimeConst), $Path); // 时, Eg: 00
$Path = str_replace("{{MI}}" , date("i", $TimeConst), $Path); // 分, Eg: 00
$Path = str_replace("{{SS}}" , date("s", $TimeConst), $Path); // 秒, Eg: 00
$Path = str_replace("{{TZ}}" , date("O", $TimeConst), $Path); // 时区, Eg: +0800
$Path = str_replace("{{TZS}}" , date("T", $TimeConst), $Path); // 时区, Eg: CST
try {
if (dirname($Path) && !file_exists(dirname($Path))) {
mkdir(dirname($Path), 0777, true);
}
} catch (Exception $Error) {
$Response["Ec"] = 50001; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
try {
$Fp = fopen($Path, "a");
if ($Fp === false) {
throw new Exception("Unable To Open File: $Path");
}
if (flock($Fp, LOCK_EX)) {
fwrite($Fp, sprintf("[%s] %s\n", date("Y-m-d H:i:s", $TimeConst), $Content));
fflush($Fp);
flock($Fp, LOCK_UN);
} else {
throw new Exception("Unable To Lock File: $Path");
}
fclose($Fp);
$Response["Path"] = $Path;
} catch (Exception $Error) {
$Response["Ec"] = 50002; $Response["Em"] = MakeErrorMessage($Error); return $Response;
}
return $Response;
}
/**
* Make Error Message, Include File, Line Number and Function Name, for Debugging.
*/
function MakeErrorMessage(Exception $Error): string {
if ($Error->getFile() && $Error->getLine()) {
$Modu = str_replace(getcwd() . DIRECTORY_SEPARATOR, "", $Error->getFile()) ?: "N/A";
$Func = isset($Error->getTrace()[0]["function"]) ? "Function <" . $Error->getTrace()[0]["function"] . ">" : "N/A";
$Line = $Error->getLine() ?: "N/A";
} else {
$Modu = "N/A";
$Func = "N/A";
$Line = "N/A";
}
if (get_class($Error)) {
$Type = get_class($Error);
} else {
$Type = "Error";
}
return sprintf("File \"%s\", Line %s, in %s @%s: %s.", $Modu, $Line, $Func, $Type, ucwords(rtrim($Error->getMessage(), ".")));
}
?>