diff options
author | Indieteq <admin@indieteq.com> | 2012-10-17 21:38:56 +0200 |
---|---|---|
committer | Indieteq <admin@indieteq.com> | 2012-10-17 21:38:56 +0200 |
commit | fda5f14b8fefad0a80d01b694ff5621fc0976894 (patch) | |
tree | d8654d8c0e9f8493befef1492b4c4d186a67395d /Log.class.php | |
parent | 43feb75d2886596d4b358c0830bdd9021c129ef7 (diff) | |
download | php-mysql-pdo-database-class-fda5f14b8fefad0a80d01b694ff5621fc0976894.zip php-mysql-pdo-database-class-fda5f14b8fefad0a80d01b694ff5621fc0976894.tar.gz php-mysql-pdo-database-class-fda5f14b8fefad0a80d01b694ff5621fc0976894.tar.bz2 |
The database files
Diffstat (limited to 'Log.class.php')
-rw-r--r-- | Log.class.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Log.class.php b/Log.class.php new file mode 100644 index 0000000..58e5672 --- /dev/null +++ b/Log.class.php @@ -0,0 +1,70 @@ +<?php + /* * + * Log A logger class which creates logs when an exception is thrown. + * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) + * @git htt://github.com/indieteq-vivek/simple-db-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, 'w') 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) // Check on true, Otherwise we would have a never-ending loop :S + { + $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); + } + } +?>
\ No newline at end of file |