-
Notifications
You must be signed in to change notification settings - Fork 387
/
Log.class.php
70 lines (65 loc) · 2.11 KB
/
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
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
<?php
/* *
* Log A logger class which creates logs when an exception is thrown.
* @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal)
* @git https://github.com/wickyaswal/PHP-MySQL-PDO-Database-Class
* @version 0.1a
*/
class Log {
# @string, Log directory name
private $path = '/logs/';
# @void, Default Constructor, Sets the timezone and path of the log files.
public function __construct() {
date_default_timezone_set('Europe/Amsterdam');
$this->path = dirname(__FILE__) . $this->path;
}
/**
* @void
* Creates the log
*
* @param string $message the message which is written into the log.
* @description:
* 1. Checks if directory exists, if not, create one and call this method again.
* 2. Checks if log already exists.
* 3. If not, new log gets created. Log is written into the logs folder.
* 4. Logname is current date(Year - Month - Day).
* 5. If log exists, edit method called.
* 6. Edit method modifies the current log.
*/
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);
}
}
}
/**
* @void
* Gets called if log exists.
* Modifies current log and adds the message to the log.
*
* @param string $log
* @param DateTimeObject $date
* @param string $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);
}
}
?>