-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCxLogger.php
More file actions
151 lines (124 loc) · 3.52 KB
/
CxLogger.php
File metadata and controls
151 lines (124 loc) · 3.52 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
<?php
/*
* CxLogger is a library that allows PHP
* applications to log in easier.
* Depending on configuration, it logs to
* file, stdout or both. It distinguishes
* between 4 log levels:
** info - standard event
** warning - first level of danger
** standard_error - second level of danger, standard error
** critical_error - critical danger, app stop working for example
*
* Author: Cixo
*/
/* log levels */
abstract class Level{
const info = 0;
const warning = 1;
const standard_error = 2;
const critical_error = 3;
}
class CxLogger{
public function __construct(
?string $file_to_logging = null,
bool $log_to_stdout = true,
bool $log_to_stdout_warn_err = true
){
/*
* The constructor, if a path to a
* file was given, opens it
*/
$this->log_to_stdout = $log_to_stdout;
$this->log_to_stdout_warn_err = $log_to_stdout_warn_err;
if($file_to_logging !== null)
$this->openLogFile($file_to_logging);
}
public function log(int $level, ...$messages){
/*
* The function that creates the next
* log entry, level is the danger level,
* after which you can enter the
* parameters that will be combined into
* the log. After folding, he adds a
* time stamp and saves it
*/
$message_content = '';
foreach($messages as $message)
$message_content .= $message.' ';
$message_content .= "\n";
$message_level = self::level_to_string[$level];
$message_time = self::timeStamp();
$message_to_save = "[$message_level] [$message_time] $message_content";
$this->logStdout($message_to_save, $level);
$this->logFile($message_to_save);
}
/* handler for log file, if not logging to a file, null */
private $file_to_logging;
/* log to stdout, if true, then all logs are also saved to STDERR */
private bool $log_to_stdout;
/*
* if it is true, but $ log_to_stdout false,
* logs with log level> 0 will be added to STDERR
*/
private bool $log_to_stdout_warn_err;
/* log levels to text const */
private const level_to_string = [
'INFO',
'WARNING',
'ERROR',
'!!! CRITICAL ERROR !!!',
];
private function openLogFile(string $file_to_logging){
/*
* It tries to open the file to which
* it is to log, if it succeeds, it
* writes the handle, if not, it writes
* null and logs about it
*/
if(!@$this->file_to_logging = fopen($file_to_logging, 'a')){
$this->file_to_logging = null;
$this->log(
Level::standard_error,
"Can not open log file [$file_to_logging]!"
);
}
}
private function closeLogFile(){
/*
* Closes the log file, if it is open
*/
if($this->file_to_logging !== null)
fclose($this->file_to_logging);
}
private static function timeStamp() : string{
/*
* The function generates a timestamp
*/
return date('Y-m-d H:i:s');
}
private function logStdout(string $message, int $level){
/*
* Writes a message to SDTERR if
* the parameters allow it
*/
if($this->log_to_stdout or $this->log_to_stdout_warn_err and $level > 0)
fwrite(STDERR, $message);
}
private function logFile(string $message){
/*
* Writes the message to a file,
* if it is open
*/
if($this->file_to_logging !== null)
fputs($this->file_to_logging, $message);
}
public function __destruct(){
/*
* Destruktor, closes the file after
* destroying the logger
*/
$this->closeLogFile();
}
}
?>