forked from lincanbin/Carbon-Forum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDO.Log.class.php
35 lines (34 loc) · 912 Bytes
/
PDO.Log.class.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
<?php
class Log
{
private $path = '/logs/';
public function __construct()
{
$this->path = dirname(__FILE__) . $this->path;
}
public function write($message)
{
$date = new DateTime();
$log = $this->path . $date->format('Y-m-d') . ".txt";
if (is_dir($this->path)) {
if (!file_exists($log)) {
$fh = fopen($log, 'a+') or die("Fatal Error !");
$logcontent = "Time : " . $date->format('H:i:s') . "\r\n" . $message . "\r\n";
fwrite($fh, $logcontent);
fclose($fh);
} else {
$this->edit($log, $date, $message);
}
} else {
if (mkdir($this->path, 0777) === true) {
$this->write($message);
}
}
}
private function edit($log, $date, $message)
{
$logcontent = "Time : " . $date->format('H:i:s') . "\r\n" . $message . "\r\n\r\n";
$logcontent = $logcontent . file_get_contents($log);
file_put_contents($log, $logcontent);
}
}