-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.php
More file actions
137 lines (107 loc) · 4.25 KB
/
Logger.php
File metadata and controls
137 lines (107 loc) · 4.25 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
<?php
class Logger
{
const FUNCTION = 'function';
const SERVER_PHPSELF = 'PHP_SELF';
const ENTRY = 'Entry -> ';
const EXIT = 'Exit -> ';
const LOGLEVEL_TRACE = 'TRACE';
const LOGLEVEL_INFO = 'INFO ';
const LOGLEVEL_ERROR = 'ERROR';
const LOGLEVEL_WARN = 'WARN ';
const LOGLEVEL_SQL = 'SQL ';
const SESSION_USERDATA = 'userdata';
const LOCALHOST_IPV4 = '127.0.0.1';
const LOCALHOST_IPV6 = '::1';
public static function trace(string $message): void
{
$methodName = debug_backtrace()[1][self::FUNCTION] ?? '';
if (empty($methodName)) {
$methodName = $_SERVER[self::SERVER_PHPSELF];
}
self::logit(self::formatMethodNameForLogging($methodName) . $message, self::LOGLEVEL_TRACE);
}
public static function traceEntry(): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . self::ENTRY, self::LOGLEVEL_TRACE);
}
public static function traceEntryData(mixed ...$data): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . self::ENTRY . json_encode($data), self::LOGLEVEL_TRACE);
}
public static function traceExit(): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . self::EXIT, self::LOGLEVEL_TRACE);
}
public static function traceExitData(mixed $data): mixed
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . self::EXIT . json_encode($data), self::LOGLEVEL_TRACE);
return $data;
}
public static function traceExitDataMsg(string $message, mixed $data): mixed
{
if (!empty($message)) {
$resultMessage = self::EXIT . $message . " " . json_encode($data);
} else {
$resultMessage = self::EXIT . json_encode($data);
}
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . $resultMessage, self::LOGLEVEL_TRACE);
return $data;
}
public static function info(string $message): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . $message, self::LOGLEVEL_INFO);
}
public static function error(string $message): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . $message, self::LOGLEVEL_ERROR);
}
public static function warn(string $message): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . $message, self::LOGLEVEL_WARN);
}
public static function sql(string $sql): void
{
self::logit(self::formatMethodNameForLogging(debug_backtrace()[1][self::FUNCTION] ?? '') . formatString(EXECUTE_SQL, [$sql]), self::LOGLEVEL_SQL);
}
public static function logit(string $message, string $level): void
{
// falls kein Logverzeichnis existiert
if (!file_exists(PROJECT_LOGFOLDERNAME)) {
mkdir(PROJECT_LOGFOLDERNAME, DEFAULT_LOGFOLDER_PERMISSION, true);
}
$date = new DateTime();
$dateStringLogfileName = date_format($date, DATE_PATTERN_YMD);
$dateStringLogger = date_format($date, DATETIME_PATTERN_YMDHMSS);
$ip = $_SERVER["REMOTE_ADDR"];
$logType = 3; // Type 3 = Log in Datei
$logFile = PROJECT_LOGFOLDERNAME . '/' . $dateStringLogfileName . LOG_FILEEXTENSION;
if ($ip == self::LOCALHOST_IPV6) {
$ip = self::LOCALHOST_IPV4;
}
$logMessage = $dateStringLogger . ' [' . $level . '] [' . self::usernameAndId() . '] (' . $ip . ') | ' . $message . "\n";
error_log($logMessage, $logType, $logFile);
}
private static function usernameAndId(): String
{
$username = STR_EMPTY;
$userid = STR_EMPTY;
if (isset($_SESSION)) {
$userdata = $_SESSION[self::SESSION_USERDATA];
$username = $userdata[FIELD_NAME];
$userid = $userdata[FIELD_ID];
}
return $userid . '|' . $username;
}
public static function formatMethodNameForLogging(string $methodName): string
{
$maxCharacter = 38;
$methodString = $methodName . '()';
if (strlen($methodString) < $maxCharacter) {
$resultString = str_pad($methodString, $maxCharacter);
} else {
$resultString = substr($methodString, strlen($methodString));
}
return '[' . $resultString . '] ';
}
}