ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLog.php
Go to the documentation of this file.
1<?php
2
30class ilLog
31{
32 private string $path = '';
33 private string $filename = '';
34 private string $tag = '';
35 private string $log_format = '';
36
40 private int $FATAL;
41
45 private int $WARNING;
46
50 private int $MESSAGE;
51
55 private $fp = null;
56
57 protected int $default_log_level;
58 protected int $current_log_level;
59 protected bool $enabled;
60
61 public function __construct(
62 string $a_log_path,
63 string $a_log_file,
64 string $a_tag = "",
65 bool $a_enabled = true,
66 ?int $a_log_level = null
67 ) {
68 // init vars
69
70 $this->FATAL = ilLogLevel::CRITICAL;
71 $this->WARNING = ilLogLevel::WARNING;
72 $this->MESSAGE = ilLogLevel::INFO;
73
74 $this->default_log_level = $this->WARNING;
75 $this->current_log_level = $this->setLogLevel($a_log_level ?? $this->default_log_level);
76
77 $this->path = ($a_log_path) ?: ILIAS_ABSOLUTE_PATH;
78 $this->filename = ($a_log_file) ?: "ilias.log";
79 $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
80 $this->enabled = $a_enabled;
81 $this->setLogFormat(date("[y-m-d H:i:s] ") . "[" . $this->tag . "] ");
82 $this->open();
83 }
84
85 public function setLogLevel(int $a_log_level): int
86 {
87 switch (strtolower($a_log_level)) {
88 case "fatal":
89 return $this->FATAL;
90 case "warning":
91 return $this->WARNING;
92 case "message":
93 return $this->MESSAGE;
94 default:
96 }
97 }
98
102 public function checkLogLevel($a_log_level): int
103 {
104 if (empty($a_log_level)) {
106 }
107 $level = (int) $a_log_level;
108 if ($a_log_level != (int) $a_log_level) {
110 }
111 return $level;
112 }
113
114 public function setLogFormat(string $a_format): void
115 {
116 $this->log_format = $a_format;
117 }
118
119 public function getLogFormat(): string
120 {
121 return $this->log_format;
122 }
123
124 public function setPath(string $a_str): void
125 {
126 $this->path = $a_str;
127
128 // on filename change reload close current file
129 if ($this->fp) {
130 fclose($this->fp);
131 $this->fp = null;
132 }
133 }
134
135 public function setFilename(string $a_str): void
136 {
137 $this->filename = $a_str;
138
139 // on filename change reload close current file
140 if ($this->fp) {
141 fclose($this->fp);
142 $this->fp = null;
143 }
144 }
145
146 public function setTag(string $a_str): void
147 {
148 $this->tag = $a_str;
149 }
150
157 public function writeLanguageLog(string $a_topic, string $a_lang_key): void
158 {
159 //TODO: go through logfile and search for the topic
160 //only write the log if the error wasn't reported yet
161 $this->write("Language (" . $a_lang_key . "): topic -" . $a_topic . "- not present", $this->MESSAGE);
162 }
163
167 public function writeWarning(string $a_message): void
168 {
169 $this->write("WARNING: " . $a_message);
170 }
171
176 public function logError(string $a_code, string $a_msg): void
177 {
178 switch ($a_code) {
179 case "3":
180 return; // don't log messages
181
182 case "2":
183 $error_level = "warning";
184 break;
185
186 case "1":
187 $error_level = "fatal";
188 break;
189
190 default:
191 $error_level = "unknown";
192 break;
193 }
194 $this->write("ERROR (" . $error_level . "): " . $a_msg);
195 }
196
211 public function write(string $a_msg, $a_log_level = null): void
212 {
213 $a_log_level = (int) $a_log_level;
214 if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level)) {
215 $this->open();
216
217 if ($this->fp == false) {
218 //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
219 }
220
221 if (fwrite($this->fp, $this->getLogFormat() . $a_msg . "\n") == -1) {
222 //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
223 }
224
225 // note: logStack() calls write() again, so do not make this call
226 // if no log level is given
227 if ($a_log_level == $this->FATAL) {
228 $this->logStack();
229 }
230 }
231 }
232
233 public function logStack(string $a_message = ''): void
234 {
235 try {
236 throw new Exception($a_message);
237 } catch (Exception $e) {
238 $this->write($e->getTraceAsString());
239 }
240 }
241
242 public function dump($a_var, ?int $a_log_level = null): void
243 {
244 $this->write(print_r($a_var, true), $a_log_level);
245 }
246
250 private function open(): void
251 {
252 if (!$this->fp) {
253 $this->fp = @fopen($this->path . "/" . $this->filename, "a");
254 }
255
256 if (!$this->fp && $this->enabled) {
257 throw new ilLogException('Unable to open log file for writing. Please check setup path to log file and possible write access.');
258 }
259 }
260
264 public function delete(): void
265 {
266 if (is_file($this->path . "/" . $this->filename)) {
267 unlink($this->path . "/" . $this->filename);
268 }
269 }
270} // END class.ilLog
ILIAS Log exception class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilLog.php:31
setTag(string $a_str)
int $default_log_level
Definition: class.ilLog.php:57
__construct(string $a_log_path, string $a_log_file, string $a_tag="", bool $a_enabled=true, ?int $a_log_level=null)
Definition: class.ilLog.php:61
string $path
Definition: class.ilLog.php:32
logStack(string $a_message='')
writeLanguageLog(string $a_topic, string $a_lang_key)
special language checking routine
open()
Open log file.
setFilename(string $a_str)
checkLogLevel($a_log_level)
int $WARNING
Log level 20: This is the standard log level that is set if no level is given.
Definition: class.ilLog.php:45
setLogLevel(int $a_log_level)
Definition: class.ilLog.php:85
string $log_format
Definition: class.ilLog.php:35
string $filename
Definition: class.ilLog.php:33
getLogFormat()
int $current_log_level
Definition: class.ilLog.php:58
write(string $a_msg, $a_log_level=null)
logging
setLogFormat(string $a_format)
logError(string $a_code, string $a_msg)
this function is automatically called by class.ilErrorHandler in case of an error To log manually ple...
setPath(string $a_str)
string $tag
Definition: class.ilLog.php:34
bool $enabled
Definition: class.ilLog.php:59
int $FATAL
Log level 10: Log only fatal errors that could lead to serious problems.
Definition: class.ilLog.php:40
dump($a_var, ?int $a_log_level=null)
int $MESSAGE
Log level 30: Logs messages and notices that are less important for system functionality like not tra...
Definition: class.ilLog.php:50
writeWarning(string $a_message)
special warning message